Skip to content

Commit

Permalink
Implement unload to allow reloading integration (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 authored Oct 9, 2023
1 parent 955aa16 commit b39be4b
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions custom_components/healthchecksio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@
For more details about this component, please refer to
https://github.com/custom-components/healthchecksio
"""
import os
import async_timeout
import asyncio
import os
from datetime import timedelta

import async_timeout
from homeassistant import config_entries, core
from homeassistant.const import Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import voluptuous as vol
from homeassistant import config_entries
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle

from integrationhelper.const import CC_STARTUP_VERSION
from integrationhelper import Logger
from integrationhelper.const import CC_STARTUP_VERSION

from .const import (
DOMAIN_DATA,
DOMAIN,
ISSUE_URL,
REQUIRED_FILES,
DOMAIN_DATA,
INTEGRATION_VERSION,
ISSUE_URL,
OFFICIAL_SITE_ROOT,
REQUIRED_FILES,
)

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)


async def async_setup(hass, config):
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(
Expand All @@ -40,7 +39,9 @@ async def async_setup(hass, config):
return True


async def async_setup_entry(hass, config_entry):
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(
Expand Down Expand Up @@ -74,12 +75,30 @@ async def async_setup_entry(hass, config_entry):

# Add binary_sensor
hass.async_add_job(
hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
hass.config_entries.async_forward_entry_setup(
config_entry, Platform.BINARY_SENSOR
)
)

return True


async def async_unload_entry(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
) -> bool:
"""Unload a config entry."""

unload_ok = await hass.config_entries.async_forward_entry_unload(
config_entry, Platform.BINARY_SENSOR
)
if unload_ok:
hass.data.pop(DOMAIN_DATA, None)
Logger("custom_components.healthchecksio").info(
"Successfully removed the healthchecksio integration"
)
return unload_ok


class HealthchecksioData:
"""This class handle communication and stores the data."""

Expand Down Expand Up @@ -119,7 +138,7 @@ async def update_data(self):
)


async def check_files(hass):
async def check_files(hass: core.HomeAssistant) -> bool:
"""Return bool that indicates if all files are present."""
# Verify that the user downloaded all files.
base = f"{hass.config.path()}/custom_components/{DOMAIN}/"
Expand All @@ -138,11 +157,3 @@ async def check_files(hass):
returnvalue = True

return returnvalue


async def async_remove_entry(hass, config_entry):
"""Handle removal of an entry."""
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
Logger("custom_components.healthchecksio").info(
"Successfully removed the healthchecksio integration"
)

0 comments on commit b39be4b

Please sign in to comment.