From 870289ce60c7e8765334c7a7a0050c827ef69793 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Fri, 22 Mar 2024 13:21:11 +0300 Subject: [PATCH] fix(yandex_station): adapt intent listener to handle mapping in event data This commit updates the `listener` function within the Yandex Station integration to support a change introduced in version 2024.4.0, where event data can be passed directly as a `Mapping[str, Any]` instead of an `Event` object. The update involves checking the type of the `event_data` parameter and extracting the data from it if it's an instance of `Event`. This change ensures compatibility with Home Assistant's evolving event model while maintaining performance by using a direct type check rather than `isinstance`, which is slower. The adjustment is critical for ensuring that the Yandex Station integration continues to function correctly without breaking due to the structural changes in how events are passed to listeners in Home Assistant versions 2024.4.0 and onwards. --- custom_components/yandex_station/intent.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/custom_components/yandex_station/intent.py b/custom_components/yandex_station/intent.py index 0b4845a..5d4ae6c 100644 --- a/custom_components/yandex_station/intent.py +++ b/custom_components/yandex_station/intent.py @@ -2,8 +2,10 @@ import logging from typing import Optional +from collections.abc import Mapping from homeassistant.core import Event, HomeAssistant from homeassistant.helpers.intent import Intent, IntentHandler, IntentResponse +from typing import Any from .core.const import DATA_SPEAKERS, DOMAIN @@ -28,11 +30,19 @@ async def async_setup_intents(hass: HomeAssistant) -> None: if not handlers: return - async def listener(event: Event): - request_id = event.data["request_id"] + async def listener(event_data: Mapping[str, Any] | Event): + + # Breaking change in 2024.4.0, check for Event for versions prior to this + if ( + type(event_data) is Event + ): # Intentionally avoid `isinstance` because it's slow and we trust `Event` is not subclassed + event_data = event_data.data + + request_id = event_data["request_id"] + for handler in handlers: if handler.request_id == request_id: - handler.response_text = event.data["text"] + handler.response_text = event_data["text"] handler.response_waiter.set() return