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 29, 2024
1 parent de59e8e commit 5b947f1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,19 @@ def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cy
class SolarmanBatteryCapacitySensor(SolarmanRestoreSensor):
def __init__(self, coordinator, sensor):
super().__init__(coordinator, sensor)
self._digits = sensor[DIGITS] if DIGITS in sensor else DEFAULT_[DIGITS]
self._digits = sensor.get(DIGITS, DEFAULT_[DIGITS])
self._threshold = sensor.get("threshold", 200)
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]
if (state := await self.async_get_last_state()) and "states" in state.attributes:
self._attr_extra_state_attributes["states"] = self._states = state.attributes["states"]

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 (power > -300 and was_charging) or (power < 300 and not was_charging):
if (power > -self._threshold and was_charging) or (power < self._threshold and not was_charging):
self._temp = []
return
if (soc := get_tuple(self.coordinator.data.get("battery_sensor"))) is not None and (tb := get_tuple(self.coordinator.data.get("total_battery_charge_sensor" if is_charging else "total_battery_discharge_sensor"))) is not None:
Expand All @@ -148,15 +149,15 @@ def update(self):
self._states.append(state)
self._attr_extra_state_attributes["states"] = self._states
self._temp = [(power, soc, tb)]
if (srtd := sorted(self._states)) and (c := srtd[1:-1] if len(srtd) > 2 else srtd):
self.set_state(get_number((sum(c) + self._states[0]) / (len(c) + 1), self._digits))
if (srtd := sorted(self._states)):
self.set_state(get_number(sum(srtd) / len(srtd), self._digits))

class SolarmanBatteryCustomSensor(SolarmanSensor):
def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating):
super().__init__(coordinator, sensor)
self._digits = sensor.get(DIGITS, DEFAULT_[DIGITS])
self._battery_nominal_voltage = battery_nominal_voltage
self._battery_life_cycle_rating = battery_life_cycle_rating
self._digits = sensor[DIGITS] if DIGITS in sensor else DEFAULT_[DIGITS]

def update(self):
#super().update()
Expand Down

0 comments on commit 5b947f1

Please sign in to comment.