From e58ee6822611bf3c5b84a0759275f088b6f15cd7 Mon Sep 17 00:00:00 2001 From: Alex X Date: Wed, 25 Dec 2024 17:56:31 +0300 Subject: [PATCH] Update config entry for new HA versions #1516 --- custom_components/xiaomi_gateway3/__init__.py | 39 +++++++++---------- .../xiaomi_gateway3/config_flow.py | 12 +++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/custom_components/xiaomi_gateway3/__init__.py b/custom_components/xiaomi_gateway3/__init__.py index 9577157f..518393a6 100644 --- a/custom_components/xiaomi_gateway3/__init__.py +++ b/custom_components/xiaomi_gateway3/__init__.py @@ -72,54 +72,53 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: - if config_entry.data: - return await hass_utils.setup_cloud(hass, config_entry) +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + if entry.data: + return await hass_utils.setup_cloud(hass, entry) - await hass_utils.store_gateway_key(hass, config_entry) + await hass_utils.store_gateway_key(hass, entry) - await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - gw = MultiGateway(**config_entry.options) - handle_add_entities(hass, config_entry, gw) + gw = MultiGateway(**entry.options) + handle_add_entities(hass, entry, gw) gw.start() - hass.data[DOMAIN][config_entry.entry_id] = gw + hass.data[DOMAIN][entry.entry_id] = gw - if not config_entry.update_listeners: - config_entry.add_update_listener(async_update_options) + entry.async_on_unload(entry.add_update_listener(async_reload_entry)) async def hass_stop(event): await gw.stop() - config_entry.async_on_unload( + entry.async_on_unload( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hass_stop) ) return True -async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry): - await hass.config_entries.async_reload(config_entry.entry_id) - - -async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: - if config_entry.data: +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + if entry.data: return True # skip unload for cloud config entry # remove all stats entities if disable stats - hass_utils.remove_stats_entities(hass, config_entry) + hass_utils.remove_stats_entities(hass, entry) # important to remove entities before stop gateway - ok = await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS) + ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - gw: MultiGateway = hass.data[DOMAIN][config_entry.entry_id] + gw: MultiGateway = hass.data[DOMAIN][entry.entry_id] await gw.stop() gw.remove_all_devices() return ok +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry): + await hass.config_entries.async_reload(entry.entry_id) + + async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry): if config_entry.version == 1: hass_utils.migrate_legacy_devices_unique_id(hass) diff --git a/custom_components/xiaomi_gateway3/config_flow.py b/custom_components/xiaomi_gateway3/config_flow.py index e2de8583..e831b729 100644 --- a/custom_components/xiaomi_gateway3/config_flow.py +++ b/custom_components/xiaomi_gateway3/config_flow.py @@ -129,13 +129,15 @@ async def async_step_token(self, user_input: dict = None): @staticmethod @callback def async_get_options_flow(config_entry: ConfigEntry): - return CloudOptionsandler(config_entry) + return OptionsFlowHandler() -class CloudOptionsandler(OptionsFlow): - def __init__(self, config_entry: ConfigEntry): - self.config_entry = config_entry - self.cloud_devices = None +class OptionsFlowHandler(OptionsFlow): + cloud_devices = None + + @property + def config_entry(self): + return self.hass.config_entries.async_get_entry(self.handler) @property def cloud(self) -> MiCloud: