Skip to content

Commit

Permalink
Уменьшена зависимость от интеграции Yandex Station при отправке событий
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Apr 27, 2024
1 parent 857180f commit 4f27072
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
20 changes: 15 additions & 5 deletions custom_components/yandex_station_intents/yandex_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,28 @@ def event_from_id(self, intent_id: int) -> None:
_LOGGER.debug(f"Получена команда: {text}")
self._hass.bus.async_fire(EVENT_NAME, {"text": text, "account": self._entry.unique_id})

async def async_handle_phrase(self, phrase: str, event_data: ConfigType, yandex_station_entity_id: str) -> None:
async def async_handle_phrase(
self, phrase: str, event_data: ConfigType, yandex_station_entity_id: str | None
) -> None:
intent = self._intent_from_phrase(phrase)
if intent:
event_data.update({"text": intent.name, "account": self._entry.unique_id})
_LOGGER.debug(f"Получена команда: {event_data!r}")
self._hass.bus.async_fire(EVENT_NAME, event_data)

if intent.execute_command:
await self._execute_command(intent, event_data, yandex_station_entity_id)
if yandex_station_entity_id:
if intent.execute_command:
await self._execute_command(intent, event_data, yandex_station_entity_id)

if intent.say_phrase_template:
await self._tts(intent, event_data, yandex_station_entity_id)
if intent.say_phrase_template:
await self._tts(intent, event_data, yandex_station_entity_id)
else:
_LOGGER.warning(
f"В Home Assistant не найдена колонка для события {phrase!r}. "
f"Интеграция Yandex.Station установлена и настроена?"
)
else:
_LOGGER.warning(f"Не найден интент для события {phrase}")

async def _execute_command(self, intent: Intent, event_data: ConfigType, yandex_station_entity_id: str) -> None:
assert intent.execute_command
Expand Down
23 changes: 10 additions & 13 deletions custom_components/yandex_station_intents/yandex_quasar.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,20 @@ async def _on_message(self, payload: dict[Any, Any]) -> None:
if cap_state["instance"] in ["text_action", "phrase_action"] and INTENT_ID_MARKER in cap_state["value"]:
_LOGGER.debug(f"Интент обнаружен в событии: {dev!r}")

yandex_station_entity_id: str | None = None
event_data: ConfigType = {}

for device in self._quasar.devices:
if device.id != dev["id"] or not device.yandex_station_id:
continue

phrase = cap_state["value"]
entity_id = self._entity_registry.async_get_entity_id(
media_player.DOMAIN, YANDEX_STATION_DOMAIN, device.yandex_station_id
)
if not entity_id:
_LOGGER.warning(
f"Не найдена колонка {device.yandex_station_id} для события {phrase!r}. "
f"Интеграция Yandex.Station установлена и настроена?"
)
continue

event_data = {ATTR_ENTITY_ID: entity_id}
if device.room:
event_data["room"] = device.room

await self._manager.async_handle_phrase(phrase, event_data, entity_id)
yandex_station_entity_id = self._entity_registry.async_get_entity_id(
media_player.DOMAIN, YANDEX_STATION_DOMAIN, device.yandex_station_id
)
if yandex_station_entity_id:
event_data[ATTR_ENTITY_ID] = yandex_station_entity_id

await self._manager.async_handle_phrase(cap_state["value"], event_data, yandex_station_entity_id)

0 comments on commit 4f27072

Please sign in to comment.