Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Salda Smarty XP/XV Ventilation Unit Sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime, timedelta
8 import logging
9 
10 from pysmarty2 import Smarty
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16 )
17 from homeassistant.const import REVOLUTIONS_PER_MINUTE, UnitOfTemperature
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 import homeassistant.util.dt as dt_util
21 
22 from .coordinator import SmartyConfigEntry, SmartyCoordinator
23 from .entity import SmartyEntity
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 def get_filter_days_left(smarty: Smarty) -> datetime | None:
29  """Return the date when the filter needs to be replaced."""
30  if (days_left := smarty.filter_timer) is not None:
31  return dt_util.now() + timedelta(days=days_left)
32  return None
33 
34 
35 @dataclass(frozen=True, kw_only=True)
37  """Class describing Smarty sensor."""
38 
39  value_fn: Callable[[Smarty], float | datetime | None]
40 
41 
42 ENTITIES: tuple[SmartySensorDescription, ...] = (
44  key="supply_air_temperature",
45  translation_key="supply_air_temperature",
46  device_class=SensorDeviceClass.TEMPERATURE,
47  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
48  value_fn=lambda smarty: smarty.supply_air_temperature,
49  ),
51  key="extract_air_temperature",
52  translation_key="extract_air_temperature",
53  device_class=SensorDeviceClass.TEMPERATURE,
54  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
55  value_fn=lambda smarty: smarty.extract_air_temperature,
56  ),
58  key="outdoor_air_temperature",
59  translation_key="outdoor_air_temperature",
60  device_class=SensorDeviceClass.TEMPERATURE,
61  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
62  value_fn=lambda smarty: smarty.outdoor_air_temperature,
63  ),
65  key="supply_fan_speed",
66  translation_key="supply_fan_speed",
67  native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
68  value_fn=lambda smarty: smarty.supply_fan_speed,
69  ),
71  key="extract_fan_speed",
72  translation_key="extract_fan_speed",
73  native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
74  value_fn=lambda smarty: smarty.extract_fan_speed,
75  ),
77  key="filter_days_left",
78  translation_key="filter_days_left",
79  device_class=SensorDeviceClass.TIMESTAMP,
80  value_fn=get_filter_days_left,
81  ),
82 )
83 
84 
86  hass: HomeAssistant,
87  entry: SmartyConfigEntry,
88  async_add_entities: AddEntitiesCallback,
89 ) -> None:
90  """Set up the Smarty Sensor Platform."""
91 
92  coordinator = entry.runtime_data
93 
95  SmartySensor(coordinator, description) for description in ENTITIES
96  )
97 
98 
100  """Representation of a Smarty Sensor."""
101 
102  entity_description: SmartySensorDescription
103 
104  def __init__(
105  self,
106  coordinator: SmartyCoordinator,
107  entity_description: SmartySensorDescription,
108  ) -> None:
109  """Initialize the entity."""
110  super().__init__(coordinator)
111  self.entity_descriptionentity_description = entity_description
112  self._attr_unique_id_attr_unique_id = (
113  f"{coordinator.config_entry.entry_id}_{entity_description.key}"
114  )
115 
116  @property
117  def native_value(self) -> float | datetime | None:
118  """Return the state of the sensor."""
119  return self.entity_descriptionentity_description.value_fn(self.coordinator.client)
None __init__(self, SmartyCoordinator coordinator, SmartySensorDescription entity_description)
Definition: sensor.py:108
datetime|None get_filter_days_left(Smarty smarty)
Definition: sensor.py:28
None async_setup_entry(HomeAssistant hass, SmartyConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:89