From 186ace4959d87f70db45c08795faa41bb07d91ff Mon Sep 17 00:00:00 2001 From: Kostas Chatzikokolakis Date: Sun, 10 Mar 2024 17:35:03 +0200 Subject: [PATCH] Fix 2024.3 issue (#34) --- custom_components/ultrasync/__init__.py | 6 +- custom_components/ultrasync/coordinator.py | 157 ++++++++++----------- custom_components/ultrasync/sensor.py | 2 +- 3 files changed, 80 insertions(+), 85 deletions(-) diff --git a/custom_components/ultrasync/__init__.py b/custom_components/ultrasync/__init__.py index d591387..e9b3ee2 100644 --- a/custom_components/ultrasync/__init__.py +++ b/custom_components/ultrasync/__init__.py @@ -65,8 +65,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool } for component in PLATFORMS: - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, component) + entry.async_create_task( + hass, hass.config_entries.async_forward_entry_setup(entry, component) ) _async_register_services(hass, coordinator) @@ -118,7 +118,7 @@ def bypass(call) -> None: def unbypass(call) -> None: """Service call to unbypass a zone in UltraSync Hub.""" coordinator.hub.set_zone_bypass(state=False, zone=call.data['zone']) - + def switch(call) -> None: """Service call to switch on/off an output control in UltraSync Hub.""" coordinator.hub.set_output_control(output=call.data['output'], state=call.data['state']) diff --git a/custom_components/ultrasync/coordinator.py b/custom_components/ultrasync/coordinator.py index a70c2e3..425cac2 100644 --- a/custom_components/ultrasync/coordinator.py +++ b/custom_components/ultrasync/coordinator.py @@ -44,87 +44,82 @@ def __init__(self, hass: HomeAssistantType, *, config: dict, options: dict): async def _async_update_data(self) -> dict: """Fetch data from UltraSync Hub.""" - def _update_data() -> dict: - """Fetch data from UltraSync via sync functions.""" - - # initialize our response - response = {} - - # Update our details - details = self.hub.details(max_age_sec=0) - if details: - async_dispatcher_send( - self.hass, - SENSOR_UPDATE_LISTENER, - details["areas"], - details["zones"], - details["outputs"] - ) - - for zone in details["zones"]: - if self._zone_delta.get(zone["bank"]) != zone["sequence"]: - self.hass.bus.fire( - "ultrasync_zone_update", - { - "sensor": zone["bank"] + 1, - "name": zone["name"], - "status": zone["status"], - }, - ) - - # Update our sequence - self._zone_delta[zone["bank"]] = zone["sequence"] - - # Set our state: - response["zone{:0>2}_state".format(zone["bank"] + 1)] = zone[ - "status" - ] - - for area in details["areas"]: - if self._area_delta.get(area["bank"]) != area["sequence"]: - self.hass.bus.fire( - "ultrasync_area_update", - { - "area": area["bank"] + 1, - "name": area["name"], - "status": area["status"], - }, - ) - - # Update our sequence - self._area_delta[area["bank"]] = area["sequence"] - - # Set our state: - response["area{:0>2}_state".format(area["bank"] + 1)] = area[ - "status" - ] - - output_index = 1 - for output in details["outputs"]: - if self._output_delta.get(output["name"]) != output["state"]: - self.hass.bus.fire( - "ultrasync_output_update", - { - "name": output["name"], - "status": output["state"], - }, - ) - - # Update our sequence - self._output_delta[output["name"]] = output["state"] - - # Set our state: - response["output{}state".format(output_index)] = output[ - "state" - ] - output_index += 1 - - self._init = True - - # Return our response - return response + # initialize our response + response = {} # The hub can sometimes take a very long time to respond; wait - # 10 seconds before giving up async with timeout(10): - return await self.hass.async_add_executor_job(_update_data) + details = await self.hass.async_add_executor_job(lambda: self.hub.details(max_age_sec=0)) + + # Update our details + if details: + async_dispatcher_send( + self.hass, + SENSOR_UPDATE_LISTENER, + details["areas"], + details["zones"], + details["outputs"] + ) + + for zone in details["zones"]: + if self._zone_delta.get(zone["bank"]) != zone["sequence"]: + self.hass.bus.fire( + "ultrasync_zone_update", + { + "sensor": zone["bank"] + 1, + "name": zone["name"], + "status": zone["status"], + }, + ) + + # Update our sequence + self._zone_delta[zone["bank"]] = zone["sequence"] + + # Set our state: + response["zone{:0>2}_state".format(zone["bank"] + 1)] = zone[ + "status" + ] + + for area in details["areas"]: + if self._area_delta.get(area["bank"]) != area["sequence"]: + self.hass.bus.fire( + "ultrasync_area_update", + { + "area": area["bank"] + 1, + "name": area["name"], + "status": area["status"], + }, + ) + + # Update our sequence + self._area_delta[area["bank"]] = area["sequence"] + + # Set our state: + response["area{:0>2}_state".format(area["bank"] + 1)] = area[ + "status" + ] + + output_index = 1 + for output in details["outputs"]: + if self._output_delta.get(output["name"]) != output["state"]: + self.hass.bus.fire( + "ultrasync_output_update", + { + "name": output["name"], + "status": output["state"], + }, + ) + + # Update our sequence + self._output_delta[output["name"]] = output["state"] + + # Set our state: + response["output{}state".format(output_index)] = output[ + "state" + ] + output_index += 1 + + self._init = True + + # Return our response + return response diff --git a/custom_components/ultrasync/sensor.py b/custom_components/ultrasync/sensor.py index fe4e973..5fd9e56 100644 --- a/custom_components/ultrasync/sensor.py +++ b/custom_components/ultrasync/sensor.py @@ -145,7 +145,7 @@ def _auto_manage_sensors(areas: dict, zones: dict, outputs: dict) -> None: for sensor_id in set(sensors.keys()).difference(detected_sensors): # Tidy up sensors leaving our listing - hass.async_create_task(sensors[sensor_id].async_remove()) + entry.async_create_task(hass, sensors[sensor_id].async_remove()) del sensors[sensor_id] # register our callback which will be called the second we make a