Skip to content

Commit

Permalink
fix(yandex_station): adapt intent listener to handle mapping in event…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
skrashevich committed Mar 22, 2024
1 parent 1293f88 commit 870289c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions custom_components/yandex_station/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 870289c

Please sign in to comment.