Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
Update to cope with DST used in consumption data
Browse files Browse the repository at this point in the history
  • Loading branch information
badguy99 committed Mar 30, 2020
1 parent be892ee commit dc22f43
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions apps/octocost/octocost.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import datetime
import requests
import json
import dateutil.parser
import pytz


class OctoCost(hass.Hass):
Expand Down Expand Up @@ -89,6 +91,7 @@ def calculate_cost_and_usage(self, start):
usage = 0
price = 0
cost = []
utc = pytz.timezone('UTC')

results = jconsumption[u'results']

Expand All @@ -105,9 +108,22 @@ def calculate_cost_and_usage(self, start):
usage = usage + (results[curridx][u'consumption'])
if ((results[curridx][u'interval_start']) !=
(cost[curridx][u'valid_from'])):
print('Unmatched consumption {}'.format(
results[curridx][u'interval_start']) +
' / cost {}'.format(cost[curridx][u'valid_from']))
# Daylight Savings?
consumption_date = (results[curridx][u'interval_start'])
if consumption_date.endswith('+01:00'):
date_time = dateutil.parser.parse(consumption_date)
utc_datetime = date_time.astimezone(utc)
utc_iso = utc_datetime.isoformat().replace("+00:00", "Z")
if utc_iso == (cost[curridx][u'valid_from']):
(results[curridx][u'interval_start']) = utc_iso
else:
print('UTC Unmatched consumption {}'.format(
results[curridx][u'interval_start']) +
' / cost {}'.format(cost[curridx][u'valid_from']))
else:
print('Unmatched consumption {}'.format(
results[curridx][u'interval_start']) +
' / cost {}'.format(cost[curridx][u'valid_from']))
price = price + ((cost[curridx][u'value_inc_vat']) *
(results[curridx][u'consumption']))
return usage, price

0 comments on commit dc22f43

Please sign in to comment.