From 4e449a3c7e2c07abf179df408dbf44da36a5ab44 Mon Sep 17 00:00:00 2001 From: Eran Kutner <5628151+ekutner@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:58:15 +0300 Subject: [PATCH] Delay OperationState events published to the HA bus to improve handling of out of order events --- custom_components/home_connect_alt/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/custom_components/home_connect_alt/__init__.py b/custom_components/home_connect_alt/__init__.py index 034fe2b..b549b83 100644 --- a/custom_components/home_connect_alt/__init__.py +++ b/custom_components/home_connect_alt/__init__.py @@ -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: