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

Commit

Permalink
change call back to run at a defined time rather than offset from loc…
Browse files Browse the repository at this point in the history
…al time

Fixes: #3
  • Loading branch information
badguy99 committed Apr 25, 2020
1 parent 4e3b52e commit 17e247a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions apps/octocost/octocost.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ def initialize(self):

self.startdate = datetime.date.fromisoformat(
str(self.args['startdate']))
self.consumptionurl = 'https://api.octopus.energy/' + \

consumptionurl = 'https://api.octopus.energy/' + \
'v1/electricity-meter-points/' + str(MPAN) + '/meters/' + \
str(SERIAL) + '/consumption/'
self.costurl = 'https://api.octopus.energy/v1/products/' + \
costurl = 'https://api.octopus.energy/v1/products/' + \
'AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-' + \
str(region).upper() + '/standard-unit-rates/'
time = datetime.datetime.now()
time = time + datetime.timedelta(seconds=5)
self.run_every(self.cost_and_usage_callback, time, 120 * 60)

self.run_in(self.period_and_cost_callback, 5,
use=consumptionurl, cost=costurl)

for hour in [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]:
self.run_hourly(self.period_and_cost_callback,
datetime.time(hour, 0, 0),
use=consumptionurl,
cost=costurl)

def find_region(mpan):
url = 'https://api.octopus.energy/v1/electricity-meter-points/' + \
Expand All @@ -34,7 +40,9 @@ def find_region(mpan):
region = str(json_meter_details['gsp'][-1])
return region

def cost_and_usage_callback(self, kwargs):
def cost_and_usage_callback(self, **kwargs):
self.useurl = kwargs.get('use')
self.costurl = kwargs.get('cost')
today = datetime.date.today()
self.yesterday = today - datetime.timedelta(days=1)
startyear = datetime.date(today.year, 1, 1)
Expand Down Expand Up @@ -80,7 +88,7 @@ def calculate_count(self, start):

def calculate_cost_and_usage(self, start):
self.calculate_count(start=start)
rconsumption = requests.get(url=self.consumptionurl +
rconsumption = requests.get(url=self.useurl +
'?order_by=period&period_from=' +
start.isoformat() +
'T00:00:00Z&period_to=' +
Expand Down

0 comments on commit 17e247a

Please sign in to comment.