From fe8e51116c78c81644fcc5269dc89a1f4a56d026 Mon Sep 17 00:00:00 2001 From: Johan Isacsson Date: Wed, 8 Sep 2021 15:50:57 +0200 Subject: [PATCH] Changed terminoligy, diverted -> green, added sum ct group power --- pymyenergi/base_device.py | 15 ++++++++++++++- pymyenergi/cli.py | 2 +- pymyenergi/client.py | 12 ++++++------ pymyenergi/eddi.py | 8 ++++---- pymyenergi/zappi.py | 6 +++--- setup.py | 2 +- tests/test_eddi.py | 2 +- 7 files changed, 30 insertions(+), 17 deletions(-) diff --git a/pymyenergi/base_device.py b/pymyenergi/base_device.py index 76545ec..da42463 100644 --- a/pymyenergi/base_device.py +++ b/pymyenergi/base_device.py @@ -63,6 +63,8 @@ def __init__(self, connection: Connection, serialno, data=None) -> None: self._serialno = serialno self._data = data or {} self._name = None + self.ct_groups = {} + self.refresh_ct_groups() @property @abstractmethod @@ -149,7 +151,7 @@ async def fetch_history_data(self, date_from, how_long, resolution): "device_total": round( (energy_wh["h1b"] + energy_wh["h2b"] + energy_wh["h3b"]) / 1000, 2 ), - "device_diverted": round( + "device_green": round( (energy_wh["h1d"] + energy_wh["h2d"] + energy_wh["h3d"]) / 1000, 2 ), } @@ -207,10 +209,21 @@ def data(self): """All device data""" return self._data + def refresh_ct_groups(self): + groups = {} + for i in range(6): + key = f"ct{i+1}" + if hasattr(self, key): + ct = getattr(self, key) + if ct.name_as_key != "ct_none": + groups[ct.name_as_key] = groups.get(ct.name_as_key, 0) + ct.power + self.ct_groups = groups + @data.setter def data(self, value): """Set all device data""" self._data = value + self.refresh_ct_groups() async def refresh_history_data(self, from_date, how_long, resolution): """Refresh device history data""" diff --git a/pymyenergi/cli.py b/pymyenergi/cli.py index 738d85d..2b9732e 100644 --- a/pymyenergi/cli.py +++ b/pymyenergi/cli.py @@ -47,7 +47,7 @@ async def main(args): out = out + f"Energy imported : {client.energy_imported}kWh\n" out = out + f"Energy exported : {client.energy_exported}kWh\n" out = out + f"Energy generated : {client.energy_generated}kWh\n" - out = out + f"Energy diverted : {client.energy_diverted}kWh\n" + out = out + f"Energy green : {client.energy_green}kWh\n" out = out + "Devices:\n" for device in devices: out = out + f"\t{device.kind.capitalize()}: {device.name}" diff --git a/pymyenergi/client.py b/pymyenergi/client.py index 7ad45bf..f2777e8 100644 --- a/pymyenergi/client.py +++ b/pymyenergi/client.py @@ -57,7 +57,7 @@ def get_energy_totals(self): """Get total energy""" devices = self.get_devices_sync() keys = ["generated", "grid_import", "grid_export"] - totals = {"diverted": 0} + totals = {"green": 0} for key in keys: totals[key] = 0 for device in devices: @@ -66,8 +66,8 @@ def get_energy_totals(self): for key in keys: if totals[key] == 0: totals[key] = device.history_data.get(key, 0) - totals["diverted"] = totals.get("diverted", 0) + device.history_data.get( - "device_diverted", 0 + totals["green"] = totals.get("green", 0) + device.history_data.get( + "device_green", 0 ) return totals @@ -147,10 +147,10 @@ def energy_generated(self): return energy_totals.get("generated", 0) @property - def energy_diverted(self): - """Diverted energy""" + def energy_green(self): + """Green energy""" energy_totals = self.get_energy_totals() - return energy_totals.get("diverted", 0) + return energy_totals.get("green", 0) @property def power_grid(self): diff --git a/pymyenergi/eddi.py b/pymyenergi/eddi.py index e2a86bd..f34a16d 100644 --- a/pymyenergi/eddi.py +++ b/pymyenergi/eddi.py @@ -44,7 +44,7 @@ def status(self): return STATES[self._data.get("sta", 1)] @property - def diverted_session(self): + def consumed_session(self): """Energy diverted this session kWh""" return self._data.get("che") @@ -64,9 +64,9 @@ def energy_total(self): return self.history_data.get("device_total", 0) @property - def energy_diverted(self): - """Device diverted energy from history data""" - return self.history_data.get("device_diverted", 0) + def energy_green(self): + """Device green energy from history data""" + return self.history_data.get("device_green", 0) async def stop(self): """Stop diverting""" diff --git a/pymyenergi/zappi.py b/pymyenergi/zappi.py index fb5a835..02cb934 100644 --- a/pymyenergi/zappi.py +++ b/pymyenergi/zappi.py @@ -167,9 +167,9 @@ def energy_total(self): return self.history_data.get("device_total", 0) @property - def energy_diverted(self): - """Device diverted energy from history data""" - return self.history_data.get("device_diverted", 0) + def energy_green(self): + """Device green energy from history data""" + return self.history_data.get("device_green", 0) # @property # def boost_start_hour(self): diff --git a/setup.py b/setup.py index 154bd49..caec114 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="pymyenergi", - version="0.0.13", + version="0.0.14", author="Johan Isaksson", author_email="johan@generatorhallen.se", description="Python library and CLI for communicating with myenergi API.", diff --git a/tests/test_eddi.py b/tests/test_eddi.py index 3cb7098..1b5e4bb 100644 --- a/tests/test_eddi.py +++ b/tests/test_eddi.py @@ -9,4 +9,4 @@ async def test_refresh(eddi_fetch_data_fixture): eddi = Eddi({}, 16042300) await eddi.refresh() assert eddi.serial_number == 10088800 - assert eddi.diverted_session == 8.2 + assert eddi.consumed_session == 8.2