Skip to content

Commit d8ff285

Browse files
committed
Add more information on sensor
1 parent 76fabd7 commit d8ff285

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

custom_components/aton_storage/sensor.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,28 @@ async def async_setup_entry(
4848

4949
add_entities(
5050
[
51+
#power
5152
BatteryStatus(coordinator),
5253
HouseConsumption(coordinator),
5354
BatteryPower(coordinator),
5455
SolarProduction(coordinator),
5556
GridPower(coordinator),
57+
#power direction
5658
GridToHouse(coordinator),
5759
SolarToBattery(coordinator),
5860
SolarToGrid(coordinator),
5961
BatteryToHouse(coordinator),
6062
SolarToHouse(coordinator),
6163
GridToBattery(coordinator),
6264
BatteryToGrid(coordinator),
65+
#energy
66+
SoldEnergy(coordinator),
67+
SolarEnergy(coordinator),
68+
SelfConsumedEnergy(coordinator),
69+
BoughtEnergy(coordinator),
70+
ConsumedEnergy(coordinator),
71+
#other
72+
SelfSufficiency(coordinator),
6373
]
6474
)
6575

@@ -312,3 +322,114 @@ def __init__(self, coordinator: ApiCoordinator) -> None:
312322
@property
313323
def is_on(self) -> bool:
314324
return self.coordinator.api.status.battery_to_grid
325+
326+
327+
class BaseEnergySensor(SensorEntity, CoordinatorEntity):
328+
"""Representation of a Sensor."""
329+
330+
@property
331+
def device_info(self) -> DeviceInfo | None:
332+
return {
333+
"identifiers": {(DOMAIN, "aton_storage_" + self.coordinator.api.username)}
334+
}
335+
336+
def __init__(self, coordinator: ApiCoordinator) -> None:
337+
super().__init__(coordinator)
338+
self._attr_native_unit_of_measurement = ENERGY_WATT_HOUR
339+
self._attr_device_class = SensorDeviceClass.ENERGY
340+
self._attr_state_class = SensorStateClass.MEASUREMENT
341+
342+
def update(self) -> None:
343+
"""update"""
344+
345+
@callback
346+
def _handle_coordinator_update(self) -> None:
347+
"""Handle updated data from the coordinator."""
348+
self.update()
349+
self.async_write_ha_state()
350+
351+
352+
class SoldEnergy(BaseEnergySensor):
353+
"""Representation of a Sensor"""
354+
355+
def __init__(self,coordinator: ApiCoordinator) -> None:
356+
super().__init__(coordinator)
357+
self._attr_name = "Energia Venduta"
358+
self._attr_unique_id = "aton_sold_energy_"+coordinator.api.username
359+
360+
def update(self) -> None:
361+
self._attr_native_value = self.coordinator.api.sold_energy
362+
363+
364+
class SolarEnergy(BaseEnergySensor):
365+
"""Representation of a Sensor"""
366+
367+
def __init__(self,coordinator: ApiCoordinator) -> None:
368+
super().__init__(coordinator)
369+
self._attr_name = "Energia Solare"
370+
self._attr_unique_id = "aton_solar_energy_"+coordinator.api.username
371+
372+
def update(self) -> None:
373+
self._attr_native_value = self.coordinator.api.solar_energy
374+
375+
376+
class SelfConsumedEnergy(BaseEnergySensor):
377+
"""Representation of a Sensor"""
378+
379+
def __init__(self,coordinator: ApiCoordinator) -> None:
380+
super().__init__(coordinator)
381+
self._attr_name = "Energia Auto Consumata"
382+
self._attr_unique_id = "aton_self_energy_"+coordinator.api.username
383+
384+
def update(self) -> None:
385+
self._attr_native_value = self.coordinator.api.self_consumed_energy
386+
387+
388+
class BoughtEnergy(BaseEnergySensor):
389+
"""Representation of a Sensor"""
390+
391+
def __init__(self,coordinator: ApiCoordinator) -> None:
392+
super().__init__(coordinator)
393+
self._attr_name = "Energia Comprata"
394+
self._attr_unique_id = "aton_bought_energy_"+coordinator.api.username
395+
396+
def update(self) -> None:
397+
self._attr_native_value = self.coordinator.api.bought_energy
398+
399+
400+
class ConsumedEnergy(BaseEnergySensor):
401+
"""Representation of a Sensor"""
402+
403+
def __init__(self,coordinator: ApiCoordinator) -> None:
404+
super().__init__(coordinator)
405+
self._attr_name = "Energia Consumata"
406+
self._attr_unique_id = "aton_consumed_energy_"+coordinator.api.username
407+
408+
def update(self) -> None:
409+
self._attr_native_value = self.coordinator.api.consumed_energy
410+
411+
class SelfSufficiency(SensorEntity, CoordinatorEntity):
412+
"""Representation of a Sensor."""
413+
414+
@property
415+
def device_info(self) -> DeviceInfo | None:
416+
return {
417+
"identifiers": {
418+
(DOMAIN, "aton_storage_" + self.coordinator.api.username)
419+
},
420+
}
421+
422+
def __init__(self, coordinator: ApiCoordinator) -> None:
423+
super().__init__(coordinator)
424+
self._attr_name = "Autosufficienza"
425+
self._attr_native_unit_of_measurement = PERCENTAGE
426+
self._attr_device_class = SensorDeviceClass.POWER_FACTOR
427+
self._attr_state_class = SensorStateClass.MEASUREMENT
428+
self._attr_unique_id = "aton_self_sufficiency_" + coordinator.api.username
429+
430+
@callback
431+
def _handle_coordinator_update(self) -> None:
432+
"""Handle updated data from the coordinator."""
433+
self._attr_native_value = self.coordinator.api.status.self_sufficiency
434+
self.async_write_ha_state()
435+

0 commit comments

Comments
 (0)