Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """The sensor platform for the A. O. Smith integration."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from py_aosmith.models import Device as AOSmithDevice
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import PERCENTAGE, UnitOfEnergy
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import AOSmithConfigEntry
19 from .coordinator import AOSmithEnergyCoordinator, AOSmithStatusCoordinator
20 from .entity import AOSmithEnergyEntity, AOSmithStatusEntity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Entity description class for sensors using data from the status coordinator."""
26 
27  value_fn: Callable[[AOSmithDevice], str | int | None]
28 
29 
30 STATUS_ENTITY_DESCRIPTIONS: tuple[AOSmithStatusSensorEntityDescription, ...] = (
32  key="hot_water_availability",
33  translation_key="hot_water_availability",
34  native_unit_of_measurement=PERCENTAGE,
35  value_fn=lambda device: device.status.hot_water_status,
36  ),
37 )
38 
39 
41  hass: HomeAssistant,
42  entry: AOSmithConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Set up A. O. Smith sensor platform."""
46  data = entry.runtime_data
47 
49  AOSmithStatusSensorEntity(data.status_coordinator, description, junction_id)
50  for description in STATUS_ENTITY_DESCRIPTIONS
51  for junction_id in data.status_coordinator.data
52  )
53 
55  AOSmithEnergySensorEntity(data.energy_coordinator, junction_id)
56  for junction_id in data.status_coordinator.data
57  )
58 
59 
61  """Class for sensor entities that use data from the status coordinator."""
62 
63  entity_description: AOSmithStatusSensorEntityDescription
64 
65  def __init__(
66  self,
67  coordinator: AOSmithStatusCoordinator,
68  description: AOSmithStatusSensorEntityDescription,
69  junction_id: str,
70  ) -> None:
71  """Initialize the entity."""
72  super().__init__(coordinator, junction_id)
73  self.entity_descriptionentity_description = description
74  self._attr_unique_id_attr_unique_id = f"{description.key}_{junction_id}"
75 
76  @property
77  def native_value(self) -> str | int | None:
78  """Return the state of the sensor."""
79  return self.entity_descriptionentity_description.value_fn(self.devicedevice)
80 
81 
83  """Class for the energy sensor entity."""
84 
85  _attr_translation_key = "energy_usage"
86  _attr_device_class = SensorDeviceClass.ENERGY
87  _attr_state_class = SensorStateClass.TOTAL_INCREASING
88  _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
89  _attr_suggested_display_precision = 1
90 
91  def __init__(
92  self,
93  coordinator: AOSmithEnergyCoordinator,
94  junction_id: str,
95  ) -> None:
96  """Initialize the entity."""
97  super().__init__(coordinator, junction_id)
98  self._attr_unique_id_attr_unique_id = f"energy_usage_{junction_id}"
99 
100  @property
101  def native_value(self) -> float | None:
102  """Return the state of the sensor."""
103  return self.energy_usageenergy_usage
None __init__(self, AOSmithEnergyCoordinator coordinator, str junction_id)
Definition: sensor.py:95
None __init__(self, AOSmithStatusCoordinator coordinator, AOSmithStatusSensorEntityDescription description, str junction_id)
Definition: sensor.py:70
None async_setup_entry(HomeAssistant hass, AOSmithConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:44