Skip to content

Commit

Permalink
Fixed CLI energy output bug, show raw json response for history data
Browse files Browse the repository at this point in the history
  • Loading branch information
CJNE committed Sep 28, 2021
1 parent f202b8f commit 2e68ce0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 13 additions & 7 deletions pymyenergi/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,27 @@ async def fetch_data(self):
data = response[self.kind][0]
return data

async def energy_today(self):
async def energy_today(self, raw_response=False):
today = datetime.now(timezone.utc)
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
# return await self.history_energy_minutes(today, 1440)
return await self.history_energy_hours(today, 24)
return await self.history_energy_hours(today, 24, raw_response)

async def history_energy_minutes(self, date_from, how_long=1440):
async def history_energy_minutes(
self, date_from, how_long=1440, raw_response=False
):
if date_from is None:
date_from = datetime.now(timezone.utc) - timedelta(minutes=how_long)
return await self.fetch_history_data(date_from, how_long, MINUTE)
return await self.fetch_history_data(date_from, how_long, MINUTE, raw_response)

async def history_energy_hours(self, date_from, how_long=24):
async def history_energy_hours(self, date_from, how_long=24, raw_response=False):
if date_from is None:
date_from = datetime.now(timezone.utc) - timedelta(hours=how_long)
return await self.fetch_history_data(date_from, how_long, HOUR)
return await self.fetch_history_data(date_from, how_long, HOUR, raw_response)

async def fetch_history_data(self, date_from, how_long, resolution):
async def fetch_history_data(
self, date_from, how_long, resolution, raw_response=False
):
energy_wh = {
"gep": 0,
"gen": 0,
Expand All @@ -134,6 +138,8 @@ async def fetch_history_data(self, date_from, how_long, resolution):
_LOGGER.debug(f"Fetching {resolution} history data for {self.kind}")
data = await self._connection.get(url)
data = data[f"U{self.serial_number}"]
if raw_response:
return data

for row in data:
for key in energy_wh:
Expand Down
4 changes: 2 additions & 2 deletions pymyenergi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ async def main(args):
else:
print(device.show())
elif args.action == "energy":
data = await device.energy_today()
data = await device.energy_today(args.json)
if args.json:
print(json.dumps(data, indent=2))
else:
for key in data.keys():
print(f"{key}: {(data[key]/1000):.2f}kWh")
print(f"{key}: {data[key]}kWh")
elif args.action == "stop" and args.command == ZAPPI:
await device.stop_charge()
print("Charging was stopped")
Expand Down

0 comments on commit 2e68ce0

Please sign in to comment.