Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for sensor integration."""
2 
3 import logging
4 
5 from laundrify_aio import LaundrifyDevice
6 from laundrify_aio.exceptions import LaundrifyDeviceException
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorStateClass,
12 )
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import UnitOfEnergy, UnitOfPower
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.device_registry import DeviceInfo
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.update_coordinator import CoordinatorEntity
19 
20 from .const import DOMAIN
21 from .coordinator import LaundrifyUpdateCoordinator
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
27  hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
28 ) -> None:
29  """Add power sensor for passed config_entry in HA."""
30 
31  coordinator: LaundrifyUpdateCoordinator = hass.data[DOMAIN][config.entry_id][
32  "coordinator"
33  ]
34 
35  sensor_entities: list[LaundrifyPowerSensor | LaundrifyEnergySensor] = []
36  for device in coordinator.data.values():
37  sensor_entities.append(LaundrifyPowerSensor(device))
38  sensor_entities.append(LaundrifyEnergySensor(coordinator, device))
39 
40  async_add_entities(sensor_entities)
41 
42 
44  """Base class for Laundrify sensors."""
45 
46  _attr_has_entity_name = True
47 
48  def __init__(self, device: LaundrifyDevice) -> None:
49  """Initialize the sensor."""
50  self._device_device = device
51  self._attr_device_info_attr_device_info = DeviceInfo(identifiers={(DOMAIN, device.id)})
52  self._attr_unique_id_attr_unique_id = f"{device.id}_{self._attr_device_class}"
53 
54 
56  """Representation of a Power sensor."""
57 
58  _attr_device_class = SensorDeviceClass.POWER
59  _attr_native_unit_of_measurement = UnitOfPower.WATT
60  _attr_state_class = SensorStateClass.MEASUREMENT
61  _attr_suggested_display_precision = 0
62 
63  async def async_update(self) -> None:
64  """Fetch latest power measurement from the device."""
65  try:
66  power = await self._device_device.get_power()
67  except LaundrifyDeviceException as err:
68  _LOGGER.debug("Couldn't load power for %s: %s", self._attr_unique_id_attr_unique_id, err)
69  self._attr_available_attr_available = False
70  else:
71  _LOGGER.debug("Retrieved power for %s: %s", self._attr_unique_id_attr_unique_id, power)
72  if power is not None:
73  self._attr_available_attr_available = True
74  self._attr_native_value_attr_native_value = power
75 
76 
78  CoordinatorEntity[LaundrifyUpdateCoordinator], LaundrifyBaseSensor
79 ):
80  """Representation of an Energy sensor."""
81 
82  _attr_device_class = SensorDeviceClass.ENERGY
83  _attr_native_unit_of_measurement = UnitOfEnergy.WATT_HOUR
84  _attr_state_class = SensorStateClass.TOTAL
85  _attr_suggested_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
86  _attr_suggested_display_precision = 2
87 
88  def __init__(
89  self, coordinator: LaundrifyUpdateCoordinator, device: LaundrifyDevice
90  ) -> None:
91  """Initialize the sensor."""
92  CoordinatorEntity.__init__(self, coordinator)
93  LaundrifyBaseSensor.__init__(self, device)
94 
95  @property
96  def native_value(self) -> float:
97  """Return the total energy of the device."""
98  device = self.coordinator.data[self._device_device.id]
99  return float(device.totalEnergy)
None __init__(self, LaundrifyDevice device)
Definition: sensor.py:48
None __init__(self, LaundrifyUpdateCoordinator coordinator, LaundrifyDevice device)
Definition: sensor.py:90
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)
Definition: sensor.py:28