Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor entities for the Motionblinds BLE integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 from math import ceil
9 from typing import Generic, TypeVar
10 
11 from motionblindsble.const import (
12  MotionBlindType,
13  MotionCalibrationType,
14  MotionConnectionType,
15 )
16 from motionblindsble.device import MotionDevice
17 
19  SensorDeviceClass,
20  SensorEntity,
21  SensorEntityDescription,
22  SensorStateClass,
23 )
24 from homeassistant.config_entries import ConfigEntry
25 from homeassistant.const import (
26  PERCENTAGE,
27  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
28  EntityCategory,
29 )
30 from homeassistant.core import HomeAssistant, callback
31 from homeassistant.helpers.entity_platform import AddEntitiesCallback
32 from homeassistant.helpers.typing import StateType
33 
34 from .const import (
35  ATTR_BATTERY,
36  ATTR_CALIBRATION,
37  ATTR_CONNECTION,
38  ATTR_SIGNAL_STRENGTH,
39  CONF_MAC_CODE,
40  DOMAIN,
41 )
42 from .entity import MotionblindsBLEEntity
43 
44 _LOGGER = logging.getLogger(__name__)
45 
46 PARALLEL_UPDATES = 0
47 
48 _T = TypeVar("_T")
49 
50 
51 @dataclass(frozen=True, kw_only=True)
53  """Entity description of a sensor entity with initial_value attribute."""
54 
55  initial_value: str | None = None
56  register_callback_func: Callable[
57  [MotionDevice], Callable[[Callable[[_T | None], None]], None]
58  ]
59  value_func: Callable[[_T | None], StateType]
60  is_supported: Callable[[MotionDevice], bool] = lambda device: True
61 
62 
63 SENSORS: tuple[MotionblindsBLESensorEntityDescription, ...] = (
64  MotionblindsBLESensorEntityDescription[MotionConnectionType](
65  key=ATTR_CONNECTION,
66  translation_key=ATTR_CONNECTION,
67  device_class=SensorDeviceClass.ENUM,
68  entity_category=EntityCategory.DIAGNOSTIC,
69  options=["connected", "connecting", "disconnected", "disconnecting"],
70  initial_value=MotionConnectionType.DISCONNECTED.value,
71  register_callback_func=lambda device: device.register_connection_callback,
72  value_func=lambda value: value.value if value else None,
73  ),
74  MotionblindsBLESensorEntityDescription[MotionCalibrationType](
75  key=ATTR_CALIBRATION,
76  translation_key=ATTR_CALIBRATION,
77  device_class=SensorDeviceClass.ENUM,
78  entity_category=EntityCategory.DIAGNOSTIC,
79  options=["calibrated", "uncalibrated", "calibrating"],
80  register_callback_func=lambda device: device.register_calibration_callback,
81  value_func=lambda value: value.value if value else None,
82  is_supported=lambda device: device.blind_type
83  in {MotionBlindType.CURTAIN, MotionBlindType.VERTICAL},
84  ),
85  MotionblindsBLESensorEntityDescription[int](
86  key=ATTR_SIGNAL_STRENGTH,
87  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
88  entity_category=EntityCategory.DIAGNOSTIC,
89  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
90  register_callback_func=lambda device: device.register_signal_strength_callback,
91  value_func=lambda value: value,
92  entity_registry_enabled_default=False,
93  ),
94 )
95 
96 
98  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
99 ) -> None:
100  """Set up sensor entities based on a config entry."""
101 
102  device: MotionDevice = hass.data[DOMAIN][entry.entry_id]
103 
104  entities: list[SensorEntity] = [
105  MotionblindsBLESensorEntity(device, entry, description)
106  for description in SENSORS
107  if description.is_supported(device)
108  ]
109  entities.append(BatterySensor(device, entry))
110  async_add_entities(entities)
111 
112 
114  """Representation of a sensor entity."""
115 
116  entity_description: MotionblindsBLESensorEntityDescription[_T]
117 
118  def __init__(
119  self,
120  device: MotionDevice,
121  entry: ConfigEntry,
122  entity_description: MotionblindsBLESensorEntityDescription[_T],
123  ) -> None:
124  """Initialize the sensor entity."""
125  super().__init__(
126  device, entry, entity_description, unique_id_suffix=entity_description.key
127  )
128  self._attr_native_value_attr_native_value = entity_description.initial_value
129 
130  async def async_added_to_hass(self) -> None:
131  """Log sensor entity information."""
132  _LOGGER.debug(
133  "(%s) Setting up %s sensor entity",
134  self.entryentry.data[CONF_MAC_CODE],
135  self.entity_descriptionentity_description.key.replace("_", " "),
136  )
137 
138  def async_callback(value: _T | None) -> None:
139  """Update the sensor value."""
140  self._attr_native_value_attr_native_value = self.entity_descriptionentity_description.value_func(value)
141  self.async_write_ha_stateasync_write_ha_state()
142 
143  self.entity_descriptionentity_description.register_callback_func(self.devicedevice)(async_callback)
144 
145 
147  """Representation of a battery sensor entity."""
148 
149  def __init__(
150  self,
151  device: MotionDevice,
152  entry: ConfigEntry,
153  ) -> None:
154  """Initialize the sensor entity."""
155  entity_description = SensorEntityDescription(
156  key=ATTR_BATTERY,
157  native_unit_of_measurement=PERCENTAGE,
158  device_class=SensorDeviceClass.BATTERY,
159  state_class=SensorStateClass.MEASUREMENT,
160  entity_category=EntityCategory.DIAGNOSTIC,
161  )
162  super().__init__(device, entry, entity_description)
163 
164  async def async_added_to_hass(self) -> None:
165  """Register device callbacks."""
166  await super().async_added_to_hass()
167  self.devicedevice.register_battery_callback(self.async_update_batteryasync_update_battery)
168 
169  @callback
171  self,
172  battery_percentage: int | None,
173  is_charging: bool | None,
174  is_wired: bool | None,
175  ) -> None:
176  """Update the battery sensor value and icon."""
177  self._attr_native_value_attr_native_value = battery_percentage
178  if battery_percentage is None:
179  # Battery percentage is unknown
180  self._attr_icon_attr_icon = "mdi:battery-unknown"
181  elif is_wired:
182  # Motor is wired and does not have a battery
183  self._attr_icon_attr_icon = "mdi:power-plug-outline"
184  elif battery_percentage > 90 and not is_charging:
185  # Full battery icon if battery > 90% and not charging
186  self._attr_icon_attr_icon = "mdi:battery"
187  elif battery_percentage <= 5 and not is_charging:
188  # Empty battery icon with alert if battery <= 5% and not charging
189  self._attr_icon_attr_icon = "mdi:battery-alert-variant-outline"
190  else:
191  battery_icon_prefix = (
192  "mdi:battery-charging" if is_charging else "mdi:battery"
193  )
194  battery_percentage_multiple_ten = ceil(battery_percentage / 10) * 10
195  self._attr_icon_attr_icon = f"{battery_icon_prefix}-{battery_percentage_multiple_ten}"
196  self.async_write_ha_stateasync_write_ha_state()
None async_update_battery(self, int|None battery_percentage, bool|None is_charging, bool|None is_wired)
Definition: sensor.py:175
None __init__(self, MotionDevice device, ConfigEntry entry)
Definition: sensor.py:153
None __init__(self, MotionDevice device, ConfigEntry entry, MotionblindsBLESensorEntityDescription[_T] entity_description)
Definition: sensor.py:123
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:99