Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Hue sensors."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 from typing import Any
7 
8 from aiohue.v2 import HueBridgeV2
9 from aiohue.v2.controllers.events import EventType
10 from aiohue.v2.controllers.sensors import (
11  DevicePowerController,
12  LightLevelController,
13  SensorsController,
14  TemperatureController,
15  ZigbeeConnectivityController,
16 )
17 from aiohue.v2.models.device_power import DevicePower
18 from aiohue.v2.models.light_level import LightLevel
19 from aiohue.v2.models.temperature import Temperature
20 from aiohue.v2.models.zigbee_connectivity import ZigbeeConnectivity
21 
23  SensorDeviceClass,
24  SensorEntity,
25  SensorEntityDescription,
26  SensorStateClass,
27 )
28 from homeassistant.config_entries import ConfigEntry
29 from homeassistant.const import LIGHT_LUX, PERCENTAGE, EntityCategory, UnitOfTemperature
30 from homeassistant.core import HomeAssistant, callback
31 from homeassistant.helpers.entity_platform import AddEntitiesCallback
32 
33 from ..bridge import HueBridge
34 from ..const import DOMAIN
35 from .entity import HueBaseEntity
36 
37 type SensorType = DevicePower | LightLevel | Temperature | ZigbeeConnectivity
38 type ControllerType = (
39  DevicePowerController
40  | LightLevelController
41  | TemperatureController
42  | ZigbeeConnectivityController
43 )
44 
45 
47  hass: HomeAssistant,
48  config_entry: ConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up Hue Sensors from Config Entry."""
52  bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
53  api: HueBridgeV2 = bridge.api
54  ctrl_base: SensorsController = api.sensors
55 
56  @callback
57  def register_items(controller: ControllerType, sensor_class: SensorType):
58  make_sensor_entity = partial(sensor_class, bridge, controller)
59 
60  @callback
61  def async_add_sensor(event_type: EventType, resource: SensorType) -> None:
62  """Add Hue Sensor."""
63  async_add_entities([make_sensor_entity(resource)])
64 
65  # add all current items in controller
66  async_add_entities(make_sensor_entity(sensor) for sensor in controller)
67 
68  # register listener for new sensors
69  config_entry.async_on_unload(
70  controller.subscribe(
71  async_add_sensor, event_filter=EventType.RESOURCE_ADDED
72  )
73  )
74 
75  # setup for each sensor-type hue resource
76  register_items(ctrl_base.temperature, HueTemperatureSensor)
77  register_items(ctrl_base.light_level, HueLightLevelSensor)
78  register_items(ctrl_base.device_power, HueBatterySensor)
79  register_items(ctrl_base.zigbee_connectivity, HueZigbeeConnectivitySensor)
80 
81 
82 # pylint: disable-next=hass-enforce-class-module
84  """Representation of a Hue sensor."""
85 
86  def __init__(
87  self,
88  bridge: HueBridge,
89  controller: ControllerType,
90  resource: SensorType,
91  ) -> None:
92  """Initialize the light."""
93  super().__init__(bridge, controller, resource)
94  self.resourceresourceresource = resource
95  self.controllercontrollercontroller = controller
96 
97 
98 # pylint: disable-next=hass-enforce-class-module
100  """Representation of a Hue Temperature sensor."""
101 
102  entity_description = SensorEntityDescription(
103  key="temperature_sensor",
104  device_class=SensorDeviceClass.TEMPERATURE,
105  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
106  has_entity_name=True,
107  state_class=SensorStateClass.MEASUREMENT,
108  )
109 
110  @property
111  def native_value(self) -> float:
112  """Return the value reported by the sensor."""
113  return round(self.resourceresourceresource.temperature.value, 1)
114 
115 
116 # pylint: disable-next=hass-enforce-class-module
118  """Representation of a Hue LightLevel (illuminance) sensor."""
119 
120  entity_description = SensorEntityDescription(
121  key="lightlevel_sensor",
122  device_class=SensorDeviceClass.ILLUMINANCE,
123  native_unit_of_measurement=LIGHT_LUX,
124  has_entity_name=True,
125  state_class=SensorStateClass.MEASUREMENT,
126  )
127 
128  @property
129  def native_value(self) -> int:
130  """Return the value reported by the sensor."""
131  # Light level in 10000 log10 (lux) +1 measured by sensor. Logarithm
132  # scale used because the human eye adjusts to light levels and small
133  # changes at low lux levels are more noticeable than at high lux
134  # levels.
135  return int(10 ** ((self.resourceresourceresource.light.value - 1) / 10000))
136 
137  @property
138  def extra_state_attributes(self) -> dict[str, Any]:
139  """Return the optional state attributes."""
140  return {
141  "light_level": self.resourceresourceresource.light.value,
142  }
143 
144 
145 # pylint: disable-next=hass-enforce-class-module
147  """Representation of a Hue Battery sensor."""
148 
149  entity_description = SensorEntityDescription(
150  key="battery_sensor",
151  device_class=SensorDeviceClass.BATTERY,
152  native_unit_of_measurement=PERCENTAGE,
153  has_entity_name=True,
154  state_class=SensorStateClass.MEASUREMENT,
155  entity_category=EntityCategory.DIAGNOSTIC,
156  )
157 
158  @property
159  def native_value(self) -> int:
160  """Return the value reported by the sensor."""
161  return self.resourceresourceresource.power_state.battery_level
162 
163  @property
164  def extra_state_attributes(self) -> dict[str, Any]:
165  """Return the optional state attributes."""
166  if self.resourceresourceresource.power_state.battery_state is None:
167  return {}
168  return {"battery_state": self.resourceresourceresource.power_state.battery_state.value}
169 
170 
171 # pylint: disable-next=hass-enforce-class-module
173  """Representation of a Hue ZigbeeConnectivity sensor."""
174 
175  entity_description = SensorEntityDescription(
176  key="zigbee_connectivity_sensor",
177  device_class=SensorDeviceClass.ENUM,
178  has_entity_name=True,
179  entity_category=EntityCategory.DIAGNOSTIC,
180  translation_key="zigbee_connectivity",
181  options=[
182  "connected",
183  "disconnected",
184  "connectivity_issue",
185  "unidirectional_incoming",
186  ],
187  entity_registry_enabled_default=False,
188  )
189 
190  @property
191  def native_value(self) -> str:
192  """Return the value reported by the sensor."""
193  return self.resourceresourceresource.status.value
194 
195  @property
196  def extra_state_attributes(self) -> dict[str, Any]:
197  """Return the optional state attributes."""
198  return {"mac_address": self.resourceresourceresource.mac_address}
None __init__(self, HueBridge bridge, ControllerType controller, SensorType resource)
Definition: sensor.py:91
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:50