From df89d612a82499432c8a9ae95b2a034585619dc1 Mon Sep 17 00:00:00 2001 From: Josh <12600312+bubonicbob@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:26:57 -0800 Subject: [PATCH] Add battery percent charged calculation --- README.md | 2 ++ tesla_powerwall/responses.py | 6 ++++++ tests/integration/test_powerwall.py | 1 + tests/unit/test_powerwall.py | 1 + 4 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 7d51f38..ee32a6f 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,8 @@ batteries[0].i_out #=> -7.4 batteries[0].grid_state #=> GridState.COMPLIANT +batteries[0].percent_charged +#=> 52.58 ``` ### Powerwall Status diff --git a/tesla_powerwall/responses.py b/tesla_powerwall/responses.py index b72236a..5ea73ce 100644 --- a/tesla_powerwall/responses.py +++ b/tesla_powerwall/responses.py @@ -285,9 +285,14 @@ class BatteryResponse(ResponseBase): f_out: float i_out: float grid_state: GridState + percent_charged: float @staticmethod def from_dict(src: dict) -> "BatteryResponse": + percent_charged = ( + 100.0 * src["nominal_energy_remaining"] / src["nominal_full_pack_energy"] + ) + return BatteryResponse( src, part_number=src["PackagePartNumber"], @@ -303,4 +308,5 @@ def from_dict(src: dict) -> "BatteryResponse": f_out=src["f_out"], i_out=src["i_out"], grid_state=GridState(src["pinv_grid_state"]), + percent_charged=percent_charged, ) diff --git a/tests/integration/test_powerwall.py b/tests/integration/test_powerwall.py index 1c11568..6a0b327 100644 --- a/tests/integration/test_powerwall.py +++ b/tests/integration/test_powerwall.py @@ -87,6 +87,7 @@ async def test_batteries(self) -> None: battery.capacity battery.part_number battery.serial_number + battery.percent_charged async def test_grid_status(self) -> None: grid_status = await self.powerwall.get_grid_status() diff --git a/tests/unit/test_powerwall.py b/tests/unit/test_powerwall.py index 88bccee..948eba5 100644 --- a/tests/unit/test_powerwall.py +++ b/tests/unit/test_powerwall.py @@ -256,6 +256,7 @@ async def test_system_status(self): self.assertEqual(batteries[0].q_out, 30) self.assertEqual(batteries[0].v_out, 226.60000000000002) self.assertEqual(batteries[0].grid_state, GridState.COMPLIANT) + self.assertEqual(batteries[0].percent_charged, 100.0 * 7378 / 14031) self.aresponses.assert_plan_strictly_followed() async def test_islanding_mode_offgrid(self):