Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Palazzetti sensors."""
2 
3 from dataclasses import dataclass
4 
6  SensorDeviceClass,
7  SensorEntity,
8  SensorEntityDescription,
9  SensorStateClass,
10 )
11 from homeassistant.const import UnitOfLength, UnitOfMass, UnitOfTemperature
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.typing import StateType
15 
16 from . import PalazzettiConfigEntry
17 from .coordinator import PalazzettiDataUpdateCoordinator
18 from .entity import PalazzettiEntity
19 
20 
21 @dataclass(frozen=True, kw_only=True)
23  """Describes a Palazzetti sensor entity that is read from a `PalazzettiClient` property."""
24 
25  client_property: str
26  presence_flag: None | str = None
27 
28 
29 PROPERTY_SENSOR_DESCRIPTIONS: list[PropertySensorEntityDescription] = [
31  key="pellet_quantity",
32  device_class=SensorDeviceClass.WEIGHT,
33  native_unit_of_measurement=UnitOfMass.KILOGRAMS,
34  state_class=SensorStateClass.MEASUREMENT,
35  translation_key="pellet_quantity",
36  client_property="pellet_quantity",
37  ),
39  key="pellet_level",
40  device_class=SensorDeviceClass.DISTANCE,
41  native_unit_of_measurement=UnitOfLength.CENTIMETERS,
42  state_class=SensorStateClass.MEASUREMENT,
43  translation_key="pellet_level",
44  presence_flag="has_pellet_level",
45  client_property="pellet_level",
46  ),
47 ]
48 
49 
51  hass: HomeAssistant,
52  entry: PalazzettiConfigEntry,
53  async_add_entities: AddEntitiesCallback,
54 ) -> None:
55  """Set up Palazzetti sensor entities based on a config entry."""
56 
57  coordinator = entry.runtime_data
58 
59  sensors = [
61  coordinator,
63  key=sensor.description_key.value,
64  device_class=SensorDeviceClass.TEMPERATURE,
65  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66  state_class=SensorStateClass.MEASUREMENT,
67  translation_key=sensor.description_key.value,
68  client_property=sensor.state_property,
69  ),
70  )
71  for sensor in coordinator.client.list_temperatures()
72  ]
73 
74  sensors.extend(
75  [
76  PalazzettiSensor(coordinator, description)
77  for description in PROPERTY_SENSOR_DESCRIPTIONS
78  if not description.presence_flag
79  or getattr(coordinator.client, description.presence_flag)
80  ]
81  )
82 
83  if sensors:
84  async_add_entities(sensors)
85 
86 
88  """Define a Palazzetti sensor."""
89 
90  entity_description: PropertySensorEntityDescription
91 
92  def __init__(
93  self,
94  coordinator: PalazzettiDataUpdateCoordinator,
95  description: PropertySensorEntityDescription,
96  ) -> None:
97  """Initialize Palazzetti sensor."""
98  super().__init__(coordinator)
99  self.entity_descriptionentity_description = description
100  self._attr_unique_id_attr_unique_id = f"{coordinator.config_entry.unique_id}-{description.key}"
101 
102  @property
103  def native_value(self) -> StateType:
104  """Return the state value of the sensor."""
105 
106  return getattr(self.coordinator.client, self.entity_descriptionentity_description.client_property)
None __init__(self, PalazzettiDataUpdateCoordinator coordinator, PropertySensorEntityDescription description)
Definition: sensor.py:96
None async_setup_entry(HomeAssistant hass, PalazzettiConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:54