Skip to content

Commit bcb84b4

Browse files
committed
Merge branch 'master' of github.com:markgdev/OctopusAgile
2 parents d3b2c21 + ee16b04 commit bcb84b4

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

OctopusAgile/Agile.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
from datetime import datetime, timedelta, date
33
import collections
44

5+
import logging
6+
_LOGGER = logging.getLogger("OctopusAgile")
7+
58

69
class Agile:
710
area_code = None
811
base_url = None
912

13+
def round_time(self, t):
14+
# Rounds to nearest half hour
15+
minute = 00
16+
if t.minute//30 == 1:
17+
minute = 30
18+
return (t.replace(second=0, microsecond=0, minute=minute, hour=t.hour))
19+
1020
def __init__(self, area_code):
1121
self.area_code = area_code
1222
self.base_url = 'https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs'
@@ -17,6 +27,9 @@ def get_times_below(self, in_d, limit):
1727
if rate <= limit:
1828
ret_d[time] = rate
1929
return ret_d
30+
31+
def get_area_code(self):
32+
return self.area_code
2033

2134
def get_min_times(self, num, in_d, requirements):
2235
ret_d = {}
@@ -60,7 +73,7 @@ def get_max_times(self, num, in_d):
6073

6174

6275
def get_min_time_run(self, hours, in_d):
63-
slots = hours*2
76+
slots = int(hours*2)
6477
d = {}
6578
d.update(collections.OrderedDict(reversed(list(in_d.items())))) # Dict was in wrong order
6679
keys = list(d.keys())
@@ -88,17 +101,25 @@ def get_rates_delta(self, day_delta):
88101
# print(date_from)
89102
return self.get_rates(date_from, date_to)
90103

91-
def get_raw_rates(self, date_from, date_to):
104+
def get_raw_rates(self, date_from, date_to=None):
92105
date_from = f"?period_from={ date_from }"
93-
date_to = f"&period_to={ date_to }"
106+
if date_to is not None:
107+
date_to = f"&period_to={ date_to }"
108+
else:
109+
date_to = ""
94110
headers = {'content-type': 'application/json'}
95111
r = requests.get(f'{self.base_url}/'
96112
f'E-1R-AGILE-18-02-21-{self.area_code}/'
97113
f'standard-unit-rates/{ date_from }{ date_to }', headers=headers)
98114
results = r.json()["results"]
115+
_LOGGER.debug(r.url)
99116
return results
100117

101-
def get_rates(self, date_from, date_to):
118+
def get_new_rates(self):
119+
date_from = datetime.strftime(datetime.utcnow(), '%Y-%m-%dT%H:%M:%SZ')
120+
return self.get_rates(date_from)
121+
122+
def get_rates(self, date_from, date_to=None):
102123
results = self.get_raw_rates(date_from, date_to)
103124

104125
date_rates = collections.OrderedDict()
@@ -177,9 +198,35 @@ def summary(self, days, daily_sum=False):
177198
print(f"Num Days: {days}")
178199

179200
# print(all_rates)
201+
202+
def get_previous_rate(self):
203+
now = self.round_time(datetime.utcnow())
204+
rounded_time = datetime.strftime(self.round_time(now), '%Y-%m-%dT%H:%M:%SZ')
205+
prev_time = datetime.strftime(now - timedelta(minutes=30), '%Y-%m-%dT%H:%M:%SZ')
206+
date_rates = self.get_rates(prev_time, rounded_time)["date_rates"]
207+
return date_rates[next(iter(date_rates))]
208+
209+
def get_current_rate(self):
210+
now = self.round_time(datetime.utcnow())
211+
rounded_time = datetime.strftime(self.round_time(now), '%Y-%m-%dT%H:%M:%SZ')
212+
next_time = datetime.strftime(now + timedelta(minutes=30), '%Y-%m-%dT%H:%M:%SZ')
213+
date_rates = self.get_rates(rounded_time, next_time)["date_rates"]
214+
return date_rates[next(iter(date_rates))]
215+
216+
def get_next_rate(self):
217+
now = self.round_time(datetime.utcnow())
218+
rounded_time = datetime.strftime(self.round_time(now) + timedelta(minutes=30), '%Y-%m-%dT%H:%M:%SZ')
219+
next_time = datetime.strftime(now + timedelta(minutes=60), '%Y-%m-%dT%H:%M:%SZ')
220+
date_rates = self.get_rates(rounded_time, next_time)["date_rates"]
221+
return date_rates[next(iter(date_rates))]
222+
180223
if __name__ == "__main__":
181224
myagile = Agile("L")
182225
rates = myagile.get_rates_delta(1)['date_rates']
183226
low_rates = myagile.get_times_below(rates, 0)
184227
print(low_rates)
185228
print(myagile.get_min_time_run(3, rates))
229+
print("prev: ", myagile.get_previous_rate())
230+
print("now: ", myagile.get_current_rate())
231+
print("next: ", myagile.get_next_rate())
232+
print("New: ", myagile.get_new_rates())

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# OctopusAgile
22
Init with the region code (Don't include the _) as per: https://en.wikipedia.org/wiki/Distribution_network_operator
33

4+
## Referral code
5+
Feel free to use my referral code and get £50 credit to your account (as well as mine): https://share.octopus.energy/lilac-bison-793
6+
47
## Methods
58
### Time format
69
All time formats are "%Y-%m-%dT%H:%M:%SZ" e.g. 2020-04-16T06:00:00Z
@@ -39,5 +42,20 @@ Requirements a list of dicts with details of particular times that must be inclu
3942

4043
Example, must have 2 slots between 1900 and 0600: {'slots': 2, 'time_from': '2020-04-15T19:00:00Z', 'time_to': '2020-04-16T06:00:00Z'}
4144

45+
### get_area_code()
46+
Return the area code that is being used
47+
48+
### get_new_rates()
49+
Return all available future rates
50+
51+
### get_previous_rate()
52+
Return the previous period rate
53+
54+
### get_current_rate()
55+
Return the current period rate
56+
57+
### get_next_rate()
58+
Return the next period rate
59+
4260
### get_max_times(num, in_d)
43-
Get a date_rate dict of "num" number of max periods
61+
Get a date_rate dict of "num" number of max periods

0 commit comments

Comments
 (0)