-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathentity.py
455 lines (396 loc) · 15.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""Common code for tplink."""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Awaitable, Callable, Coroutine, Mapping
from dataclasses import dataclass, replace
import logging
from typing import Any, Concatenate
from kasa import (
AuthenticationError,
Device,
DeviceType,
Feature,
KasaException,
TimeoutError,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import get_device_name, legacy_device_id
from .const import (
ATTR_CURRENT_A,
ATTR_CURRENT_POWER_W,
ATTR_TODAY_ENERGY_KWH,
ATTR_TOTAL_ENERGY_KWH,
DOMAIN,
PRIMARY_STATE_ID,
)
from .coordinator import TPLinkDataUpdateCoordinator
from .deprecate import DeprecatedInfo, async_check_create_deprecated
_LOGGER = logging.getLogger(__name__)
# Mapping from upstream category to homeassistant category
FEATURE_CATEGORY_TO_ENTITY_CATEGORY = {
Feature.Category.Config: EntityCategory.CONFIG,
Feature.Category.Info: EntityCategory.DIAGNOSTIC,
Feature.Category.Debug: EntityCategory.DIAGNOSTIC,
}
# Skips creating entities for primary features supported by a specialized platform.
# For example, we do not need a separate "state" switch for light bulbs.
DEVICETYPES_WITH_SPECIALIZED_PLATFORMS = {
DeviceType.Bulb,
DeviceType.LightStrip,
DeviceType.Dimmer,
DeviceType.Fan,
DeviceType.Thermostat,
}
# Primary features to always include even when the device type has its own platform
FEATURES_ALLOW_LIST = {
# lights have current_consumption and a specialized platform
"current_consumption"
}
# Features excluded due to future platform additions
EXCLUDED_FEATURES = {
# update
"current_firmware_version",
"available_firmware_version",
"update_available",
"check_latest_firmware",
# siren
"alarm",
}
LEGACY_KEY_MAPPING = {
"current": ATTR_CURRENT_A,
"current_consumption": ATTR_CURRENT_POWER_W,
"consumption_today": ATTR_TODAY_ENERGY_KWH,
"consumption_total": ATTR_TOTAL_ENERGY_KWH,
}
@dataclass(frozen=True, kw_only=True)
class TPLinkFeatureEntityDescription(EntityDescription):
"""Base class for a TPLink feature based entity description."""
deprecated_info: DeprecatedInfo | None = None
available_fn: Callable[[Device], bool] = lambda _: True
@dataclass(frozen=True, kw_only=True)
class TPLinkModuleEntityDescription(EntityDescription):
"""Base class for a TPLink module based entity description."""
deprecated_info: DeprecatedInfo | None = None
available_fn: Callable[[Device], bool] = lambda _: True
def async_refresh_after[_T: CoordinatedTPLinkEntity, **_P](
func: Callable[Concatenate[_T, _P], Awaitable[None]],
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
"""Define a wrapper to raise HA errors and refresh after."""
async def _async_wrap(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
try:
await func(self, *args, **kwargs)
except AuthenticationError as ex:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="device_authentication",
translation_placeholders={
"func": func.__name__,
"exc": str(ex),
},
) from ex
except TimeoutError as ex:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="device_timeout",
translation_placeholders={
"func": func.__name__,
"exc": str(ex),
},
) from ex
except KasaException as ex:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="device_error",
translation_placeholders={
"func": func.__name__,
"exc": str(ex),
},
) from ex
await self.coordinator.async_request_refresh()
return _async_wrap
class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator], ABC):
"""Common base class for all coordinated tplink entities."""
_attr_has_entity_name = True
_device: Device
def __init__(
self,
device: Device,
coordinator: TPLinkDataUpdateCoordinator,
*,
feature: Feature | None = None,
parent: Device | None = None,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._device: Device = device
self._feature = feature
registry_device = device
device_name = get_device_name(device, parent=parent)
if parent and parent.device_type is not Device.Type.Hub:
if not feature or feature.id == PRIMARY_STATE_ID:
# Entity will be added to parent if not a hub and no feature
# parameter (i.e. core platform like Light, Fan) or the feature
# is the primary state
registry_device = parent
device_name = get_device_name(registry_device)
else:
# Prefix the device name with the parent name unless it is a
# hub attached device. Sensible default for child devices like
# strip plugs or the ks240 where the child alias makes more
# sense in the context of the parent. i.e. Hall Ceiling Fan &
# Bedroom Ceiling Fan; Child device aliases will be Ceiling Fan
# and Dimmer Switch for both so should be distinguished by the
# parent name.
device_name = f"{get_device_name(parent)} {get_device_name(device, parent=parent)}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, str(registry_device.device_id))},
manufacturer="TP-Link",
model=registry_device.model,
name=device_name,
sw_version=registry_device.hw_info["sw_ver"],
hw_version=registry_device.hw_info["hw_ver"],
)
if (
parent is not None
and parent != registry_device
and parent.device_type is not Device.Type.WallSwitch
):
self._attr_device_info["via_device"] = (DOMAIN, parent.device_id)
else:
self._attr_device_info["connections"] = {
(dr.CONNECTION_NETWORK_MAC, device.mac)
}
self._attr_unique_id = self._get_unique_id()
self._async_call_update_attrs()
def _get_unique_id(self) -> str:
"""Return unique ID for the entity."""
return legacy_device_id(self._device)
@abstractmethod
@callback
def _async_update_attrs(self) -> bool:
"""Platforms implement this to update the entity internals.
The return value is used to the set the entity available attribute.
"""
raise NotImplementedError
@callback
def _async_call_update_attrs(self) -> None:
"""Call update_attrs and make entity unavailable on errors."""
try:
available = self._async_update_attrs()
except Exception as ex: # noqa: BLE001
if self._attr_available:
_LOGGER.warning(
"Unable to read data for %s %s: %s",
self._device,
self.entity_id,
ex,
)
self._attr_available = False
else:
self._attr_available = available
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._async_call_update_attrs()
super()._handle_coordinator_update()
@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success and self._attr_available
class CoordinatedTPLinkFeatureEntity(CoordinatedTPLinkEntity, ABC):
"""Common base class for all coordinated tplink feature entities."""
entity_description: TPLinkFeatureEntityDescription
_feature: Feature
def __init__(
self,
device: Device,
coordinator: TPLinkDataUpdateCoordinator,
*,
feature: Feature,
description: TPLinkFeatureEntityDescription,
parent: Device | None = None,
) -> None:
"""Initialize the entity."""
self.entity_description = description
super().__init__(device, coordinator, parent=parent, feature=feature)
def _get_unique_id(self) -> str:
"""Return unique ID for the entity."""
return self._get_feature_unique_id(self._device, self.entity_description)
@staticmethod
def _get_feature_unique_id(
device: Device, entity_description: TPLinkFeatureEntityDescription
) -> str:
"""Return unique ID for the entity."""
key = entity_description.key
# The unique id for the state feature in the switch platform is the
# device_id
if key == PRIMARY_STATE_ID:
return legacy_device_id(device)
# Historically the legacy device emeter attributes which are now
# replaced with features used slightly different keys. This ensures
# that those entities are not orphaned. Returns the mapped key or the
# provided key if not mapped.
key = LEGACY_KEY_MAPPING.get(key, key)
return f"{legacy_device_id(device)}_{key}"
@classmethod
def _category_for_feature(cls, feature: Feature | None) -> EntityCategory | None:
"""Return entity category for a feature."""
# Main controls have no category
if feature is None or feature.category is Feature.Category.Primary:
return None
if (
entity_category := FEATURE_CATEGORY_TO_ENTITY_CATEGORY.get(feature.category)
) is None:
_LOGGER.error(
"Unhandled category %s, fallback to DIAGNOSTIC", feature.category
)
entity_category = EntityCategory.DIAGNOSTIC
return entity_category
@classmethod
def _description_for_feature[_D: EntityDescription](
cls,
feature: Feature,
descriptions: Mapping[str, _D],
*,
device: Device,
parent: Device | None = None,
) -> _D | None:
"""Return description object for the given feature.
This is responsible for setting the common parameters & deciding
based on feature id which additional parameters are passed.
"""
if descriptions and (desc := descriptions.get(feature.id)):
translation_key: str | None = feature.id
# HA logic is to name entities based on the following logic:
# _attr_name > translation.name > description.name
# > device_class (if base platform supports).
name: str | None | UndefinedType = UNDEFINED
# The state feature gets the device name or the child device
# name if it's a child device
if feature.id == PRIMARY_STATE_ID:
translation_key = None
# if None will use device name
name = get_device_name(device, parent=parent) if parent else None
return replace(
desc,
translation_key=translation_key,
name=name, # if undefined will use translation key
entity_category=cls._category_for_feature(feature),
# enabled_default can be overridden to False in the description
entity_registry_enabled_default=feature.category
is not Feature.Category.Debug
and desc.entity_registry_enabled_default,
)
_LOGGER.debug(
"Device feature: %s (%s) needs an entity description defined in HA",
feature.name,
feature.id,
)
return None
@classmethod
def _entities_for_device[
_E: CoordinatedTPLinkFeatureEntity,
_D: TPLinkFeatureEntityDescription,
](
cls,
hass: HomeAssistant,
device: Device,
coordinator: TPLinkDataUpdateCoordinator,
*,
feature_type: Feature.Type,
entity_class: type[_E],
descriptions: Mapping[str, _D],
parent: Device | None = None,
) -> list[_E]:
"""Return a list of entities to add.
This filters out unwanted features to avoid creating unnecessary entities
for device features that are implemented by specialized platforms like light.
"""
entities: list[_E] = [
entity_class(
device,
coordinator,
feature=feat,
description=desc,
parent=parent,
)
for feat in device.features.values()
if feat.type == feature_type
and feat.id not in EXCLUDED_FEATURES
and (
feat.category is not Feature.Category.Primary
or device.device_type not in DEVICETYPES_WITH_SPECIALIZED_PLATFORMS
or feat.id in FEATURES_ALLOW_LIST
)
and (
desc := cls._description_for_feature(
feat, descriptions, device=device, parent=parent
)
)
and async_check_create_deprecated(
hass,
cls._get_feature_unique_id(device, desc),
desc,
)
]
return entities
@classmethod
def entities_for_device_and_its_children[
_E: CoordinatedTPLinkFeatureEntity,
_D: TPLinkFeatureEntityDescription,
](
cls,
hass: HomeAssistant,
device: Device,
coordinator: TPLinkDataUpdateCoordinator,
*,
feature_type: Feature.Type,
entity_class: type[_E],
descriptions: Mapping[str, _D],
child_coordinators: list[TPLinkDataUpdateCoordinator] | None = None,
) -> list[_E]:
"""Create entities for device and its children.
This is a helper that calls *_entities_for_device* for the device and its children.
"""
entities: list[_E] = []
# Add parent entities before children so via_device id works.
entities.extend(
cls._entities_for_device(
hass,
device,
coordinator=coordinator,
feature_type=feature_type,
entity_class=entity_class,
descriptions=descriptions,
)
)
if device.children:
_LOGGER.debug("Initializing device with %s children", len(device.children))
for idx, child in enumerate(device.children):
# HS300 does not like too many concurrent requests and its
# emeter data requires a request for each socket, so we receive
# separate coordinators.
if child_coordinators:
child_coordinator = child_coordinators[idx]
else:
child_coordinator = coordinator
entities.extend(
cls._entities_for_device(
hass,
child,
coordinator=child_coordinator,
feature_type=feature_type,
entity_class=entity_class,
descriptions=descriptions,
parent=device,
)
)
return entities