Home Assistant Unofficial Reference 2024.12.1
notify.py
Go to the documentation of this file.
1 """Support for LG WebOS TV notification service."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aiowebostv import WebOsClient, WebOsTvPairError
9 
10 from homeassistant.components.notify import ATTR_DATA, BaseNotificationService
11 from homeassistant.const import ATTR_ICON
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
14 
15 from .const import ATTR_CONFIG_ENTRY_ID, DATA_CONFIG_ENTRY, DOMAIN, WEBOSTV_EXCEPTIONS
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
21  hass: HomeAssistant,
22  config: ConfigType,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> BaseNotificationService | None:
25  """Return the notify service."""
26 
27  if discovery_info is None:
28  return None
29 
30  client = hass.data[DOMAIN][DATA_CONFIG_ENTRY][discovery_info[ATTR_CONFIG_ENTRY_ID]]
31 
32  return LgWebOSNotificationService(client)
33 
34 
35 class LgWebOSNotificationService(BaseNotificationService):
36  """Implement the notification service for LG WebOS TV."""
37 
38  def __init__(self, client: WebOsClient) -> None:
39  """Initialize the service."""
40  self._client_client = client
41 
42  async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
43  """Send a message to the tv."""
44  try:
45  if not self._client_client.is_connected():
46  await self._client_client.connect()
47 
48  data = kwargs[ATTR_DATA]
49  icon_path = data.get(ATTR_ICON) if data else None
50  await self._client_client.send_message(message, icon_path=icon_path)
51  except WebOsTvPairError:
52  _LOGGER.error("Pairing with TV failed")
53  except FileNotFoundError:
54  _LOGGER.error("Icon %s not found", icon_path)
55  except WEBOSTV_EXCEPTIONS:
56  _LOGGER.error("TV unreachable")
None async_send_message(self, str message="", **Any kwargs)
Definition: notify.py:42
BaseNotificationService|None async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
Definition: notify.py:24