Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_per_month to get month based consumption data for a given year. #11

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyeloverblik/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Main for pyeloverblik
'''
import argparse
from datetime import datetime
import logging
from . import Eloverblik

Expand Down Expand Up @@ -30,6 +31,17 @@ def main():
print(f"Total: {total}kWh")
else:
print(f"Error getting data. Status: {result.status}. Error: {result.detailed_status}")

result = Eloverblik(args.refresh_token).get_per_month(args.metering_point)
if result.status == 200:
print(f"Date: {result.data_date}")
for month in range(1, datetime.today().month + 1):
data = result.get_metering_data(month)
print(f"Month {month}: {data}kWh")

print(f"Total: {result.get_total_metering_data()}kWh")
else:
print(f"Error getting data. Status: {result.status}. Error: {result.detailed_status}")

def _configureLogging(args):
if args.log:
Expand Down
30 changes: 30 additions & 0 deletions pyeloverblik/eloverblik.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
from datetime import timedelta
import json
import re
import requests
import logging
from .models import RawResponse
Expand Down Expand Up @@ -121,6 +122,35 @@ def get_latest(self, metering_point):

return result

def get_per_month(self, metering_point, year=None):
'''
Get total consumption for each month in the given year, as well as the total for the year.
'''
if year is None:
year = datetime.today().year

if not re.match('\d{4}', str(year)):
raise ValueError("Year must be a four digit number.")

raw_data = self.get_time_series(metering_point,
from_date=datetime(year, 1, 1),
to_date=datetime(year, 12, 31) if year < datetime.today().year else datetime.today(),
aggregation='Month')

if raw_data.status == 200:
json_response = json.loads(raw_data.body)

r = self._parse_result(json_response)
keys = list(r.keys())
keys.sort()

result = TimeSeries(raw_data.status, keys[-1], [r[k].get_total_metering_data() for k in keys])
else:
result = TimeSeries(raw_data.status, None, None, raw_data.body)

return result


def _parse_result(self, result):
'''
Parse result from API call.
Expand Down
10 changes: 5 additions & 5 deletions pyeloverblik/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def detailed_status(self):
def data_date(self):
return self._data_date

def get_metering_data(self, hour):
def get_metering_data(self, index):
'''
Get metering data for a single hour.
hour=1: data between 00.00 and 01.00.
hour=4: data between 03.00 and 04.00.
Get metering data for a single hour or month.
index=1: data between 00.00 and 01.00 if TimeSeries contains day data, or January if TimeSeries contains month data.
index=4: data between 03.00 and 04.00 if TimeSeries contains day data, or April if TimeSeries contains month data.
'''
return self._metering_data[hour-1]
return self._metering_data[index-1]

def get_total_metering_data(self):
total = 0
Expand Down