Skip to content

Commit

Permalink
calculated consumption added
Browse files Browse the repository at this point in the history
  • Loading branch information
Euan Campbell committed Jun 16, 2021
1 parent 3d9778d commit dcb7f5a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ To confirm this worked, the below can be used. A valid response should be return
energy_api.account_details()
```

## Consumption Data
## Consumption

Getting all consumption data.
```python
from octopus_energy_api import oe_api
import datetime
Expand All @@ -63,7 +64,24 @@ start = today.replace(day=1) # setting start date to the beginning of the month
energy_api.consumption(start, today)
```

## Product Data
Getting calculated consumption data - total, mean, and median.
```python
from octopus_energy_api import oe_api
import datetime

energy_api = oe_api(account_number, api_key, mpan=mpan, serial_number=serial_number)

today = datetime.date.today() # setting end date to today
start = today.replace(day=1) # setting start date to the beginning of the month

energy_api.consumption_total(start, today)

energy_api.consumption_mean(start, today)

energy_api.consumption_median(start, today)
```

## Products

```python
from octopus_energy_api import oe_api
Expand All @@ -73,7 +91,7 @@ energy_api = oe_api(account_number, api_key, mpan=mpan, serial_number=serial_num
energy_api.products()
```

## Product Data
## Meter Information

```python
from octopus_energy_api import oe_api
Expand Down
15 changes: 9 additions & 6 deletions octopus_energy_api/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from octopus_energy_api import oe_api
import datetime
import os

api_key = os.environ.get('API_KEY')
account_number = os.environ.get('ACCOUNT_NUMBER')

# account_details = oe_api.get_mpan_serial(account_number, api_key)

energy_api = oe_api(account_number, api_key)
print(energy_api.account_details()['properties'][0]['moved_in_at'])
# print(energy_api.account_details()['properties'][0]['moved_in_at'])

today = datetime.date.today()
start = today.replace(day=1)

energy_api.consumption_total(start, today)

energy_api = oe_api(account_number, api_key, mpan="", serial_number="")
# energy_api.account_details()
energy_api.consumption_mean(start, today)

energy_api.consumption_median(start, today)

print(energy_api.meter_point())

# print(energy_api.account_details())

Expand Down
43 changes: 42 additions & 1 deletion octopus_energy_api/octopus_energy_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
from requests.auth import HTTPBasicAuth
import json
import statistics

class oe_api():

Expand Down Expand Up @@ -44,7 +45,6 @@ def get_mpan_serial(self, account_number, api_key):
to_return={}

# mpan
print(details)
mpan = details['properties'][0]['electricity_meter_points'][0]['mpan']
to_return['mpan']=mpan

Expand Down Expand Up @@ -99,6 +99,47 @@ def consumption(self, start, end):

return(response['results'])

def consumption_total(self, start, end):
"""Calculates the amount of kWh used in the timeframe given, returns the value as a float"""

consumption=self.consumption(start, end)

total_consumption = 0

for record in consumption:
total_consumption+=record['consumption']

total_consumption = float('%.2f' % total_consumption)

return(total_consumption)

def consumption_mean(self, start, end):
"""Calculates the average kWh used in 30 minutes within the timeframe given, returns the value as a float"""

consumption=self.consumption(start, end)

consumption_list = []

for record in consumption:
consumption_list.append(record['consumption'])

average = float('%.2f' % float(sum(consumption_list) / len(consumption_list)))

return(average)

def consumption_median(self, start, end):

consumption=self.consumption(start, end)

consumption_list = []

for record in consumption:
consumption_list.append(record['consumption'])

median = statistics.median(consumption_list)

return(median)

def meter_point(self):

setup=f"/electricity-meter-points/{self.mpan}/"
Expand Down
8 changes: 2 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import os
import sys

print('sys args...')
print(sys.argv)

# load from environment variables
api_key = os.environ.get('API_KEY')
account_number = os.environ.get('ACCOUNT_NUMBER')
Expand All @@ -13,12 +10,11 @@


def test_account_details():
expected_output = '020-11-19T00:00:00Z'
expected_output = '2020-11-19T00:00:00Z'

output = energy_api.account_details()['properties'][0]['moved_in_at']
random=sys.argv[1]
if output != expected_output:
raise Exception(f"expected {expected_output}, got {output}. Random: {random}.")
raise Exception(f"expected {expected_output}, got {output}.")



Expand Down

0 comments on commit dcb7f5a

Please sign in to comment.