Skip to content

Commit

Permalink
Expose additional battery voltage sensors (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdpuk authored Dec 16, 2022
1 parent d0a5dc9 commit bb108e8
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions custom_components/givenergy_local/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Home Assistant sensor descriptions."""
from __future__ import annotations

from collections.abc import Mapping

from typing import Any

from givenergy_modbus.model.inverter import Model
from homeassistant.components.sensor import (
SensorDeviceClass,
Expand Down Expand Up @@ -122,6 +126,14 @@
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
SensorEntityDescription(
key="v_battery",
name="Battery Voltage",
icon=Icon.BATTERY,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
),
SensorEntityDescription(
key="p_battery",
name="Battery Power",
Expand Down Expand Up @@ -260,8 +272,8 @@
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="v_battery_cells_sum",
name="Battery Voltage",
key="v_battery_out",
name="Battery Output Voltage",
icon=Icon.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
Expand All @@ -277,6 +289,14 @@
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
)

_BATTERY_CELLS_VOLTAGE_SENSOR = SensorEntityDescription(
key="v_battery_cells_sum",
name="Battery Cells Voltage",
icon=Icon.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
)


async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -343,7 +363,13 @@ async def async_setup_entry(
config_entry,
entity_description=_BATTERY_REMAINING_CAPACITY_SENSOR,
battery_id=batt_num,
)
),
BatteryCellsVoltageSensor(
coordinator,
config_entry,
entity_description=_BATTERY_CELLS_VOLTAGE_SENSOR,
battery_id=batt_num,
),
]
)
else:
Expand Down Expand Up @@ -434,7 +460,7 @@ def native_value(self) -> StateType:


class BatteryModeSensor(InverterBasicSensor):
"""Battery mdoe sensor."""
"""Battery mode sensor."""

@property
def native_value(self) -> StateType:
Expand Down Expand Up @@ -485,7 +511,12 @@ def native_value(self) -> StateType:


class BatteryBasicSensor(BatteryEntity, SensorEntity):
"""A battery sensor that derives its value from the register values fetched from the inverter."""
"""
A battery sensor that derives its value from the register values fetched from the inverter.
Values are as reported from the BMS in each battery. Sometimes there are differences in
values as reported by the inverter itself and the BMS.
"""

def __init__(
self,
Expand Down Expand Up @@ -519,3 +550,12 @@ def native_value(self) -> StateType:
# Raw value is in Ah (Amp Hour)
# Convert to KWh using formula Ah * V / 1000
return round(battery_remaining_capacity, 3)


class BatteryCellsVoltageSensor(BatteryBasicSensor):
"""Battery cell voltage sensor."""

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Expose individual cell voltages."""
return self.data.dict(include={f"v_battery_cell_{i:02d}" for i in range(1, 16)}) # type: ignore[no-any-return]

0 comments on commit bb108e8

Please sign in to comment.