Skip to content

Commit

Permalink
feat: Add experimental Battery Capacity sensor - Deye
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 19, 2024
1 parent 1385c65 commit 6bbb0fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,15 @@ parameters:
registers: [0x00BF]
icon: "mdi:current-dc"

- name: "Battery Capacity"
description: Battery Capacity estimation
class: "power"
state_class: "measurement"
uom: "kW"
rule: 0
digits: 2
icon: "mdi:battery-check"

- group: Grid
items:
- name: "Grid Frequency"
Expand Down
10 changes: 10 additions & 0 deletions custom_components/solarman/inverter_definitions/deye_p3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,16 @@ parameters:
digits: 2
icon: "mdi:battery-heart"

# Battery - Capacity
- name: "Battery Capacity"
description: Battery Capacity estimation
class: "power"
state_class: "measurement"
uom: "kW"
rule: 0
digits: 2
icon: "mdi:battery-check"

# Battery - The state of battery
- name: "Battery State"
description: Determines battery state from battery power by +-50 W
Expand Down
30 changes: 30 additions & 0 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def _create_entity(coordinator, description, options):
return SolarmanBatteryCustomSensor(coordinator, description, battery_nominal_voltage, battery_life_cycle_rating)
elif battery_nominal_voltage > 0 and battery_life_cycle_rating > 0 and name in ("Battery SOH", "Today Battery Life Cycles", "Total Battery Life Cycles"):
return SolarmanBatteryCustomSensor(coordinator, description, battery_nominal_voltage, battery_life_cycle_rating)
elif name == "Battery Capacity":
return SolarmanBatteryCapacitySensor(coordinator, description)

if "persistent" in description:
return SolarmanPersistentSensor(coordinator, description)
Expand Down Expand Up @@ -113,6 +115,34 @@ def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cy
if battery_nominal_voltage > 0 and battery_life_cycle_rating > 0:
self._attr_extra_state_attributes = self._attr_extra_state_attributes | { "Nominal Voltage": battery_nominal_voltage, "Life Cycle Rating": battery_life_cycle_rating }

class SolarmanBatteryCapacitySensor(SolarmanRestoreSensor):
def __init__(self, coordinator, sensor):
super().__init__(coordinator, sensor)
self._is_charging = False
self._start_charge = 0.0
self._start_soc = 0

self._states = []

def update(self):
if (power := get_tuple(self.coordinator.data.get("battery_power_sensor"))):
if power > -500:
self._states = []
return
if (soc := get_tuple(self.coordinator.data.get("battery_sensor"))) is not None and (tbc := get_tuple(self.coordinator.data.get("total_battery_charge_sensor"))) is not None:
self._states.append((power, soc, tbc))
h = l = (soc, tbc)
for i in reversed(self._states):
s = (i[1], i[2])
if h[1] > l[1] > s[1]:
break
if h[1] == s[1]:
h = l = s
if l[1] >= s[1]:
l = s
if h[1] > l[1] > s[1] and (diff := h[0] - l[0]) > 0:
self.set_state((h[1] - l[1]) * (100 / (diff)))

class SolarmanBatteryCustomSensor(SolarmanSensor):
def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating):
super().__init__(coordinator, sensor)
Expand Down

0 comments on commit 6bbb0fd

Please sign in to comment.