diff --git a/custom_components/yandex_station/core/utils.py b/custom_components/yandex_station/core/utils.py index 75abc0e..e15bc02 100644 --- a/custom_components/yandex_station/core/utils.py +++ b/custom_components/yandex_station/core/utils.py @@ -22,7 +22,6 @@ TrackTemplateResult, ) from homeassistant.helpers.template import Template -from homeassistant.helpers.typing import HomeAssistantType from yarl import URL from .const import CONF_MEDIA_PLAYERS, DATA_CONFIG, DOMAIN @@ -52,7 +51,7 @@ class YandexDebug(logging.Handler, HomeAssistantView): text = "" - def __init__(self, hass: HomeAssistantType, logger: Logger): + def __init__(self, hass: HomeAssistant, logger: Logger): super().__init__() logger.addHandler(self) @@ -119,7 +118,7 @@ def find_station(devices, name: str = None): return None -async def error(hass: HomeAssistantType, text: str): +async def error(hass: HomeAssistant, text: str): _LOGGER.error(text) hass.components.persistent_notification.async_create( text, title="YandexStation ERROR" @@ -137,7 +136,7 @@ def clean_v1(hass_dir): os.remove(path) -async def has_custom_icons(hass: HomeAssistantType): +async def has_custom_icons(hass: HomeAssistant): # GUI off mode if "lovelace" not in hass.data: return False @@ -229,7 +228,7 @@ async def get_media_payload(text: str, session): return None -async def get_zeroconf_singleton(hass: HomeAssistantType): +async def get_zeroconf_singleton(hass: HomeAssistant): try: # Home Assistant 0.110.0 and above from homeassistant.components.zeroconf import async_get_instance @@ -278,7 +277,7 @@ async def get_tts_message(session: ClientSession, url: str): # noinspection PyProtectedMember -def fix_recognition_lang(hass: HomeAssistantType, folder: str, lng: str): +def fix_recognition_lang(hass: HomeAssistant, folder: str, lng: str): path = frontend._frontend_root(None).joinpath(folder) for child in path.iterdir(): # find all chunc.xxxx.js files diff --git a/custom_components/yandex_station/intent.py b/custom_components/yandex_station/intent.py deleted file mode 100644 index b5f9123..0000000 --- a/custom_components/yandex_station/intent.py +++ /dev/null @@ -1,78 +0,0 @@ -import asyncio -import logging - -from homeassistant.core import Event, HomeAssistant -from homeassistant.helpers.intent import Intent, IntentHandler, IntentResponse - -from .core.const import DATA_SPEAKERS, DOMAIN - -_LOGGER = logging.getLogger(__name__) - - -async def async_setup_intents(hass: HomeAssistant) -> None: - if DOMAIN not in hass.data: - return - - handlers = [] - - for device in hass.data[DOMAIN][DATA_SPEAKERS].values(): - if "quasar_info" not in device: - continue - platform = device["quasar_info"]["platform"] - if ("host" in device or platform.startswith("yandex")) and "entity" in device: - handler = YandexIntentHandler(device["entity"].entity_id) - hass.helpers.intent.async_register(handler) - handlers.append(handler) - - if not handlers: - return - - async def listener(event_data: dict | Event): - # Breaking change in 2024.4.0, check for Event for versions prior to this - # Intentionally avoid `isinstance` because it's slow and we trust `Event` is not subclassed - if type(event_data) is Event: - 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_waiter.set() - return - - hass.bus.async_listen("yandex_station_response", listener) - - -class YandexIntentHandler(IntentHandler): - request_id: str = None - response_text: str | None = None - response_waiter = None - - def __init__(self, entity_id: str): - self.intent_type = entity_id - self.response_waiter = asyncio.Event() - - _LOGGER.debug(f"Init intent: {self.intent_type}") - - async def async_handle(self, intent: Intent) -> IntentResponse: - self.request_id = intent.context.id - self.response_text = None - self.response_waiter.clear() - - await intent.hass.services.async_call( - "media_player", - "play_media", - { - "entity_id": self.intent_type, - "media_content_id": intent.text_input, - "media_content_type": f"question:{self.request_id}", - }, - ) - - await asyncio.wait_for(self.response_waiter.wait(), 2.0) - - response = intent.create_response() - if self.response_text: - response.async_set_speech(self.response_text) - return response