From 8ee00287bd1b969ccb6850331799b2e2110fd5b6 Mon Sep 17 00:00:00 2001 From: nao-pon Date: Thu, 19 Oct 2023 01:36:51 +0900 Subject: [PATCH] Supports when IP address change is detected during setup (rel #144) --- custom_components/echonetlite/__init__.py | 10 ++++++++-- custom_components/echonetlite/config_flow.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/custom_components/echonetlite/__init__.py b/custom_components/echonetlite/__init__.py index 76ccc68..70bef17 100644 --- a/custom_components/echonetlite/__init__.py +++ b/custom_components/echonetlite/__init__.py @@ -241,14 +241,20 @@ def unload_config_entry(): echonetlite = ECHONETConnector(instance, hass.data[DOMAIN]["api"], entry) try: - await asyncio.wait_for(echonetlite.async_update(), timeout=20) + await asyncio.wait_for( + echonetlite.async_update(), timeout=60 + ) # 20 secs * retry 3 times = 60 hass.data[DOMAIN][entry.entry_id].append( {"instance": instance, "echonetlite": echonetlite} ) - except asyncio.exceptions.TimeoutError as ex: + except (asyncio.TimeoutError, asyncio.CancelledError) as ex: raise ConfigEntryNotReady( f"Connection error while connecting to {host}: {ex}" ) from ex + except KeyError as ex: + raise ConfigEntryNotReady( + f"IP address change was detected during setup of {host}" + ) from ex _LOGGER.debug(f"Plaform entry data - {entry.data}") diff --git a/custom_components/echonetlite/config_flow.py b/custom_components/echonetlite/config_flow.py index 6922bb7..7ae44a8 100644 --- a/custom_components/echonetlite/config_flow.py +++ b/custom_components/echonetlite/config_flow.py @@ -134,7 +134,7 @@ async def validate_input( _LOGGER.info(f"ECHONET registed node IP hanged from {old_host} to {host}.") _LOGGER.info(f"New instances data is {instances}") if server._state.get(old_host): - server._state.pop(old_host) + server._state[host] = server._state.pop(old_host) hass.config_entries.async_update_entry( config_entry, data={"instances": instances} )