Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Vera sensors."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import cast
7 
8 import pyvera as veraApi
9 
11  ENTITY_ID_FORMAT,
12  SensorDeviceClass,
13  SensorEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  LIGHT_LUX,
18  PERCENTAGE,
19  Platform,
20  UnitOfPower,
21  UnitOfTemperature,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 
26 from .common import ControllerData, get_controller_data
27 from .entity import VeraEntity
28 
29 SCAN_INTERVAL = timedelta(seconds=5)
30 
31 
33  hass: HomeAssistant,
34  entry: ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up the sensor config entry."""
38  controller_data = get_controller_data(hass, entry)
40  [
41  VeraSensor(device, controller_data)
42  for device in controller_data.devices[Platform.SENSOR]
43  ],
44  True,
45  )
46 
47 
48 class VeraSensor(VeraEntity[veraApi.VeraSensor], SensorEntity):
49  """Representation of a Vera Sensor."""
50 
51  def __init__(
52  self, vera_device: veraApi.VeraSensor, controller_data: ControllerData
53  ) -> None:
54  """Initialize the sensor."""
55  self._temperature_units: str | None = None
56  self.last_changed_timelast_changed_time = None
57  VeraEntity.__init__(self, vera_device, controller_data)
58  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.vera_id)
59  if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
60  self._attr_device_class_attr_device_class = SensorDeviceClass.TEMPERATURE
61  elif self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
62  self._attr_device_class_attr_device_class = SensorDeviceClass.ILLUMINANCE
63  elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
64  self._attr_device_class_attr_device_class = SensorDeviceClass.HUMIDITY
65  elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
66  self._attr_device_class_attr_device_class = SensorDeviceClass.POWER
67  if self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
68  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = LIGHT_LUX
69  elif self.vera_device.category == veraApi.CATEGORY_UV_SENSOR:
70  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = "level"
71  elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
72  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = PERCENTAGE
73  elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
74  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UnitOfPower.WATT
75 
76  def update(self) -> None:
77  """Update the state."""
78  super().update()
79  if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
80  self._attr_native_value_attr_native_value = self.vera_device.temperature
81 
82  vera_temp_units = self.vera_device.vera_controller.temperature_units
83 
84  if vera_temp_units == "F":
85  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UnitOfTemperature.FAHRENHEIT
86  else:
87  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
88 
89  elif self.vera_device.category in (
90  veraApi.CATEGORY_LIGHT_SENSOR,
91  veraApi.CATEGORY_UV_SENSOR,
92  ):
93  self._attr_native_value_attr_native_value = self.vera_device.light
94  elif self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
95  self._attr_native_value_attr_native_value = self.vera_device.humidity
96  elif self.vera_device.category == veraApi.CATEGORY_SCENE_CONTROLLER:
97  controller = cast(veraApi.VeraSceneController, self.vera_device)
98  value = controller.get_last_scene_id(True)
99  time = controller.get_last_scene_time(True)
100  if time == self.last_changed_timelast_changed_time:
101  self._attr_native_value_attr_native_value = None
102  else:
103  self._attr_native_value_attr_native_value = value
104  self.last_changed_timelast_changed_time = time
105  elif self.vera_device.category == veraApi.CATEGORY_POWER_METER:
106  self._attr_native_value_attr_native_value = self.vera_device.power
107  elif self.vera_device.is_trippable:
108  tripped = self.vera_device.is_tripped
109  self._attr_native_value_attr_native_value = "Tripped" if tripped else "Not Tripped"
110  else:
111  self._attr_native_value_attr_native_value = "Unknown"
None __init__(self, veraApi.VeraSensor vera_device, ControllerData controller_data)
Definition: sensor.py:53
ControllerData get_controller_data(HomeAssistant hass, ConfigEntry config_entry)
Definition: common.py:40
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:36