Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform support for wiffi devices."""
2 
4  SensorDeviceClass,
5  SensorEntity,
6  SensorStateClass,
7 )
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import DEGREE, LIGHT_LUX, UnitOfPressure, UnitOfTemperature
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers.dispatcher import async_dispatcher_connect
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from .const import CREATE_ENTITY_SIGNAL
15 from .entity import WiffiEntity
16 from .wiffi_strings import (
17  WIFFI_UOM_DEGREE,
18  WIFFI_UOM_LUX,
19  WIFFI_UOM_MILLI_BAR,
20  WIFFI_UOM_PERCENT,
21  WIFFI_UOM_TEMP_CELSIUS,
22 )
23 
24 # map to determine HA device class from wiffi's unit of measurement
25 UOM_TO_DEVICE_CLASS_MAP = {
26  WIFFI_UOM_TEMP_CELSIUS: SensorDeviceClass.TEMPERATURE,
27  WIFFI_UOM_PERCENT: SensorDeviceClass.HUMIDITY,
28  WIFFI_UOM_MILLI_BAR: SensorDeviceClass.PRESSURE,
29  WIFFI_UOM_LUX: SensorDeviceClass.ILLUMINANCE,
30 }
31 
32 # map to convert wiffi unit of measurements to common HA uom's
33 UOM_MAP = {
34  WIFFI_UOM_DEGREE: DEGREE,
35  WIFFI_UOM_TEMP_CELSIUS: UnitOfTemperature.CELSIUS,
36  WIFFI_UOM_MILLI_BAR: UnitOfPressure.MBAR,
37  WIFFI_UOM_LUX: LIGHT_LUX,
38 }
39 
40 
42  hass: HomeAssistant,
43  config_entry: ConfigEntry,
44  async_add_entities: AddEntitiesCallback,
45 ) -> None:
46  """Set up platform for a new integration.
47 
48  Called by the HA framework after async_forward_entry_setups has been called
49  during initialization of a new integration (= wiffi).
50  """
51 
52  @callback
53  def _create_entity(device, metric):
54  """Create platform specific entities."""
55  entities = []
56 
57  if metric.is_number:
58  entities.append(NumberEntity(device, metric, config_entry.options))
59  elif metric.is_string:
60  entities.append(StringEntity(device, metric, config_entry.options))
61 
62  async_add_entities(entities)
63 
64  async_dispatcher_connect(hass, CREATE_ENTITY_SIGNAL, _create_entity)
65 
66 
68  """Entity for wiffi metrics which have a number value."""
69 
70  def __init__(self, device, metric, options):
71  """Initialize the entity."""
72  super().__init__(device, metric, options)
73  self._attr_device_class_attr_device_class = UOM_TO_DEVICE_CLASS_MAP.get(
74  metric.unit_of_measurement
75  )
76  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UOM_MAP.get(
77  metric.unit_of_measurement, metric.unit_of_measurement
78  )
79  self._attr_native_value_attr_native_value = metric.value
80 
81  if self._is_measurement_entity_is_measurement_entity():
82  self._attr_state_class_attr_state_class = SensorStateClass.MEASUREMENT
83  elif self._is_metered_entity_is_metered_entity():
84  self._attr_state_class_attr_state_class = SensorStateClass.TOTAL_INCREASING
85 
86  self.reset_expiration_datereset_expiration_date()
87 
88  @property
89  def available(self):
90  """Return true if value is valid."""
91  return self._attr_native_value_attr_native_value is not None
92 
93  @callback
94  def _update_value_callback(self, device, metric):
95  """Update the value of the entity.
96 
97  Called if a new message has been received from the wiffi device.
98  """
99  self.reset_expiration_datereset_expiration_date()
100  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = UOM_MAP.get(
101  metric.unit_of_measurement, metric.unit_of_measurement
102  )
103 
104  self._attr_native_value_attr_native_value = metric.value
105 
106  self.async_write_ha_stateasync_write_ha_state()
107 
108 
110  """Entity for wiffi metrics which have a string value."""
111 
112  def __init__(self, device, metric, options):
113  """Initialize the entity."""
114  super().__init__(device, metric, options)
115  self._attr_native_value_attr_native_value = metric.value
116  self.reset_expiration_datereset_expiration_date()
117 
118  @property
119  def available(self):
120  """Return true if value is valid."""
121  return self._attr_native_value_attr_native_value is not None
122 
123  @callback
124  def _update_value_callback(self, device, metric):
125  """Update the value of the entity.
126 
127  Called if a new message has been received from the wiffi device.
128  """
129  self.reset_expiration_datereset_expiration_date()
130  self._attr_native_value_attr_native_value = metric.value
131  self.async_write_ha_stateasync_write_ha_state()
def __init__(self, device, metric, options)
Definition: sensor.py:70
def _update_value_callback(self, device, metric)
Definition: sensor.py:94
def _update_value_callback(self, device, metric)
Definition: sensor.py:124
def __init__(self, device, metric, options)
Definition: sensor.py:112
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:45
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103