From db6f0827aa9b9f9c31dd49f8c2ff552a8c9f7093 Mon Sep 17 00:00:00 2001
From: Penny Wood <Swamp-Ig@users.noreply.github.com>
Date: Tue, 28 Mar 2023 14:47:45 +0800
Subject: [PATCH] Allow reloading iZone config entry (#89572)

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

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
---
 homeassistant/components/izone/__init__.py  | 28 +++++++++++++--------
 homeassistant/components/izone/climate.py   |  4 ++-
 homeassistant/components/izone/discovery.py | 17 +++++++------
 3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/homeassistant/components/izone/__init__.py b/homeassistant/components/izone/__init__.py
index 3f2565bd8f4..fd8d27ac422 100644
--- a/homeassistant/components/izone/__init__.py
+++ b/homeassistant/components/izone/__init__.py
@@ -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)
diff --git a/homeassistant/components/izone/climate.py b/homeassistant/components/izone/climate.py
index 3e19afcca26..e5a45dbc5e6 100644
--- a/homeassistant/components/izone/climate.py
+++ b/homeassistant/components/izone/climate.py
@@ -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(
diff --git a/homeassistant/components/izone/discovery.py b/homeassistant/components/izone/discovery.py
index eb6e7d4a190..a170ed30a74 100644
--- a/homeassistant/components/izone/discovery.py
+++ b/homeassistant/components/izone/discovery.py
@@ -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")
-- 
GitLab