Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Minut Point sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
8  DOMAIN as SENSOR_DOMAIN,
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import PERCENTAGE, UnitOfSoundPressure, UnitOfTemperature
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.dispatcher import async_dispatcher_connect
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.util.dt import parse_datetime
19 
20 from .const import DOMAIN as POINT_DOMAIN, POINT_DISCOVERY_NEW
21 from .entity import MinutPointEntity
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
28  key="temperature",
29  suggested_display_precision=1,
30  device_class=SensorDeviceClass.TEMPERATURE,
31  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
32  ),
34  key="humidity",
35  suggested_display_precision=1,
36  device_class=SensorDeviceClass.HUMIDITY,
37  native_unit_of_measurement=PERCENTAGE,
38  ),
40  key="sound",
41  suggested_display_precision=1,
42  device_class=SensorDeviceClass.SOUND_PRESSURE,
43  native_unit_of_measurement=UnitOfSoundPressure.WEIGHTED_DECIBEL_A,
44  ),
45 )
46 
47 
49  hass: HomeAssistant,
50  config_entry: ConfigEntry,
51  async_add_entities: AddEntitiesCallback,
52 ) -> None:
53  """Set up a Point's sensors based on a config entry."""
54 
55  async def async_discover_sensor(device_id):
56  """Discover and add a discovered sensor."""
57  client = config_entry.runtime_data.client
59  [
60  MinutPointSensor(client, device_id, description)
61  for description in SENSOR_TYPES
62  ],
63  True,
64  )
65 
67  hass,
68  POINT_DISCOVERY_NEW.format(SENSOR_DOMAIN, POINT_DOMAIN),
69  async_discover_sensor,
70  )
71 
72 
74  """The platform class required by Home Assistant."""
75 
76  def __init__(
77  self, point_client, device_id, description: SensorEntityDescription
78  ) -> None:
79  """Initialize the sensor."""
80  super().__init__(point_client, device_id, description.device_class)
81  self.entity_descriptionentity_description = description
82 
83  async def _update_callback(self):
84  """Update the value of the sensor."""
85  _LOGGER.debug("Update sensor value for %s", self)
86  if self.is_updatedis_updated:
87  self._attr_native_value_attr_native_value = await self.devicedevice.sensor(self.device_classdevice_classdevice_class)
88  self._updated_updated_updated = parse_datetime(self.devicedevice.last_update)
89  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, point_client, device_id, SensorEntityDescription description)
Definition: sensor.py:78
SensorDeviceClass|None device_class(self)
Definition: __init__.py:313
datetime|None parse_datetime(str|None value)
Definition: sensor.py:138
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:52
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103