Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for Google Assistant SDK broadcast notifications."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
11 
12 from .const import CONF_LANGUAGE_CODE, DOMAIN
13 from .helpers import async_send_text_commands, default_language_code
14 
15 # https://support.google.com/assistant/answer/9071582?hl=en
16 LANG_TO_BROADCAST_COMMAND = {
17  "en": ("broadcast {0}", "broadcast to {1} {0}"),
18  "de": (
19  "Nachricht an alle {0}", # codespell:ignore alle
20  "Nachricht an alle an {1} {0}", # codespell:ignore alle
21  ),
22  "es": ("Anuncia {0}", "Anuncia en {1} {0}"),
23  "fr": ("Diffuse {0}", "Diffuse dans {1} {0}"),
24  "it": ("Trasmetti a tutti {0}", "Trasmetti in {1} {0}"),
25  "ja": ("{0}とブロードキャストして", "{0}と{1}にブロードキャストして"),
26  "ko": ("{0} 라고 방송해 줘", "{0} 라고 {1}에 방송해 줘"),
27  "pt": ("Transmitir {0}", "Transmitir {0} para {1}"),
28 }
29 
30 
31 def broadcast_commands(language_code: str) -> tuple[str, str]:
32  """Get the commands for broadcasting a message for the given language code.
33 
34  Return type is a tuple where [0] is for broadcasting to your entire home,
35  while [1] is for broadcasting to a specific target.
36  """
37  return LANG_TO_BROADCAST_COMMAND[language_code.split("-", maxsplit=1)[0]]
38 
39 
41  hass: HomeAssistant,
42  config: ConfigType,
43  discovery_info: DiscoveryInfoType | None = None,
44 ) -> BaseNotificationService:
45  """Get the broadcast notification service."""
46  return BroadcastNotificationService(hass)
47 
48 
49 class BroadcastNotificationService(BaseNotificationService):
50  """Implement broadcast notification service."""
51 
52  def __init__(self, hass: HomeAssistant) -> None:
53  """Initialize the service."""
54  self.hasshass = hass
55 
56  async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
57  """Send a message."""
58  if not message:
59  return
60 
61  # There can only be 1 entry (config_flow has single_instance_allowed)
62  entry: ConfigEntry = self.hasshass.config_entries.async_entries(DOMAIN)[0]
63  language_code = entry.options.get(
64  CONF_LANGUAGE_CODE, default_language_code(self.hasshass)
65  )
66 
67  commands: list[str] = []
68  targets = kwargs.get(ATTR_TARGET)
69  if not targets:
70  commands.append(broadcast_commands(language_code)[0].format(message))
71  else:
72  commands.extend(
73  broadcast_commands(language_code)[1].format(message, target)
74  for target in targets
75  )
76  await async_send_text_commands(self.hasshass, commands)
list[CommandResponse] async_send_text_commands(HomeAssistant hass, list[str] commands, list[str]|None media_players=None)
Definition: helpers.py:62
tuple[str, str] broadcast_commands(str language_code)
Definition: notify.py:31
BaseNotificationService async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:44