Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for BSB-Lan sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import UnitOfTemperature
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from . import BSBLanConfigEntry, BSBLanData
20 from .coordinator import BSBLanCoordinatorData
21 from .entity import BSBLanEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Describes BSB-Lan sensor entity."""
27 
28  value_fn: Callable[[BSBLanCoordinatorData], StateType]
29 
30 
31 SENSOR_TYPES: tuple[BSBLanSensorEntityDescription, ...] = (
33  key="current_temperature",
34  translation_key="current_temperature",
35  device_class=SensorDeviceClass.TEMPERATURE,
36  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
37  state_class=SensorStateClass.MEASUREMENT,
38  value_fn=lambda data: data.sensor.current_temperature.value,
39  ),
41  key="outside_temperature",
42  translation_key="outside_temperature",
43  device_class=SensorDeviceClass.TEMPERATURE,
44  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
45  state_class=SensorStateClass.MEASUREMENT,
46  value_fn=lambda data: data.sensor.outside_temperature.value,
47  ),
48 )
49 
50 
52  hass: HomeAssistant,
53  entry: BSBLanConfigEntry,
54  async_add_entities: AddEntitiesCallback,
55 ) -> None:
56  """Set up BSB-Lan sensor based on a config entry."""
57  data = entry.runtime_data
58  async_add_entities(BSBLanSensor(data, description) for description in SENSOR_TYPES)
59 
60 
62  """Defines a BSB-Lan sensor."""
63 
64  entity_description: BSBLanSensorEntityDescription
65 
66  def __init__(
67  self,
68  data: BSBLanData,
69  description: BSBLanSensorEntityDescription,
70  ) -> None:
71  """Initialize BSB-Lan sensor."""
72  super().__init__(data.coordinator, data)
73  self.entity_descriptionentity_description = description
74  self._attr_unique_id_attr_unique_id = f"{data.device.MAC}-{description.key}"
75  self._attr_temperature_unit_attr_temperature_unit = data.coordinator.client.get_temperature_unit
76 
77  @property
78  def native_value(self) -> StateType:
79  """Return the state of the sensor."""
80  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None __init__(self, BSBLanData data, BSBLanSensorEntityDescription description)
Definition: sensor.py:70
None async_setup_entry(HomeAssistant hass, BSBLanConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:55