1 """The sensor entity for the Youless integration."""
3 from __future__
import annotations
5 from youless_api
import YoulessAPI
6 from youless_api.youless_sensor
import YoulessSensor
16 UnitOfElectricCurrent,
17 UnitOfElectricPotential,
28 DataUpdateCoordinator,
35 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
37 """Initialize the integration."""
38 coordinator: DataUpdateCoordinator[YoulessAPI] = hass.data[DOMAIN][entry.entry_id]
39 device = entry.data[CONF_DEVICE]
40 if (device := entry.data[CONF_DEVICE])
is None:
41 device = entry.entry_id
48 coordinator, device,
"low", SensorStateClass.TOTAL_INCREASING
51 coordinator, device,
"high", SensorStateClass.TOTAL_INCREASING
73 CoordinatorEntity[DataUpdateCoordinator[YoulessAPI]], SensorEntity
75 """The base sensor for Youless."""
79 coordinator: DataUpdateCoordinator[YoulessAPI],
85 """Create the sensor."""
89 identifiers={(DOMAIN, f
"{device}_{device_group}")},
90 manufacturer=
"YouLess",
91 model=self.coordinator.data.model,
97 """Property to get the underlying sensor object."""
102 """Determine the state value, only if a sensor is initialized."""
110 """Return a flag to indicate the sensor not being available."""
111 return super().available
and self.
get_sensorget_sensor
is not None
115 """The Youless Water sensor."""
117 _attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS
118 _attr_device_class = SensorDeviceClass.WATER
119 _attr_state_class = SensorStateClass.TOTAL_INCREASING
122 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str
124 """Instantiate a Water sensor."""
125 super().
__init__(coordinator, device,
"water",
"Water meter",
"water")
131 """Get the sensor for providing the value."""
132 return self.coordinator.data.water_meter
136 """The Youless gas sensor."""
138 _attr_native_unit_of_measurement = UnitOfVolume.CUBIC_METERS
139 _attr_device_class = SensorDeviceClass.GAS
140 _attr_state_class = SensorStateClass.TOTAL_INCREASING
143 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str
145 """Instantiate a gas sensor."""
146 super().
__init__(coordinator, device,
"gas",
"Gas meter",
"gas")
152 """Get the sensor for providing the value."""
153 return self.coordinator.data.gas_meter
157 """The current power usage sensor."""
159 _attr_native_unit_of_measurement = UnitOfPower.WATT
160 _attr_device_class = SensorDeviceClass.POWER
161 _attr_state_class = SensorStateClass.MEASUREMENT
164 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str
166 """Instantiate the usage meter."""
167 super().
__init__(coordinator, device,
"power",
"Power usage",
"usage")
173 """Get the sensor for providing the value."""
174 return self.coordinator.data.current_power_usage
178 """The Youless delivery meter value sensor."""
180 _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
181 _attr_device_class = SensorDeviceClass.ENERGY
182 _attr_state_class = SensorStateClass.TOTAL_INCREASING
185 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, dev_type: str
187 """Instantiate a delivery meter sensor."""
189 coordinator, device,
"delivery",
"Energy delivery", f
"delivery_{dev_type}"
196 """Get the sensor for providing the value."""
197 if self.coordinator.data.delivery_meter
is None:
200 return getattr(self.coordinator.data.delivery_meter, f
"_{self._type}",
None)
204 """The Youless low meter value sensor."""
206 _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
207 _attr_device_class = SensorDeviceClass.ENERGY
208 _attr_state_class = SensorStateClass.TOTAL_INCREASING
212 coordinator: DataUpdateCoordinator[YoulessAPI],
215 state_class: SensorStateClass,
217 """Instantiate a energy meter sensor."""
219 coordinator, device,
"power",
"Energy usage", f
"power_{dev_type}"
228 """Get the sensor for providing the value."""
229 if self.coordinator.data.power_meter
is None:
232 return getattr(self.coordinator.data.power_meter, f
"_{self._type}",
None)
236 """The current power usage of a single phase."""
238 _attr_native_unit_of_measurement = UnitOfPower.WATT
239 _attr_device_class = SensorDeviceClass.POWER
240 _attr_state_class = SensorStateClass.MEASUREMENT
243 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, phase: int
245 """Initialize the power phase sensor."""
247 coordinator, device,
"power",
"Energy usage", f
"phase_{phase}_power"
254 """Get the sensor value from the coordinator."""
255 phase_sensor = getattr(self.coordinator.data, f
"phase{self._phase}",
None)
256 if phase_sensor
is None:
259 return phase_sensor.power
263 """The current voltage of a single phase."""
265 _attr_native_unit_of_measurement = UnitOfElectricPotential.VOLT
266 _attr_device_class = SensorDeviceClass.VOLTAGE
267 _attr_state_class = SensorStateClass.MEASUREMENT
270 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, phase: int
272 """Initialize the voltage phase sensor."""
274 coordinator, device,
"power",
"Energy usage", f
"phase_{phase}_voltage"
281 """Get the sensor value from the coordinator for phase voltage."""
282 phase_sensor = getattr(self.coordinator.data, f
"phase{self._phase}",
None)
283 if phase_sensor
is None:
286 return phase_sensor.voltage
290 """The current current of a single phase."""
292 _attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE
293 _attr_device_class = SensorDeviceClass.CURRENT
294 _attr_state_class = SensorStateClass.MEASUREMENT
297 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, phase: int
299 """Initialize the current phase sensor."""
301 coordinator, device,
"power",
"Energy usage", f
"phase_{phase}_current"
308 """Get the sensor value from the coordinator for phase current."""
309 phase_sensor = getattr(self.coordinator.data, f
"phase{self._phase}",
None)
310 if phase_sensor
is None:
313 return phase_sensor.current
317 """The Youless extra meter value sensor (s0)."""
319 _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
320 _attr_device_class = SensorDeviceClass.ENERGY
321 _attr_state_class = SensorStateClass.TOTAL_INCREASING
324 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, dev_type: str
326 """Instantiate an extra meter sensor."""
328 coordinator, device,
"extra",
"Extra meter", f
"extra_{dev_type}"
335 """Get the sensor for providing the value."""
336 if self.coordinator.data.extra_meter
is None:
339 return getattr(self.coordinator.data.extra_meter, f
"_{self._type}",
None)
343 """The Youless extra meter power value sensor (s0)."""
345 _attr_native_unit_of_measurement = UnitOfPower.WATT
346 _attr_device_class = SensorDeviceClass.POWER
347 _attr_state_class = SensorStateClass.MEASUREMENT
350 self, coordinator: DataUpdateCoordinator[YoulessAPI], device: str, dev_type: str
352 """Instantiate an extra meter power sensor."""
354 coordinator, device,
"extra",
"Extra meter", f
"extra_{dev_type}"
361 """Get the sensor for providing the value."""
362 if self.coordinator.data.extra_meter
is None:
365 return getattr(self.coordinator.data.extra_meter, f
"_{self._type}",
None)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, str dev_type)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, str dev_type, SensorStateClass state_class)
YoulessSensor|None get_sensor(self)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, int phase)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, int phase)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, int phase)
YoulessSensor|None get_sensor(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device)
YoulessSensor|None get_sensor(self)
YoulessSensor|None get_sensor(self)
StateType native_value(self)
None __init__(self, DataUpdateCoordinator[YoulessAPI] coordinator, str device, str device_group, str friendly_name, str sensor_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)