Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Tedee sensor entities."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from aiotedee import TedeeLock
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTime
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .coordinator import TedeeConfigEntry
19 from .entity import TedeeDescriptionEntity
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Describes Tedee sensor entity."""
25 
26  value_fn: Callable[[TedeeLock], float | None]
27 
28 
29 ENTITIES: tuple[TedeeSensorEntityDescription, ...] = (
31  key="battery_sensor",
32  device_class=SensorDeviceClass.BATTERY,
33  native_unit_of_measurement=PERCENTAGE,
34  state_class=SensorStateClass.MEASUREMENT,
35  value_fn=lambda lock: lock.battery_level,
36  entity_category=EntityCategory.DIAGNOSTIC,
37  ),
39  key="pullspring_duration",
40  translation_key="pullspring_duration",
41  device_class=SensorDeviceClass.DURATION,
42  native_unit_of_measurement=UnitOfTime.SECONDS,
43  state_class=SensorStateClass.MEASUREMENT,
44  value_fn=lambda lock: lock.duration_pullspring,
45  entity_category=EntityCategory.DIAGNOSTIC,
46  ),
47 )
48 
49 
51  hass: HomeAssistant,
52  entry: TedeeConfigEntry,
53  async_add_entities: AddEntitiesCallback,
54 ) -> None:
55  """Set up the Tedee sensor entity."""
56  coordinator = entry.runtime_data
57 
59  TedeeSensorEntity(lock, coordinator, entity_description)
60  for lock in coordinator.data.values()
61  for entity_description in ENTITIES
62  )
63 
64  def _async_add_new_lock(lock_id: int) -> None:
65  lock = coordinator.data[lock_id]
67  TedeeSensorEntity(lock, coordinator, entity_description)
68  for entity_description in ENTITIES
69  )
70 
71  coordinator.new_lock_callbacks.append(_async_add_new_lock)
72 
73 
75  """Tedee sensor entity."""
76 
77  entity_description: TedeeSensorEntityDescription
78 
79  @property
80  def native_value(self) -> float | None:
81  """Return the state of the sensor."""
82  return self.entity_descriptionentity_description.value_fn(self._lock_lock)
None async_setup_entry(HomeAssistant hass, TedeeConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:54