def __init__( self, hass: HomeAssistant, name_template: Template, value_template: Template, availability_template: Template | None, command_set_value: dict[str, Any], step_template: Template, minimum_template: Template | None, maximum_template: Template | None, optimistic: bool, unique_id: str | None, ) -> None: """Initialize the number.""" super().__init__(availability_template=availability_template) self._attr_name = DEFAULT_NAME self._name_template = name_template name_template.hass = hass with contextlib.suppress(TemplateError): self._attr_name = name_template.async_render(parse_result=False) self._value_template = value_template domain = __name__.split(".")[-2] self._command_set_value = Script(hass, command_set_value, self._attr_name, domain) self._step_template = step_template self._min_value_template = minimum_template self._max_value_template = maximum_template self._attr_assumed_state = self._optimistic = optimistic self._attr_unique_id = unique_id self._attr_value = None self._attr_step = None
def async_template(hass: HomeAssistant, value_template: Template, variables: TemplateVarsType = None) -> bool: """Test if template condition matches.""" try: value: str = value_template.async_render(variables, parse_result=False) except TemplateError as ex: raise ConditionErrorMessage("template", str(ex)) from ex return value.lower() == "true"
def async_template(hass: HomeAssistant, value_template: Template, variables: TemplateVarsType = None) -> bool: """Test if template condition matches.""" try: value = value_template.async_render(variables) except TemplateError as ex: _LOGGER.error("Error during template condition: %s", ex) return False return value.lower() == 'true'
def async_template(hass: HomeAssistant, value_template: Template, variables: TemplateVarsType = None) -> bool: """Test if template condition matches.""" try: value = value_template.async_render(variables) except TemplateError as ex: _LOGGER.error("Error during template condition: %s", ex) return False return value.lower() == "true"
def __init__( self, hass: HomeAssistant, name_template: Template, availability_template: Template | None, command_press: dict[str, Any], device_class: ButtonDeviceClass | None, unique_id: str | None, icon_template: Template | None, ) -> None: """Initialize the button.""" super().__init__(availability_template=availability_template, icon_template=icon_template) self._attr_name = DEFAULT_NAME self._name_template = name_template name_template.hass = hass with contextlib.suppress(TemplateError): self._attr_name = name_template.async_render(parse_result=False) self._command_press = Script(hass, command_press, self._attr_name, DOMAIN) self._attr_device_class = device_class self._attr_unique_id = unique_id self._attr_state = None
async def async_play_media(self, media_type: str, media_id: str, extra: dict = None, **kwargs): # backward support Hass lower than v2022.3 if '/api/tts_proxy/' in media_id: session = async_get_clientsession(self.hass) media_id = await utils.get_tts_message(session, media_id) media_type = 'tts' if media_id.startswith("media-source://tts/"): query = utils.decode_media_source(media_id) if query.get("template"): template = Template(query.pop("template"), self.hass) media_id = template.async_render(query) else: media_id = query["message"] if query.get("volume_level"): extra.setdefault("volume_level", float(query["volume_level"])) # provider, music - from 3rd party TTS (ex google) if media_type in ("provider", "music"): media_type = "text" if not media_id: _LOGGER.warning(f"Получено пустое media_id") return # tts for backward compatibility if media_type == "tts": media_type = "text" elif media_type == 'brightness': await self._set_brightness(media_id) return elif media_type == 'beta': await self._set_beta(media_id) return elif media_type == 'settings': await self._set_settings(media_id) return if self.local_state: if 'https://' in media_id or 'http://' in media_id: session = async_get_clientsession(self.hass) payload = await utils.get_media_payload(media_id, session) if not payload: _LOGGER.warning(f"Unsupported url: {media_id}") return elif media_type.startswith(("text:", "dialog:")): payload = { 'command': 'sendText', 'text': self.yandex_dialog(media_type, media_id) } elif media_type == 'text': # даже в локальном режиме делам TTS через облако, чтоб колонка # не продолжала слушать force_local: bool = extra and extra.get("force_local") if self.quasar.session.x_token and not force_local: media_id = utils.fix_cloud_text(media_id) if extra and extra.get("volume_level") is not None: self._check_set_alice_volume(extra["volume_level"]) await self.quasar.send(self.device, media_id, is_tts=True) return else: payload = { 'command': 'sendText', 'text': f"Повтори за мной '{media_id}'" } elif media_type == 'command': payload = {'command': 'sendText', 'text': media_id} elif media_type == 'dialog': if extra and extra.get("volume_level") is not None: self._check_set_alice_volume(extra["volume_level"]) payload = utils.update_form( 'personal_assistant.scenarios.repeat_after_me', request=media_id) elif media_type == 'json': payload = json.loads(media_id) elif RE_MUSIC_ID.match(media_id): payload = { 'command': 'playMusic', 'id': media_id, 'type': media_type } elif media_type == 'shopping_list': await self._shopping_list() return elif media_type.startswith('question'): request_id = (media_type.split(':', 1)[1] if ':' in media_type else None) card = await self.glagol.send({ 'command': 'sendText', 'text': media_id }) await self.response(card, request_id) return else: _LOGGER.warning(f"Unsupported local media: {media_id}") return await self.glagol.send(payload) else: if media_type.startswith(("text:", "dialog:")): media_id = self.yandex_dialog(media_type, media_id) await self.quasar.send(self.device, media_id) elif media_type == 'text': media_id = utils.fix_cloud_text(media_id) await self.quasar.send(self.device, media_id, is_tts=True) elif media_type == 'command': media_id = utils.fix_cloud_text(media_id) await self.quasar.send(self.device, media_id) elif media_type == 'brightness': await self._set_brightness(media_id) return else: _LOGGER.warning(f"Unsupported cloud media: {media_type}") return