Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Allows reading temperatures from ecoal/esterownik.pl controller."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
6 from homeassistant.const import UnitOfTemperature
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
10 
11 from . import AVAILABLE_SENSORS, DATA_ECOAL_BOILER
12 
13 
15  hass: HomeAssistant,
16  config: ConfigType,
17  add_entities: AddEntitiesCallback,
18  discovery_info: DiscoveryInfoType | None = None,
19 ) -> None:
20  """Set up the ecoal sensors."""
21  if discovery_info is None:
22  return
23  devices = []
24  ecoal_contr = hass.data[DATA_ECOAL_BOILER]
25  for sensor_id in discovery_info:
26  name = AVAILABLE_SENSORS[sensor_id]
27  devices.append(EcoalTempSensor(ecoal_contr, name, sensor_id))
28  add_entities(devices, True)
29 
30 
32  """Representation of a temperature sensor using ecoal status data."""
33 
34  _attr_device_class = SensorDeviceClass.TEMPERATURE
35  _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
36 
37  def __init__(self, ecoal_contr, name, status_attr):
38  """Initialize the sensor."""
39  self._ecoal_contr_ecoal_contr = ecoal_contr
40  self._attr_name_attr_name = name
41  self._status_attr_status_attr = status_attr
42 
43  def update(self) -> None:
44  """Fetch new state data for the sensor.
45 
46  This is the only method that should fetch new data for Home Assistant.
47  """
48  # Old values read 0.5 back can still be used
49  status = self._ecoal_contr_ecoal_contr.get_cached_status()
50  self._attr_native_value_attr_native_value = getattr(status, self._status_attr_status_attr)
def __init__(self, ecoal_contr, name, status_attr)
Definition: sensor.py:37
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:19
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40