Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The lookin integration entity."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 import logging
7 
8 from aiolookin import (
9  POWER_CMD,
10  POWER_OFF_CMD,
11  POWER_ON_CMD,
12  Climate,
13  MeteoSensor,
14  Remote,
15 )
16 from aiolookin.models import Device, UDPCommandType, UDPEvent
17 
18 from homeassistant.helpers.device_registry import DeviceInfo
19 from homeassistant.helpers.update_coordinator import CoordinatorEntity
20 
21 from .const import DOMAIN, MODEL_NAMES
22 from .coordinator import LookinDataUpdateCoordinator
23 from .models import LookinData
24 
25 LOGGER = logging.getLogger(__name__)
26 
27 
28 def _lookin_device_to_device_info(lookin_device: Device, host: str) -> DeviceInfo:
29  """Convert a lookin device into DeviceInfo."""
30  return DeviceInfo(
31  identifiers={(DOMAIN, lookin_device.id)},
32  name=lookin_device.name,
33  manufacturer="LOOKin",
34  model=MODEL_NAMES[lookin_device.model],
35  sw_version=lookin_device.firmware,
36  configuration_url=f"http://{host}/device",
37  )
38 
39 
41  lookin_device: Device, uuid: str, device: Climate | Remote, host: str
42 ) -> DeviceInfo:
43  return DeviceInfo(
44  identifiers={(DOMAIN, uuid)},
45  name=device.name,
46  model=device.device_type,
47  via_device=(DOMAIN, lookin_device.id),
48  configuration_url=f"http://{host}/data/{uuid}",
49  )
50 
51 
53  """A mix in to set lookin attributes for the lookin device."""
54 
55  def _set_lookin_device_attrs(self, lookin_data: LookinData) -> None:
56  """Set attrs for the lookin device."""
57  self._lookin_device_lookin_device = lookin_data.lookin_device
58  self._lookin_protocol_lookin_protocol = lookin_data.lookin_protocol
59  self._lookin_udp_subs_lookin_udp_subs = lookin_data.lookin_udp_subs
60 
61 
63  LookinDeviceMixIn, CoordinatorEntity[LookinDataUpdateCoordinator[MeteoSensor]]
64 ):
65  """A lookin device entity on the device itself that uses the coordinator."""
66 
67  _attr_should_poll = False
68 
69  def __init__(self, lookin_data: LookinData) -> None:
70  """Init the lookin device entity."""
71  assert lookin_data.meteo_coordinator is not None
72  super().__init__(lookin_data.meteo_coordinator)
73  self._set_lookin_device_attrs_set_lookin_device_attrs(lookin_data)
75  lookin_data.lookin_device, lookin_data.host
76  )
77 
78 
80  """A mix in to set attributes for a lookin entity."""
81 
83  self,
84  uuid: str,
85  device: Remote | Climate,
86  lookin_data: LookinData,
87  ) -> None:
88  """Set attrs for the device controlled via the lookin device."""
89  self._device_device = device
90  self._uuid_uuid = uuid
91  self._meteo_coordinator_meteo_coordinator = lookin_data.meteo_coordinator
92  self._function_names_function_names = {function.name for function in self._device_device.functions}
93 
94 
96  LookinDeviceMixIn,
97  LookinEntityMixIn,
98  CoordinatorEntity[LookinDataUpdateCoordinator[Remote]],
99 ):
100  """A lookin device entity for an external device that uses the coordinator."""
101 
102  _attr_should_poll = False
103  _attr_assumed_state = True
104 
105  def __init__(
106  self,
107  coordinator: LookinDataUpdateCoordinator[Remote],
108  uuid: str,
109  device: Remote | Climate,
110  lookin_data: LookinData,
111  ) -> None:
112  """Init the base entity."""
113  super().__init__(coordinator)
114  self._set_lookin_device_attrs_set_lookin_device_attrs(lookin_data)
115  self._set_lookin_entity_attrs_set_lookin_entity_attrs(uuid, device, lookin_data)
117  self._lookin_device_lookin_device, uuid, device, lookin_data.host
118  )
119  self._attr_unique_id_attr_unique_id = uuid
120  self._attr_name_attr_name = device.name
121 
122  async def _async_send_command(self, command: str, signal: str = "FF") -> None:
123  """Send command from saved IR device."""
124  await self._lookin_protocol_lookin_protocol.send_command(
125  uuid=self._uuid_uuid, command=command, signal=signal
126  )
127 
128 
130  """A Lookin entity that has a power on and power off command."""
131 
132  def __init__(
133  self,
134  coordinator: LookinDataUpdateCoordinator[Remote],
135  uuid: str,
136  device: Remote | Climate,
137  lookin_data: LookinData,
138  ) -> None:
139  """Init the power entity."""
140  super().__init__(coordinator, uuid, device, lookin_data)
141  self._power_on_command_power_on_command: str = POWER_CMD
142  self._power_off_command_power_off_command: str = POWER_CMD
143  if POWER_ON_CMD in self._function_names_function_names:
144  self._power_on_command_power_on_command = POWER_ON_CMD
145  if POWER_OFF_CMD in self._function_names_function_names:
146  self._power_off_command_power_off_command = POWER_OFF_CMD
147 
148 
150  """A Lookin entity that has a power on and power off command with push updates."""
151 
152  def __init__(
153  self,
154  coordinator: LookinDataUpdateCoordinator[Remote],
155  uuid: str,
156  device: Remote,
157  lookin_data: LookinData,
158  ) -> None:
159  """Init the entity."""
160  super().__init__(coordinator, uuid, device, lookin_data)
161  self._update_from_status_update_from_status(self._remote_remote.status)
162  self._attr_name_attr_name_attr_name = self._remote_remote.name
163 
164  @property
165  def _remote(self) -> Remote:
166  return self.coordinator.data
167 
168  @abstractmethod
169  def _update_from_status(self, status: str) -> None:
170  """Update properties from status."""
171 
172  def _async_push_update(self, event: UDPEvent) -> None:
173  """Process an update pushed via UDP."""
174  LOGGER.debug("Processing push message for %s: %s", self.entity_id, event)
175  self._update_from_status_update_from_status(event.value)
176  self.coordinator.async_set_updated_data(self._remote_remote)
177 
178  async def _async_push_update_device(self, event: UDPEvent) -> None:
179  """Process an update pushed via UDP."""
180  LOGGER.debug("Processing push message for %s: %s", self.entity_id, event)
181  await self.coordinator.async_refresh()
182  self._attr_name_attr_name_attr_name = self._remote_remote.name
183 
184  async def async_added_to_hass(self) -> None:
185  """Call when the entity is added to hass."""
186  await super().async_added_to_hass()
187  self.async_on_remove(
188  self._lookin_udp_subs_lookin_udp_subs.subscribe_event(
189  self._lookin_device_lookin_device.id,
190  UDPCommandType.ir,
191  self._uuid_uuid,
192  self._async_push_update_async_push_update,
193  )
194  )
195  self.async_on_remove(
196  self._lookin_udp_subs_lookin_udp_subs.subscribe_event(
197  self._lookin_device_lookin_device.id,
198  UDPCommandType.data,
199  self._uuid_uuid,
200  self._async_push_update_device_async_push_update_device,
201  )
202  )
None _async_send_command(self, str command, str signal="FF")
Definition: entity.py:122
None __init__(self, LookinDataUpdateCoordinator[Remote] coordinator, str uuid, Remote|Climate device, LookinData lookin_data)
Definition: entity.py:111
None _set_lookin_device_attrs(self, LookinData lookin_data)
Definition: entity.py:55
None _set_lookin_entity_attrs(self, str uuid, Remote|Climate device, LookinData lookin_data)
Definition: entity.py:87
None __init__(self, LookinDataUpdateCoordinator[Remote] coordinator, str uuid, Remote|Climate device, LookinData lookin_data)
Definition: entity.py:138
None __init__(self, LookinDataUpdateCoordinator[Remote] coordinator, str uuid, Remote device, LookinData lookin_data)
Definition: entity.py:158
DeviceInfo _lookin_device_to_device_info(Device lookin_device, str host)
Definition: entity.py:28
DeviceInfo _lookin_controlled_device_to_device_info(Device lookin_device, str uuid, Climate|Remote device, str host)
Definition: entity.py:42