Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for the opengarage.io sensor component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import cast
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import (
16  PERCENTAGE,
17  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
18  EntityCategory,
19  UnitOfLength,
20  UnitOfTemperature,
21 )
22 from homeassistant.core import HomeAssistant, callback
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from .const import DOMAIN
26 from .coordinator import OpenGarageDataUpdateCoordinator
27 from .entity import OpenGarageEntity
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
33  key="dist",
34  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
35  device_class=SensorDeviceClass.DISTANCE,
36  state_class=SensorStateClass.MEASUREMENT,
37  ),
39  key="rssi",
40  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
41  entity_category=EntityCategory.DIAGNOSTIC,
42  entity_registry_enabled_default=False,
43  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
44  state_class=SensorStateClass.MEASUREMENT,
45  ),
47  key="temp",
48  device_class=SensorDeviceClass.TEMPERATURE,
49  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
50  state_class=SensorStateClass.MEASUREMENT,
51  ),
53  key="humid",
54  device_class=SensorDeviceClass.HUMIDITY,
55  native_unit_of_measurement=PERCENTAGE,
56  state_class=SensorStateClass.MEASUREMENT,
57  ),
58 )
59 
60 
62  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
63 ) -> None:
64  """Set up the OpenGarage sensors."""
65  open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
66  entry.entry_id
67  ]
70  open_garage_data_coordinator,
71  cast(str, entry.unique_id),
72  description,
73  )
74  for description in SENSOR_TYPES
75  if description.key in open_garage_data_coordinator.data
76  )
77 
78 
80  """Representation of a OpenGarage sensor."""
81 
82  @callback
83  def _update_attr(self) -> None:
84  """Handle updated data from the coordinator."""
85  self._attr_native_value_attr_native_value = self.coordinator.data.get(self.entity_descriptionentity_description.key)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:63