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

Allow reloading the integration from the UI #2172

Merged
merged 1 commit into from
Aug 26, 2021
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
48 changes: 42 additions & 6 deletions custom_components/hacs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions custom_components/hacs/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

PACKAGE_NAME = "custom_components.hacs"

PLATFORMS = ["sensor"]

HACS_ACTION_GITHUB_API_HEADERS = {
"User-Agent": "HACS/action",
"Accept": ACCEPT_HEADERS["preview"],
Expand Down
10 changes: 0 additions & 10 deletions custom_components/hacs/operational/reload.py

This file was deleted.

35 changes: 0 additions & 35 deletions custom_components/hacs/operational/remove.py

This file was deleted.

17 changes: 5 additions & 12 deletions custom_components/hacs/operational/setup.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions custom_components/hacs/tasks/setup_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
)