Skip to content

Commit

Permalink
refactor: Experimental Battery Capacity sensor - Deye
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 20, 2024
1 parent 1efa7ee commit 86c746c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def __init__(self, coordinator, sensor):

class SolarmanRestoreSensor(SolarmanSensor, RestoreSensor):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()

if (last_sensor_data := await self.async_get_last_sensor_data()) is not None:
Expand Down Expand Up @@ -122,10 +121,13 @@ def __init__(self, coordinator, sensor):
self._states = []
self._temp = []

async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
if len(self._states) == 0 and self._attr_native_value is not None:
self._attr_extra_state_attributes["states"] = self._states = [self._attr_native_value]

def update(self):
if (power := get_tuple(self.coordinator.data.get("battery_power_sensor"))) is not None and (is_charging := power < 0) is not None and (was_charging := (self._temp[-1][0] < 0) if len(self._temp) > 0 else is_charging) is not None:
if len(self._states) == 0 and self._attr_native_value is not None:
self._attr_extra_state_attributes["states"] = self._states = [float(self._attr_native_value)]
if (power > -300 and was_charging) or (power < 300 and not was_charging):
self._temp = []
return
Expand All @@ -142,7 +144,7 @@ def update(self):
m = l = s
if l[1] == m[1] or l[1] == s[1]:
l = s
if h[1] > l[1] > s[1] and (diff := abs(h[0] - l[0])) > 0 and (state := get_number((h[1] - l[1]) * (100 / diff), self._digits)):
if h[1] > m[1] > l[1] > s[1] and (diff := abs(h[0] - l[0])) > 0 and (state := get_number((h[1] - l[1]) * (100 / diff), self._digits)):
self._states.append(state)
self._attr_extra_state_attributes["states"] = self._states
self._temp = [(power, soc, tb)]
Expand Down

0 comments on commit 86c746c

Please sign in to comment.