Skip to content

Commit

Permalink
Fix 2024.3 issue (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatziko authored Mar 10, 2024
1 parent 5060bcc commit 186ace4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 85 deletions.
6 changes: 3 additions & 3 deletions custom_components/ultrasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'])
Expand Down
157 changes: 76 additions & 81 deletions custom_components/ultrasync/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion custom_components/ultrasync/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 186ace4

Please sign in to comment.