Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove integrationhelper requirement #53

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions custom_components/healthchecksio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@
import asyncio
import os
from datetime import timedelta
from logging import getLogger

import async_timeout
from homeassistant import config_entries, core
from homeassistant.const import Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle
from integrationhelper import Logger
from integrationhelper.const import CC_STARTUP_VERSION

from .const import (
DOMAIN,
DOMAIN_DATA,
INTEGRATION_VERSION,
ISSUE_URL,
OFFICIAL_SITE_ROOT,
REQUIRED_FILES,
)
Expand All @@ -32,13 +29,13 @@
Platform.BINARY_SENSOR,
]

LOGGER = getLogger(__name__)


async def async_setup(hass: core.HomeAssistant, config: ConfigType):
"""Set up this component using YAML is not supported."""
if config.get(DOMAIN) is not None:
Logger("custom_components.healthchecksio").error(
"Configuration with YAML is not supported"
)
LOGGER.error("Configuration with YAML is not supported")

return True

Expand All @@ -47,13 +44,6 @@ async def async_setup_entry(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
"""Set up this integration using UI."""
# Print startup message
Logger("custom_components.healthchecksio").info(
CC_STARTUP_VERSION.format(
name=DOMAIN, version=INTEGRATION_VERSION, issue_link=ISSUE_URL
)
)

# Check that all required files are present
file_check = await check_files(hass)
if not file_check:
Expand Down Expand Up @@ -90,9 +80,7 @@ async def async_unload_entry(
)
if unload_ok:
hass.data.pop(DOMAIN_DATA, None)
Logger("custom_components.healthchecksio").info(
"Successfully removed the healthchecksio integration"
)
LOGGER.info("Successfully removed the healthchecksio integration")
return unload_ok


Expand All @@ -111,7 +99,7 @@ def __init__(self, hass, api_key, check, self_hosted, site_root, ping_endpoint):
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update_data(self):
"""Update data."""
Logger("custom_components.healthchecksio").debug("Running update")
LOGGER.debug("Running update")
# This is where the main logic to update platform data goes.
try:
verify_ssl = not self.self_hosted or self.site_root.startswith("https")
Expand All @@ -129,10 +117,8 @@ async def update_data(self):
check_url = f"https://hc-ping.com/{self.check}"
await asyncio.sleep(1) # needed for self-hosted instances
await session.get(check_url)
except Exception as error: # pylint: disable=broad-except
Logger("custom_components.healthchecksio").error(
f"Could not update data - {error}"
)
except Exception: # pylint: disable=broad-except
LOGGER.exception("Could not update data")


async def check_files(hass: core.HomeAssistant) -> bool:
Expand All @@ -146,9 +132,7 @@ async def check_files(hass: core.HomeAssistant) -> bool:
missing.append(file)

if missing:
Logger("custom_components.healthchecksio").critical(
f"The following files are missing: {missing}"
)
LOGGER.critical("The following files are missing: %s", missing)
returnvalue = False
else:
returnvalue = True
Expand Down
12 changes: 7 additions & 5 deletions custom_components/healthchecksio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import asyncio
from collections import OrderedDict
from logging import getLogger

import async_timeout
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from integrationhelper import Logger

from .const import DOMAIN, DOMAIN_DATA, OFFICIAL_SITE_ROOT

LOGGER = getLogger(__name__)


@config_entries.HANDLERS.register(DOMAIN)
class BlueprintFlowHandler(config_entries.ConfigFlow):
Expand Down Expand Up @@ -126,18 +128,18 @@ async def _test_credentials(
session = async_get_clientsession(self.hass, verify_ssl)
headers = {"X-Api-Key": api_key}
async with async_timeout.timeout(10):
Logger("custom_components.healthchecksio").info("Checking API Key")
LOGGER.info("Checking API Key")
data = await session.get(f"{site_root}/api/v1/checks/", headers=headers)
self.hass.data[DOMAIN_DATA] = {"data": await data.json()}

Logger("custom_components.healthchecksio").info("Checking Check ID")
LOGGER.info("Checking Check ID")
if self_hosted:
check_url = f"{site_root}/{ping_endpoint}/{check}"
else:
check_url = f"https://hc-ping.com/{check}"
await asyncio.sleep(1) # needed for self-hosted instances
await session.get(check_url)
return True
except Exception as exception: # pylint: disable=broad-except
Logger("custom_components.healthchecksio").error(exception)
except Exception: # pylint: disable=broad-except
LOGGER.exception("Unknown error occurred")
return False
4 changes: 0 additions & 4 deletions custom_components/healthchecksio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
"name": "Healthchecks.io",
"documentation": "https://github.com/custom-components/healthchecksio",
"issue_tracker": "https://github.com/custom-components/healthchecksio/issues",
"dependencies": [],
"config_flow": true,
"codeowners": [
"@ludeeus"
],
"requirements": [
"integrationhelper"
],
"version": "0.0.0"
}