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