mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add screenlogic reconnect (#52022)
Co-authored-by: Kevin Worrel <37058192+dieselrabbit@users.noreply.github.com>
This commit is contained in:
parent
72b55d1830
commit
f8eb07444b
@ -40,25 +40,8 @@ async def async_setup(hass: HomeAssistant, config: dict):
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Screenlogic from a config entry."""
|
"""Set up Screenlogic from a config entry."""
|
||||||
mac = entry.unique_id
|
|
||||||
# Attempt to re-discover named gateway to follow IP changes
|
|
||||||
discovered_gateways = hass.data[DOMAIN][DISCOVERED_GATEWAYS]
|
|
||||||
if mac in discovered_gateways:
|
|
||||||
connect_info = discovered_gateways[mac]
|
|
||||||
else:
|
|
||||||
_LOGGER.warning("Gateway rediscovery failed")
|
|
||||||
# Static connection defined or fallback from discovery
|
|
||||||
connect_info = {
|
|
||||||
SL_GATEWAY_NAME: name_for_mac(mac),
|
|
||||||
SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
|
|
||||||
SL_GATEWAY_PORT: entry.data[CONF_PORT],
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
gateway = await hass.async_add_executor_job(get_new_gateway, hass, entry)
|
||||||
gateway = ScreenLogicGateway(**connect_info)
|
|
||||||
except ScreenLogicError as ex:
|
|
||||||
_LOGGER.error("Error while connecting to the gateway %s: %s", connect_info, ex)
|
|
||||||
raise ConfigEntryNotReady from ex
|
|
||||||
|
|
||||||
# The api library uses a shared socket connection and does not handle concurrent
|
# The api library uses a shared socket connection and does not handle concurrent
|
||||||
# requests very well.
|
# requests very well.
|
||||||
@ -99,6 +82,39 @@ async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
|
def get_connect_info(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
|
"""Construct connect_info from configuration entry and returns it to caller."""
|
||||||
|
mac = entry.unique_id
|
||||||
|
# Attempt to re-discover named gateway to follow IP changes
|
||||||
|
discovered_gateways = hass.data[DOMAIN][DISCOVERED_GATEWAYS]
|
||||||
|
if mac in discovered_gateways:
|
||||||
|
connect_info = discovered_gateways[mac]
|
||||||
|
else:
|
||||||
|
_LOGGER.warning("Gateway rediscovery failed")
|
||||||
|
# Static connection defined or fallback from discovery
|
||||||
|
connect_info = {
|
||||||
|
SL_GATEWAY_NAME: name_for_mac(mac),
|
||||||
|
SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
|
||||||
|
SL_GATEWAY_PORT: entry.data[CONF_PORT],
|
||||||
|
}
|
||||||
|
|
||||||
|
return connect_info
|
||||||
|
|
||||||
|
|
||||||
|
def get_new_gateway(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
|
"""Instantiate a new ScreenLogicGateway, connect to it and return it to caller."""
|
||||||
|
|
||||||
|
connect_info = get_connect_info(hass, entry)
|
||||||
|
|
||||||
|
try:
|
||||||
|
gateway = ScreenLogicGateway(**connect_info)
|
||||||
|
except ScreenLogicError as ex:
|
||||||
|
_LOGGER.error("Error while connecting to the gateway %s: %s", connect_info, ex)
|
||||||
|
raise ConfigEntryNotReady from ex
|
||||||
|
|
||||||
|
return gateway
|
||||||
|
|
||||||
|
|
||||||
class ScreenlogicDataUpdateCoordinator(DataUpdateCoordinator):
|
class ScreenlogicDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
"""Class to manage the data update for the Screenlogic component."""
|
"""Class to manage the data update for the Screenlogic component."""
|
||||||
|
|
||||||
@ -119,13 +135,32 @@ class ScreenlogicDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
update_interval=interval,
|
update_interval=interval,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def reconnect_gateway(self):
|
||||||
|
"""Instantiate a new ScreenLogicGateway, connect to it and update. Return new gateway to caller."""
|
||||||
|
|
||||||
|
connect_info = get_connect_info(self.hass, self.config_entry)
|
||||||
|
|
||||||
|
try:
|
||||||
|
gateway = ScreenLogicGateway(**connect_info)
|
||||||
|
gateway.update()
|
||||||
|
except ScreenLogicError as error:
|
||||||
|
raise UpdateFailed(error) from error
|
||||||
|
|
||||||
|
return gateway
|
||||||
|
|
||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Fetch data from the Screenlogic gateway."""
|
"""Fetch data from the Screenlogic gateway."""
|
||||||
try:
|
try:
|
||||||
async with self.api_lock:
|
async with self.api_lock:
|
||||||
await self.hass.async_add_executor_job(self.gateway.update)
|
await self.hass.async_add_executor_job(self.gateway.update)
|
||||||
except ScreenLogicError as error:
|
except ScreenLogicError as error:
|
||||||
raise UpdateFailed(error) from error
|
_LOGGER.warning("ScreenLogicError - attempting reconnect: %s", error)
|
||||||
|
|
||||||
|
async with self.api_lock:
|
||||||
|
self.gateway = await self.hass.async_add_executor_job(
|
||||||
|
self.reconnect_gateway
|
||||||
|
)
|
||||||
|
|
||||||
return self.gateway.get_data()
|
return self.gateway.get_data()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user