Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base SamsungTV Entity."""
2 
3 from __future__ import annotations
4 
5 from wakeonlan import send_magic_packet
6 
7 from homeassistant.const import (
8  ATTR_CONNECTIONS,
9  ATTR_IDENTIFIERS,
10  CONF_HOST,
11  CONF_MAC,
12  CONF_MODEL,
13  CONF_NAME,
14 )
15 from homeassistant.exceptions import HomeAssistantError
16 from homeassistant.helpers import device_registry as dr
17 from homeassistant.helpers.device_registry import DeviceInfo
18 from homeassistant.helpers.entity import Entity
19 from homeassistant.helpers.trigger import PluggableAction
20 from homeassistant.helpers.update_coordinator import CoordinatorEntity
21 
22 from .const import CONF_MANUFACTURER, DOMAIN, LOGGER
23 from .coordinator import SamsungTVDataUpdateCoordinator
24 from .triggers.turn_on import async_get_turn_on_trigger
25 
26 
27 class SamsungTVEntity(CoordinatorEntity[SamsungTVDataUpdateCoordinator], Entity):
28  """Defines a base SamsungTV entity."""
29 
30  _attr_has_entity_name = True
31 
32  def __init__(self, *, coordinator: SamsungTVDataUpdateCoordinator) -> None:
33  """Initialize the SamsungTV entity."""
34  super().__init__(coordinator)
35  self._bridge_bridge = coordinator.bridge
36  config_entry = coordinator.config_entry
37  self._mac: str | None = config_entry.data.get(CONF_MAC)
38  self._host: str | None = config_entry.data.get(CONF_HOST)
39  # Fallback for legacy models that doesn't have a API to retrieve MAC or SerialNumber
40  self._attr_unique_id_attr_unique_id = config_entry.unique_id or config_entry.entry_id
41  self._attr_device_info_attr_device_info = DeviceInfo(
42  name=config_entry.data.get(CONF_NAME),
43  manufacturer=config_entry.data.get(CONF_MANUFACTURER),
44  model=config_entry.data.get(CONF_MODEL),
45  model_id=config_entry.data.get(CONF_MODEL),
46  )
47  if self.unique_idunique_id:
48  self._attr_device_info_attr_device_info[ATTR_IDENTIFIERS] = {(DOMAIN, self.unique_idunique_id)}
49  if self._mac:
50  self._attr_device_info_attr_device_info[ATTR_CONNECTIONS] = {
51  (dr.CONNECTION_NETWORK_MAC, self._mac)
52  }
53  self._turn_on_action_turn_on_action = PluggableAction(self.async_write_ha_stateasync_write_ha_state)
54 
55  @property
56  def available(self) -> bool:
57  """Return the availability of the device."""
58  if self._bridge_bridge.auth_failed:
59  return False
60  return (
61  self.coordinator.is_on
62  or bool(self._turn_on_action_turn_on_action)
63  or self._mac is not None
64  or self._bridge_bridge.power_off_in_progress
65  )
66 
67  async def async_added_to_hass(self) -> None:
68  """Connect and subscribe to dispatcher signals and state updates."""
69  await super().async_added_to_hass()
70 
71  if (entry := self.registry_entryregistry_entry) and entry.device_id:
72  self.async_on_removeasync_on_remove(
73  self._turn_on_action_turn_on_action.async_register(
74  self.hasshasshass, async_get_turn_on_trigger(entry.device_id)
75  )
76  )
77 
78  def _wake_on_lan(self) -> None:
79  """Wake the device via wake on lan."""
80  send_magic_packet(self._mac, ip_address=self._host)
81  # If the ip address changed since we last saw the device
82  # broadcast a packet as well
83  send_magic_packet(self._mac)
84 
85  async def _async_turn_off(self) -> None:
86  """Turn the device off."""
87  await self._bridge_bridge.async_power_off()
88  await self.coordinator.async_refresh()
89 
90  async def _async_turn_on(self) -> None:
91  """Turn the remote on."""
92  if self._turn_on_action_turn_on_action:
93  LOGGER.debug("Attempting to turn on %s via automation", self.entity_identity_id)
94  await self._turn_on_action_turn_on_action.async_run(self.hasshasshass, self._context_context)
95  elif self._mac:
96  LOGGER.warning(
97  "Attempting to turn on %s via Wake-On-Lan; if this does not work, "
98  "please ensure that Wake-On-Lan is available for your device or use "
99  "a turn_on automation",
100  self.entity_identity_id,
101  )
102  await self.hasshasshass.async_add_executor_job(self._wake_on_lan_wake_on_lan)
103  else:
104  LOGGER.error(
105  "Unable to turn on %s, as it does not have an automation configured",
106  self.entity_identity_id,
107  )
108  raise HomeAssistantError(
109  f"Entity {self.entity_id} does not support this service."
110  )
None __init__(self, *SamsungTVDataUpdateCoordinator coordinator)
Definition: entity.py:32
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_register(HomeAssistant hass, system_health.SystemHealthRegistration register)
dict[str, str] async_get_turn_on_trigger(str device_id)
Definition: turn_on.py:39