Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Hue sensor entities."""
2 
3 from aiohue.v1.sensors import (
4  TYPE_ZLL_LIGHTLEVEL,
5  TYPE_ZLL_ROTARY,
6  TYPE_ZLL_SWITCH,
7  TYPE_ZLL_TEMPERATURE,
8 )
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorStateClass,
14 )
15 from homeassistant.const import LIGHT_LUX, PERCENTAGE, EntityCategory, UnitOfTemperature
16 
17 from ..const import DOMAIN as HUE_DOMAIN
18 from .sensor_base import SENSOR_CONFIG_MAP, GenericHueSensor, GenericZLLSensor
19 
20 LIGHT_LEVEL_NAME_FORMAT = "{} light level"
21 REMOTE_NAME_FORMAT = "{} battery level"
22 TEMPERATURE_NAME_FORMAT = "{} temperature"
23 
24 
25 async def async_setup_entry(hass, config_entry, async_add_entities):
26  """Defer sensor setup to the shared sensor module."""
27  bridge = hass.data[HUE_DOMAIN][config_entry.entry_id]
28 
29  if not bridge.sensor_manager:
30  return
31 
32  await bridge.sensor_manager.async_register_component("sensor", async_add_entities)
33 
34 
35 # pylint: disable-next=hass-enforce-class-module
37  """Parent class for all 'gauge' Hue device sensors."""
38 
39 
40 # pylint: disable-next=hass-enforce-class-module
41 class HueLightLevel(GenericHueGaugeSensorEntity):
42  """The light level sensor entity for a Hue motion sensor device."""
43 
44  _attr_device_class = SensorDeviceClass.ILLUMINANCE
45  _attr_native_unit_of_measurement = LIGHT_LUX
46 
47  @property
48  def native_value(self):
49  """Return the state of the device."""
50  if self.sensorsensor.lightlevel is None:
51  return None
52 
53  # https://developers.meethue.com/develop/hue-api/supported-devices/#clip_zll_lightlevel
54  # Light level in 10000 log10 (lux) +1 measured by sensor. Logarithm
55  # scale used because the human eye adjusts to light levels and small
56  # changes at low lux levels are more noticeable than at high lux
57  # levels.
58  return round(float(10 ** ((self.sensorsensor.lightlevel - 1) / 10000)), 2)
59 
60  @property
62  """Return the device state attributes."""
63  attributes = super().extra_state_attributes
64  attributes.update(
65  {
66  "lightlevel": self.sensorsensor.lightlevel,
67  "daylight": self.sensorsensor.daylight,
68  "dark": self.sensorsensor.dark,
69  "threshold_dark": self.sensorsensor.tholddark,
70  "threshold_offset": self.sensorsensor.tholdoffset,
71  }
72  )
73  return attributes
74 
75 
76 # pylint: disable-next=hass-enforce-class-module
78  """The temperature sensor entity for a Hue motion sensor device."""
79 
80  _attr_device_class = SensorDeviceClass.TEMPERATURE
81  _attr_state_class = SensorStateClass.MEASUREMENT
82  _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
83 
84  @property
85  def native_value(self):
86  """Return the state of the device."""
87  if self.sensorsensor.temperature is None:
88  return None
89 
90  return self.sensorsensor.temperature / 100
91 
92 
93 # pylint: disable-next=hass-enforce-class-module
95  """Battery class for when a batt-powered device is only represented as an event."""
96 
97  _attr_device_class = SensorDeviceClass.BATTERY
98  _attr_state_class = SensorStateClass.MEASUREMENT
99  _attr_native_unit_of_measurement = PERCENTAGE
100  _attr_entity_category = EntityCategory.DIAGNOSTIC
101 
102  @property
103  def unique_id(self):
104  """Return a unique identifier for this device."""
105  return f"{self.sensor.uniqueid}-battery"
106 
107  @property
108  def native_value(self):
109  """Return the state of the battery."""
110  return self.sensorsensor.battery
111 
112 
113 SENSOR_CONFIG_MAP.update(
114  {
115  TYPE_ZLL_LIGHTLEVEL: {
116  "platform": "sensor",
117  "name_format": LIGHT_LEVEL_NAME_FORMAT,
118  "class": HueLightLevel,
119  },
120  TYPE_ZLL_TEMPERATURE: {
121  "platform": "sensor",
122  "name_format": TEMPERATURE_NAME_FORMAT,
123  "class": HueTemperature,
124  },
125  TYPE_ZLL_SWITCH: {
126  "platform": "sensor",
127  "name_format": REMOTE_NAME_FORMAT,
128  "class": HueBattery,
129  },
130  TYPE_ZLL_ROTARY: {
131  "platform": "sensor",
132  "name_format": REMOTE_NAME_FORMAT,
133  "class": HueBattery,
134  },
135  }
136 )
def async_setup_entry(hass, config_entry, async_add_entities)
Definition: sensor.py:25