Skip to content

Commit

Permalink
Changed terminoligy, diverted -> green, added sum ct group power
Browse files Browse the repository at this point in the history
  • Loading branch information
CJNE committed Sep 8, 2021
1 parent 1b49fe5 commit fe8e511
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
15 changes: 14 additions & 1 deletion pymyenergi/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
),
}
Expand Down Expand Up @@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion pymyenergi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
12 changes: 6 additions & 6 deletions pymyenergi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions pymyenergi/eddi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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"""
Expand Down
6 changes: 3 additions & 3 deletions pymyenergi/zappi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_eddi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe8e511

Please sign in to comment.