Skip to content

Commit

Permalink
Delay OperationState events published to the HA bus to improve handli…
Browse files Browse the repository at this point in the history
…ng of out of order events
  • Loading branch information
ekutner committed Jul 2, 2024
1 parent c18f4c7 commit 4e449a3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions custom_components/home_connect_alt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,21 @@ async def async_handle_event(appliance:Appliance, key:str, value:str):
"key": key,
"value": value
}
hass.bus.async_fire(f"{DOMAIN}_event", event_data)
_LOGGER.debug("Published event to Home Assistant event bus: %s = %s", key, str(value))
if key in ("BSH.Common.Status.OperationState", "BSH.Common.Event.ProgramFinished"):
# delay the firing of the event to deal with event handling race condition
_LOGGER.debug("Creating delayed_publish_event task")
asyncio.create_task(delayed_publish_event(event_data, 1))
else:
hass.bus.async_fire(f"{DOMAIN}_event", event_data)
_LOGGER.debug("Published event to Home Assistant event bus: %s = %s", key, str(value))
else:
_LOGGER.debug("Skipped publishing of duplicate event to Home Assistant event bus: %s = %s", key, str(value))

async def delayed_publish_event(event_data, timeout):
_LOGGER.debug(f"Sleeping for {timeout} seconds to delay event publishing of {event_data}")
await asyncio.sleep(timeout)
hass.bus.async_fire(f"{DOMAIN}_event", event_data)
_LOGGER.debug("Published event to Home Assistant event bus after delay: %s = %s", event_data["key"], str(event_data["value"]))

def register_appliance(appliance:Appliance):
for event in PUBLISHED_EVENTS:
Expand Down

0 comments on commit 4e449a3

Please sign in to comment.