Allow reloading iZone config entry (#89572)

* Allow reloading of iZone config entries
---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/90395/head
Penny Wood 2023-03-28 14:47:45 +08:00 committed by GitHub
parent 8807878529
commit db6f0827aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 20 deletions

View File

@ -3,7 +3,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EXCLUDE, Platform
from homeassistant.const import CONF_EXCLUDE, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
@ -29,29 +29,35 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Register the iZone component config."""
if not (conf := config.get(IZONE)):
return True
hass.data[DATA_CONFIG] = conf
# Check for manually added config, this may exclude some devices
if conf := config.get(IZONE):
hass.data[DATA_CONFIG] = conf
# Explicitly added in the config file, create a config entry.
hass.async_create_task(
hass.config_entries.flow.async_init(
IZONE, context={"source": config_entries.SOURCE_IMPORT}
# Explicitly added in the config file, create a config entry.
hass.async_create_task(
hass.config_entries.flow.async_init(
IZONE, context={"source": config_entries.SOURCE_IMPORT}
)
)
)
# Start the discovery service
await async_start_discovery_service(hass)
async def shutdown_event(event):
await async_stop_discovery_service(hass)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry."""
await async_start_discovery_service(hass)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload the config entry and stop discovery process."""
await async_stop_discovery_service(hass)
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -95,7 +95,9 @@ async def async_setup_entry(
init_controller(controller)
# connect to register any further components
async_dispatcher_connect(hass, DISPATCH_CONTROLLER_DISCOVERED, init_controller)
config.async_on_unload(
async_dispatcher_connect(hass, DISPATCH_CONTROLLER_DISCOVERED, init_controller)
)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(

View File

@ -1,7 +1,8 @@
"""Internal discovery service for iZone AC."""
import logging
import pizone
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -15,15 +16,17 @@ from .const import (
DISPATCH_ZONE_UPDATE,
)
_LOGGER = logging.getLogger(__name__)
class DiscoveryService(pizone.Listener):
"""Discovery data and interfacing with pizone library."""
def __init__(self, hass):
def __init__(self, hass: HomeAssistant) -> None:
"""Initialise discovery service."""
super().__init__()
self.hass = hass
self.pi_disco = None
self.pi_disco: pizone.DiscoveryService | None = None
# Listener interface
def controller_discovered(self, ctrl: pizone.Controller) -> None:
@ -52,6 +55,7 @@ async def async_start_discovery_service(hass: HomeAssistant):
if disco := hass.data.get(DATA_DISCOVERY_SERVICE):
# Already started
return disco
_LOGGER.debug("Starting iZone Discovery Service")
# discovery local services
disco = DiscoveryService(hass)
@ -62,11 +66,6 @@ async def async_start_discovery_service(hass: HomeAssistant):
disco.pi_disco = pizone.discovery(disco, session=session)
await disco.pi_disco.start_discovery()
async def shutdown_event(event):
await async_stop_discovery_service(hass)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)
return disco
@ -77,3 +76,5 @@ async def async_stop_discovery_service(hass: HomeAssistant):
await disco.pi_disco.close()
del hass.data[DATA_DISCOVERY_SERVICE]
_LOGGER.debug("Stopped iZone Discovery Service")