Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support notifications through TTS service."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA,
12  BaseNotificationService,
13 )
14 from homeassistant.const import ATTR_ENTITY_ID, CONF_ENTITY_ID, CONF_NAME
15 from homeassistant.core import HomeAssistant, split_entity_id
17 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
18 
19 from . import ATTR_LANGUAGE, ATTR_MEDIA_PLAYER_ENTITY_ID, ATTR_MESSAGE, DOMAIN
20 
21 CONF_MEDIA_PLAYER = "media_player"
22 CONF_TTS_SERVICE = "tts_service"
23 ENTITY_LEGACY_PROVIDER_GROUP = "entity_or_legacy_provider"
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 PLATFORM_SCHEMA = vol.All(
28  cv.has_at_least_one_key(CONF_TTS_SERVICE, CONF_ENTITY_ID),
29  NOTIFY_PLATFORM_SCHEMA.extend(
30  {
31  vol.Required(CONF_NAME): cv.string,
32  vol.Exclusive(CONF_TTS_SERVICE, ENTITY_LEGACY_PROVIDER_GROUP): cv.entity_id,
33  vol.Exclusive(
34  CONF_ENTITY_ID, ENTITY_LEGACY_PROVIDER_GROUP
35  ): cv.entities_domain(DOMAIN),
36  vol.Required(CONF_MEDIA_PLAYER): cv.entity_id,
37  vol.Optional(ATTR_LANGUAGE): cv.string,
38  }
39  ),
40 )
41 
42 
44  hass: HomeAssistant,
45  config: ConfigType,
46  discovery_info: DiscoveryInfoType | None = None,
47 ) -> TTSNotificationService:
48  """Return the notify service."""
49 
50  return TTSNotificationService(config)
51 
52 
53 class TTSNotificationService(BaseNotificationService):
54  """The TTS Notification Service."""
55 
56  def __init__(self, config: ConfigType) -> None:
57  """Initialize the service."""
58  self._target_target: str | None = None
59  self._tts_service_tts_service: str | None = None
60  if entity_id := config.get(CONF_ENTITY_ID):
61  self._target_target = entity_id
62  else:
63  _, self._tts_service_tts_service = split_entity_id(config[CONF_TTS_SERVICE])
64  self._media_player_media_player = config[CONF_MEDIA_PLAYER]
65  self._language_language = config.get(ATTR_LANGUAGE)
66 
67  async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
68  """Call TTS service to speak the notification."""
69  _LOGGER.debug("%s '%s' on %s", self._tts_service_tts_service, message, self._media_player_media_player)
70 
71  data = {
72  ATTR_MESSAGE: message,
73  }
74  service_name = ""
75 
76  if self._tts_service_tts_service:
77  data[ATTR_ENTITY_ID] = self._media_player_media_player
78  service_name = self._tts_service_tts_service
79  elif self._target_target:
80  data[ATTR_ENTITY_ID] = self._target_target
81  data[ATTR_MEDIA_PLAYER_ENTITY_ID] = self._media_player_media_player
82  service_name = "speak"
83  if self._language_language:
84  data[ATTR_LANGUAGE] = self._language_language
85 
86  await self.hass.services.async_call(
87  DOMAIN,
88  service_name,
89  data,
90  )
None async_send_message(self, str message="", **Any kwargs)
Definition: notify.py:67
TTSNotificationService async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:47
tuple[str, str] split_entity_id(str entity_id)
Definition: core.py:214