From b39be4b1b787ebb418ddf4399ab4835ba8d2d95b Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Mon, 9 Oct 2023 10:47:01 -0400 Subject: [PATCH] Implement unload to allow reloading integration (#42) --- custom_components/healthchecksio/__init__.py | 57 ++++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/custom_components/healthchecksio/__init__.py b/custom_components/healthchecksio/__init__.py index 2a4400f..08f3738 100644 --- a/custom_components/healthchecksio/__init__.py +++ b/custom_components/healthchecksio/__init__.py @@ -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( @@ -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( @@ -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.""" @@ -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}/" @@ -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" - )