From 6d0bfa1d1a9fda672f4a8d3f65d8c7e045bfd909 Mon Sep 17 00:00:00 2001 From: Ludeeus Date: Thu, 26 Aug 2021 16:41:41 +0000 Subject: [PATCH] Allow reloading the integration from the UI --- custom_components/hacs/__init__.py | 48 +++++++++++++++++--- custom_components/hacs/const.py | 2 + custom_components/hacs/operational/reload.py | 10 ---- custom_components/hacs/operational/remove.py | 35 -------------- custom_components/hacs/operational/setup.py | 17 ++----- custom_components/hacs/tasks/setup_sensor.py | 8 ++-- 6 files changed, 52 insertions(+), 68 deletions(-) delete mode 100644 custom_components/hacs/operational/reload.py delete mode 100644 custom_components/hacs/operational/remove.py diff --git a/custom_components/hacs/__init__.py b/custom_components/hacs/__init__.py index 57c4147bf4d..050932fb4ab 100644 --- a/custom_components/hacs/__init__.py +++ b/custom_components/hacs/__init__.py @@ -4,29 +4,65 @@ For more details about this integration, please refer to the documentation at https://hacs.xyz/ """ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + import voluptuous as vol +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant -from .const import DOMAIN +from .const import DOMAIN, PLATFORMS +from .enums import HacsDisabledReason from .helpers.functions.configuration_schema import hacs_config_combined -from .operational.remove import async_remove_entry as hacs_remove_entry from .operational.setup import async_setup as hacs_yaml_setup from .operational.setup import async_setup_entry as hacs_ui_setup +if TYPE_CHECKING: + from .base import HacsBase + CONFIG_SCHEMA = vol.Schema({DOMAIN: hacs_config_combined()}, extra=vol.ALLOW_EXTRA) -async def async_setup(hass, config): +async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool: """Set up this integration using yaml.""" return await hacs_yaml_setup(hass, config) -async def async_setup_entry(hass, config_entry): +async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Set up this integration using UI.""" + config_entry.add_update_listener(async_reload_entry) return await hacs_ui_setup(hass, config_entry) -async def async_remove_entry(hass, config_entry): +async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Handle removal of an entry.""" - return await hacs_remove_entry(hass, config_entry) + hacs: HacsBase = hass.data[DOMAIN] + + for task in hacs.recuring_tasks: + # Cancel all pending tasks + task() + + try: + if hass.data.get("frontend_panels", {}).get("hacs"): + hacs.log.info("Removing sidepanel") + hass.components.frontend.async_remove_panel("hacs") + except AttributeError: + pass + + unload_ok = await hass.config_entries.async_unload_platforms( + config_entry, PLATFORMS + ) + + hacs.disable_hacs(HacsDisabledReason.REMOVED) + hass.data.pop(DOMAIN, None) + + return unload_ok + + +async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None: + """Reload the HACS config entry.""" + await async_unload_entry(hass, config_entry) + await async_setup_entry(hass, config_entry) diff --git a/custom_components/hacs/const.py b/custom_components/hacs/const.py index 5b69e901651..f4d124d9693 100644 --- a/custom_components/hacs/const.py +++ b/custom_components/hacs/const.py @@ -14,6 +14,8 @@ PACKAGE_NAME = "custom_components.hacs" +PLATFORMS = ["sensor"] + HACS_ACTION_GITHUB_API_HEADERS = { "User-Agent": "HACS/action", "Accept": ACCEPT_HEADERS["preview"], diff --git a/custom_components/hacs/operational/reload.py b/custom_components/hacs/operational/reload.py deleted file mode 100644 index 037887e6857..00000000000 --- a/custom_components/hacs/operational/reload.py +++ /dev/null @@ -1,10 +0,0 @@ -"""Reload HACS""" - - -async def async_reload_entry(hass, config_entry): - """Reload HACS.""" - from custom_components.hacs.operational.remove import async_remove_entry - from custom_components.hacs.operational.setup import async_setup_entry - - await async_remove_entry(hass, config_entry) - await async_setup_entry(hass, config_entry) diff --git a/custom_components/hacs/operational/remove.py b/custom_components/hacs/operational/remove.py deleted file mode 100644 index 2b868ac26de..00000000000 --- a/custom_components/hacs/operational/remove.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Remove HACS.""" -from typing import TYPE_CHECKING - -from ..const import DOMAIN -from ..enums import HacsDisabledReason -from ..share import get_hacs - -if TYPE_CHECKING: - from homeassistant.config_entries import ConfigEntry - from homeassistant.core import HomeAssistant - - -async def async_remove_entry(hass: "HomeAssistant", config_entry: "ConfigEntry"): - """Handle removal of an entry.""" - hacs = get_hacs() - hacs.log.info("Disabling HACS") - hacs.log.info("Removing recurring tasks") - for task in hacs.recuring_tasks: - task() - - if str(config_entry.state) in ["ConfigEntryState.LOADED", "loaded"]: - hacs.log.info("Removing sensor") - try: - await hass.config_entries.async_forward_entry_unload(config_entry, "sensor") - except ValueError: - pass - try: - if "hacs" in hass.data.get("frontend_panels", {}): - hacs.log.info("Removing sidepanel") - hass.components.frontend.async_remove_panel("hacs") - except AttributeError: - pass - if DOMAIN in hass.data: - del hass.data[DOMAIN] - hacs.disable_hacs(HacsDisabledReason.REMOVED) diff --git a/custom_components/hacs/operational/setup.py b/custom_components/hacs/operational/setup.py index 375487612c1..2d9e3822422 100644 --- a/custom_components/hacs/operational/setup.py +++ b/custom_components/hacs/operational/setup.py @@ -1,25 +1,18 @@ """Setup HACS.""" from aiogithubapi import AIOGitHubAPIException, GitHub, GitHubAPI from aiogithubapi.const import ACCEPT_HEADERS -from homeassistant.config_entries import SOURCE_IMPORT +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import EVENT_HOMEASSISTANT_STARTED from homeassistant.const import __version__ as HAVERSION -from homeassistant.core import CoreState +from homeassistant.core import CoreState, HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.event import async_call_later from homeassistant.loader import async_get_integration from custom_components.hacs.const import DOMAIN, STARTUP -from custom_components.hacs.enums import ( - ConfigurationType, - HacsDisabledReason, - HacsStage, - LovelaceMode, -) +from custom_components.hacs.enums import ConfigurationType, HacsStage, LovelaceMode from custom_components.hacs.hacsbase.data import HacsData -from custom_components.hacs.operational.reload import async_reload_entry -from custom_components.hacs.operational.remove import async_remove_entry from custom_components.hacs.share import get_hacs from custom_components.hacs.tasks.manager import HacsTaskManager @@ -85,7 +78,7 @@ async def _async_common_setup(hass): hass.data[DOMAIN] = hacs -async def async_setup_entry(hass, config_entry): +async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Set up this integration using UI.""" hacs = get_hacs() @@ -132,7 +125,7 @@ async def async_setup(hass, config): async def async_startup_wrapper_for_config_entry(): """Startup wrapper for ui config.""" hacs = get_hacs() - hacs.configuration.config_entry.add_update_listener(async_reload_entry) + try: startup_result = await async_hacs_startup() except AIOGitHubAPIException: diff --git a/custom_components/hacs/tasks/setup_sensor.py b/custom_components/hacs/tasks/setup_sensor.py index b7e27380783..24c468cdd76 100644 --- a/custom_components/hacs/tasks/setup_sensor.py +++ b/custom_components/hacs/tasks/setup_sensor.py @@ -2,7 +2,7 @@ from homeassistant.helpers.discovery import async_load_platform -from ..const import DOMAIN +from ..const import DOMAIN, PLATFORMS from ..enums import ConfigurationType, HacsStage from .base import HacsTaskRuntimeBase @@ -25,8 +25,6 @@ async def async_execute(self) -> None: ) ) else: - self.hass.async_add_job( - self.hass.config_entries.async_forward_entry_setup( - self.hacs.configuration.config_entry, "sensor" - ) + self.hass.config_entries.async_setup_platforms( + self.hacs.configuration.config_entry, PLATFORMS )