Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for yalexs ble sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from yalexs_ble import ConnectionInfo, LockInfo, LockState
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import (
17  PERCENTAGE,
18  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
19  EntityCategory,
20  UnitOfElectricPotential,
21 )
22 from homeassistant.core import HomeAssistant, callback
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from . import YALEXSBLEConfigEntry
26 from .entity import YALEXSBLEEntity
27 from .models import YaleXSBLEData
28 
29 
30 @dataclass(frozen=True, kw_only=True)
32  """Describes Yale Access Bluetooth sensor entity."""
33 
34  value_fn: Callable[[LockState, LockInfo, ConnectionInfo], int | float | None]
35 
36 
37 SENSORS: tuple[YaleXSBLESensorEntityDescription, ...] = (
39  key="", # No key for the original RSSI sensor unique id
40  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
41  entity_category=EntityCategory.DIAGNOSTIC,
42  state_class=SensorStateClass.MEASUREMENT,
43  has_entity_name=True,
44  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
45  entity_registry_enabled_default=False,
46  value_fn=lambda state, info, connection: connection.rssi,
47  ),
49  key="battery_level",
50  device_class=SensorDeviceClass.BATTERY,
51  entity_category=EntityCategory.DIAGNOSTIC,
52  state_class=SensorStateClass.MEASUREMENT,
53  has_entity_name=True,
54  native_unit_of_measurement=PERCENTAGE,
55  value_fn=lambda state, info, connection: state.battery.percentage
56  if state.battery
57  else None,
58  ),
60  key="battery_voltage",
61  translation_key="battery_voltage",
62  device_class=SensorDeviceClass.VOLTAGE,
63  entity_category=EntityCategory.DIAGNOSTIC,
64  state_class=SensorStateClass.MEASUREMENT,
65  has_entity_name=True,
66  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
67  entity_registry_enabled_default=False,
68  value_fn=lambda state, info, connection: state.battery.voltage
69  if state.battery
70  else None,
71  ),
72 )
73 
74 
76  hass: HomeAssistant,
77  entry: YALEXSBLEConfigEntry,
78  async_add_entities: AddEntitiesCallback,
79 ) -> None:
80  """Set up YALE XS Bluetooth sensors."""
81  data = entry.runtime_data
82  async_add_entities(YaleXSBLESensor(description, data) for description in SENSORS)
83 
84 
86  """Yale XS Bluetooth sensor."""
87 
88  entity_description: YaleXSBLESensorEntityDescription
89 
90  def __init__(
91  self,
92  description: YaleXSBLESensorEntityDescription,
93  data: YaleXSBLEData,
94  ) -> None:
95  """Initialize the sensor."""
96  self.entity_descriptionentity_description = description
97  super().__init__(data)
98  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{data.lock.address}{description.key}"
99 
100  @callback
102  self, new_state: LockState, lock_info: LockInfo, connection_info: ConnectionInfo
103  ) -> None:
104  """Update the state."""
105  self._attr_native_value_attr_native_value = self.entity_descriptionentity_description.value_fn(
106  new_state, lock_info, connection_info
107  )
108  super()._async_update_state(new_state, lock_info, connection_info)
None __init__(self, YaleXSBLESensorEntityDescription description, YaleXSBLEData data)
Definition: sensor.py:94
None _async_update_state(self, LockState new_state, LockInfo lock_info, ConnectionInfo connection_info)
Definition: sensor.py:103
None async_setup_entry(HomeAssistant hass, YALEXSBLEConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:79