Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ecoforest sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 
9 from pyecoforest.models.device import Alarm, Device, State
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import (
18  PERCENTAGE,
19  UnitOfPressure,
20  UnitOfTemperature,
21  UnitOfTime,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import StateType
26 
27 from .const import DOMAIN
28 from .coordinator import EcoforestCoordinator
29 from .entity import EcoforestEntity
30 
31 _LOGGER = logging.getLogger(__name__)
32 
33 STATUS_TYPE = [s.value for s in State]
34 ALARM_TYPE = [a.value for a in Alarm] + ["none"]
35 
36 
37 @dataclass(frozen=True, kw_only=True)
39  """Describes Ecoforest sensor entity."""
40 
41  value_fn: Callable[[Device], StateType]
42 
43 
44 SENSOR_TYPES: tuple[EcoforestSensorEntityDescription, ...] = (
46  key="temperature",
47  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
48  device_class=SensorDeviceClass.TEMPERATURE,
49  value_fn=lambda data: data.environment_temperature,
50  ),
52  key="cpu_temperature",
53  translation_key="cpu_temperature",
54  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
55  device_class=SensorDeviceClass.TEMPERATURE,
56  entity_registry_enabled_default=False,
57  value_fn=lambda data: data.cpu_temperature,
58  ),
60  key="gas_temperature",
61  translation_key="gas_temperature",
62  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
63  device_class=SensorDeviceClass.TEMPERATURE,
64  entity_registry_enabled_default=False,
65  value_fn=lambda data: data.gas_temperature,
66  ),
68  key="ntc_temperature",
69  translation_key="ntc_temperature",
70  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
71  device_class=SensorDeviceClass.TEMPERATURE,
72  entity_registry_enabled_default=False,
73  value_fn=lambda data: data.ntc_temperature,
74  ),
76  key="status",
77  translation_key="status",
78  device_class=SensorDeviceClass.ENUM,
79  options=STATUS_TYPE,
80  value_fn=lambda data: data.state.value,
81  ),
83  key="alarm",
84  translation_key="alarm",
85  device_class=SensorDeviceClass.ENUM,
86  options=ALARM_TYPE,
87  value_fn=lambda data: data.alarm.value if data.alarm else "none",
88  ),
90  key="depression",
91  translation_key="depression",
92  native_unit_of_measurement=UnitOfPressure.PA,
93  device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE,
94  entity_registry_enabled_default=False,
95  value_fn=lambda data: data.depression,
96  ),
98  key="working_hours",
99  translation_key="working_hours",
100  native_unit_of_measurement=UnitOfTime.HOURS,
101  device_class=SensorDeviceClass.DURATION,
102  entity_registry_enabled_default=False,
103  value_fn=lambda data: data.working_hours,
104  ),
106  key="ignitions",
107  translation_key="ignitions",
108  native_unit_of_measurement="ignitions",
109  entity_registry_enabled_default=False,
110  value_fn=lambda data: data.ignitions,
111  ),
113  key="live_pulse",
114  translation_key="live_pulse",
115  native_unit_of_measurement=UnitOfTime.SECONDS,
116  device_class=SensorDeviceClass.DURATION,
117  entity_registry_enabled_default=False,
118  value_fn=lambda data: data.live_pulse,
119  ),
121  key="pulse_offset",
122  translation_key="pulse_offset",
123  native_unit_of_measurement=UnitOfTime.SECONDS,
124  device_class=SensorDeviceClass.DURATION,
125  entity_registry_enabled_default=False,
126  value_fn=lambda data: data.pulse_offset,
127  ),
129  key="extractor",
130  translation_key="extractor",
131  native_unit_of_measurement=PERCENTAGE,
132  entity_registry_enabled_default=False,
133  value_fn=lambda data: data.extractor,
134  ),
136  key="convecto_air_flow",
137  translation_key="convecto_air_flow",
138  native_unit_of_measurement=PERCENTAGE,
139  entity_registry_enabled_default=False,
140  value_fn=lambda data: data.convecto_air_flow,
141  ),
142 )
143 
144 
146  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
147 ) -> None:
148  """Set up the Ecoforest sensor platform."""
149  coordinator: EcoforestCoordinator = hass.data[DOMAIN][entry.entry_id]
150 
151  entities = [
152  EcoforestSensor(coordinator, description) for description in SENSOR_TYPES
153  ]
154 
155  async_add_entities(entities)
156 
157 
159  """Representation of an Ecoforest sensor."""
160 
161  entity_description: EcoforestSensorEntityDescription
162 
163  @property
164  def native_value(self) -> StateType:
165  """Return the state of the sensor."""
166  return self.entity_descriptionentity_description.value_fn(self.datadatadata)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:147