1 """Support for TPLink sensor entities."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
6 from typing
import TYPE_CHECKING, cast
8 from kasa
import Feature
11 DOMAIN
as SENSOR_DOMAIN,
14 SensorEntityDescription,
20 from .
import TPLinkConfigEntry
21 from .const
import UNIT_MAPPING
22 from .deprecate
import async_cleanup_deprecated
23 from .entity
import CoordinatedTPLinkFeatureEntity, TPLinkFeatureEntityDescription
26 @dataclass(frozen=True, kw_only=True)
28 SensorEntityDescription, TPLinkFeatureEntityDescription
30 """Base class for a TPLink feature based sensor entity description."""
33 SENSOR_DESCRIPTIONS: tuple[TPLinkSensorEntityDescription, ...] = (
35 key=
"current_consumption",
36 device_class=SensorDeviceClass.POWER,
37 state_class=SensorStateClass.MEASUREMENT,
40 key=
"consumption_total",
41 device_class=SensorDeviceClass.ENERGY,
42 state_class=SensorStateClass.TOTAL_INCREASING,
45 key=
"consumption_today",
46 device_class=SensorDeviceClass.ENERGY,
47 state_class=SensorStateClass.TOTAL_INCREASING,
50 key=
"consumption_this_month",
51 device_class=SensorDeviceClass.ENERGY,
52 state_class=SensorStateClass.TOTAL_INCREASING,
56 device_class=SensorDeviceClass.VOLTAGE,
57 state_class=SensorStateClass.MEASUREMENT,
61 device_class=SensorDeviceClass.CURRENT,
62 state_class=SensorStateClass.MEASUREMENT,
66 device_class=SensorDeviceClass.TEMPERATURE,
67 state_class=SensorStateClass.MEASUREMENT,
71 entity_registry_enabled_default=
False,
73 device_class=SensorDeviceClass.TIMESTAMP,
77 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
78 state_class=SensorStateClass.MEASUREMENT,
82 state_class=SensorStateClass.MEASUREMENT,
89 device_class=SensorDeviceClass.BATTERY,
90 state_class=SensorStateClass.MEASUREMENT,
94 device_class=SensorDeviceClass.TIMESTAMP,
98 device_class=SensorDeviceClass.TIMESTAMP,
101 key=
"water_alert_timestamp",
102 device_class=SensorDeviceClass.TIMESTAMP,
106 device_class=SensorDeviceClass.HUMIDITY,
107 state_class=SensorStateClass.MEASUREMENT,
110 key=
"report_interval",
111 device_class=SensorDeviceClass.DURATION,
118 device_class=SensorDeviceClass.TEMPERATURE,
119 state_class=SensorStateClass.MEASUREMENT,
123 SENSOR_DESCRIPTIONS_MAP = {desc.key: desc
for desc
in SENSOR_DESCRIPTIONS}
128 config_entry: TPLinkConfigEntry,
129 async_add_entities: AddEntitiesCallback,
131 """Set up sensors."""
132 data = config_entry.runtime_data
133 parent_coordinator = data.parent_coordinator
134 children_coordinators = data.children_coordinators
135 device = parent_coordinator.device
137 entities = CoordinatedTPLinkFeatureEntity.entities_for_device_and_its_children(
140 coordinator=parent_coordinator,
141 feature_type=Feature.Type.Sensor,
142 entity_class=TPLinkSensorEntity,
143 descriptions=SENSOR_DESCRIPTIONS_MAP,
144 child_coordinators=children_coordinators,
151 """Representation of a feature-based TPLink sensor."""
153 entity_description: TPLinkSensorEntityDescription
157 """Update the entity's attributes."""
159 if value
is not None and self.
_feature_feature.precision_hint
is not None:
160 value = round(cast(float, value), self.
_feature_feature.precision_hint)
166 from datetime
import date, datetime
168 assert isinstance(value, str | int | float | date | datetime |
None)
172 if (unit := self.
_feature_feature.unit)
is not None:
None _async_update_attrs(self)
_attr_suggested_display_precision
_attr_native_unit_of_measurement
None async_cleanup_deprecated(HomeAssistant hass, str platform, str entry_id, Sequence[CoordinatedTPLinkFeatureEntity] entities)
None async_setup_entry(HomeAssistant hass, TPLinkConfigEntry config_entry, AddEntitiesCallback async_add_entities)