Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for OSO Energy sensors."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from apyosoenergyapi import OSOEnergy
7 from apyosoenergyapi.helper.const import OSOEnergySensorData
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  UnitOfEnergy,
18  UnitOfPower,
19  UnitOfTemperature,
20  UnitOfVolume,
21 )
22 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.typing import StateType
25 
26 from .const import DOMAIN
27 from .entity import OSOEnergyEntity
28 
29 
30 @dataclass(frozen=True, kw_only=True)
32  """Class describing OSO Energy heater sensor entities."""
33 
34  value_fn: Callable[[OSOEnergy], StateType]
35 
36 
37 SENSOR_TYPES: dict[str, OSOEnergySensorEntityDescription] = {
38  "heater_mode": OSOEnergySensorEntityDescription(
39  key="heater_mode",
40  translation_key="heater_mode",
41  device_class=SensorDeviceClass.ENUM,
42  options=[
43  "auto",
44  "manual",
45  "off",
46  "legionella",
47  "powersave",
48  "extraenergy",
49  "voltage",
50  "ffr",
51  ],
52  value_fn=lambda entity_data: entity_data.state.lower(),
53  ),
54  "optimization_mode": OSOEnergySensorEntityDescription(
55  key="optimization_mode",
56  translation_key="optimization_mode",
57  device_class=SensorDeviceClass.ENUM,
58  options=["off", "oso", "gridcompany", "smartcompany", "advanced"],
59  value_fn=lambda entity_data: entity_data.state.lower(),
60  ),
62  key="power_load",
63  device_class=SensorDeviceClass.POWER,
64  state_class=SensorStateClass.MEASUREMENT,
65  native_unit_of_measurement=UnitOfPower.KILO_WATT,
66  value_fn=lambda entity_data: entity_data.state,
67  ),
68  "tapping_capacity": OSOEnergySensorEntityDescription(
69  key="tapping_capacity",
70  translation_key="tapping_capacity",
71  device_class=SensorDeviceClass.ENERGY,
72  native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
73  value_fn=lambda entity_data: entity_data.state,
74  ),
75  "capacity_mixed_water_40": OSOEnergySensorEntityDescription(
76  key="capacity_mixed_water_40",
77  translation_key="capacity_mixed_water_40",
78  device_class=SensorDeviceClass.VOLUME,
79  native_unit_of_measurement=UnitOfVolume.LITERS,
80  value_fn=lambda entity_data: entity_data.state,
81  ),
83  key="v40_min",
84  translation_key="v40_min",
85  device_class=SensorDeviceClass.VOLUME,
86  native_unit_of_measurement=UnitOfVolume.LITERS,
87  value_fn=lambda entity_data: entity_data.state,
88  ),
89  "v40_level_min": OSOEnergySensorEntityDescription(
90  key="v40_level_min",
91  translation_key="v40_level_min",
92  device_class=SensorDeviceClass.VOLUME,
93  native_unit_of_measurement=UnitOfVolume.LITERS,
94  value_fn=lambda entity_data: entity_data.state,
95  ),
96  "v40_level_max": OSOEnergySensorEntityDescription(
97  key="v40_level_max",
98  translation_key="v40_level_max",
99  device_class=SensorDeviceClass.VOLUME,
100  native_unit_of_measurement=UnitOfVolume.LITERS,
101  value_fn=lambda entity_data: entity_data.state,
102  ),
104  key="volume",
105  device_class=SensorDeviceClass.VOLUME,
106  native_unit_of_measurement=UnitOfVolume.LITERS,
107  value_fn=lambda entity_data: entity_data.state,
108  ),
109  "temperature_top": OSOEnergySensorEntityDescription(
110  key="temperature_top",
111  translation_key="temperature_top",
112  device_class=SensorDeviceClass.TEMPERATURE,
113  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
114  value_fn=lambda entity_data: entity_data.state,
115  ),
116  "temperature_mid": OSOEnergySensorEntityDescription(
117  key="temperature_mid",
118  translation_key="temperature_mid",
119  device_class=SensorDeviceClass.TEMPERATURE,
120  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
121  value_fn=lambda entity_data: entity_data.state,
122  ),
123  "temperature_low": OSOEnergySensorEntityDescription(
124  key="temperature_low",
125  translation_key="temperature_low",
126  device_class=SensorDeviceClass.TEMPERATURE,
127  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
128  value_fn=lambda entity_data: entity_data.state,
129  ),
130  "temperature_one": OSOEnergySensorEntityDescription(
131  key="temperature_one",
132  translation_key="temperature_one",
133  device_class=SensorDeviceClass.TEMPERATURE,
134  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
135  value_fn=lambda entity_data: entity_data.state,
136  ),
137 }
138 
139 
141  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
142 ) -> None:
143  """Set up OSO Energy sensor."""
144  osoenergy = hass.data[DOMAIN][entry.entry_id]
145  devices = osoenergy.session.device_list.get("sensor")
146  entities = []
147  if devices:
148  for dev in devices:
149  sensor_type = dev.osoEnergyType.lower()
150  if sensor_type in SENSOR_TYPES:
151  entities.append(
152  OSOEnergySensor(osoenergy, SENSOR_TYPES[sensor_type], dev)
153  )
154 
155  async_add_entities(entities, True)
156 
157 
158 class OSOEnergySensor(OSOEnergyEntity[OSOEnergySensorData], SensorEntity):
159  """OSO Energy Sensor Entity."""
160 
161  entity_description: OSOEnergySensorEntityDescription
162 
163  def __init__(
164  self,
165  instance: OSOEnergy,
166  description: OSOEnergySensorEntityDescription,
167  entity_data: OSOEnergySensorData,
168  ) -> None:
169  """Initialize the OSO Energy sensor."""
170  super().__init__(instance, entity_data)
171 
172  device_id = entity_data.device_id
173  self._attr_unique_id_attr_unique_id = f"{device_id}_{description.key}"
174  self.entity_descriptionentity_description = description
175 
176  @property
177  def native_value(self) -> StateType:
178  """Return the state of the sensor."""
179  return self.entity_descriptionentity_description.value_fn(self.entity_dataentity_data)
180 
181  async def async_update(self) -> None:
182  """Update all data for OSO Energy."""
183  await self.osoenergy.session.update_data()
184  self.entity_dataentity_data = await self.osoenergy.sensor.get_sensor(self.entity_dataentity_data)
None __init__(self, OSOEnergy instance, OSOEnergySensorEntityDescription description, OSOEnergySensorData entity_data)
Definition: sensor.py:168
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:142