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

Examples: Agile pricing: Get API endpoint dynamically. #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions examples/agile_pricing_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
from presto import Presto

# Constants
API_URL = 'https://api.octopus.energy/v1/products/AGILE-FLEX-22-11-25/electricity-tariffs/E-1R-AGILE-FLEX-22-11-25-C/standard-unit-rates/'
# API_URL = 'https://api.octopus.energy/v1/products/AGILE-24-10-01/electricity-tariffs/E-1R-AGILE-24-10-01-C/standard-unit-rates/'
API_URL = None # Will try to guess the rates

# Find your region code: https://en.wikipedia.org/wiki/Meter_Point_Administration_Number#Distributor_ID
# This is required for automatic API endpoint detection
REGION_CODE = "_E"

# Print out API endpoints for debugging
DEBUG = True

# Setup for the Presto display

Expand Down Expand Up @@ -56,8 +64,26 @@
last_updated = time.time()


def get_prices():
def get_api_endpoint():
# Get the latest valid API endpoint for prices
# First look up the right product in the products list
request = requests.get("https://api.octopus.energy/v1/products/")
json = request.json()
product = [result for result in json["results"] if result["display_name"].startswith("Agile") and result["direction"] == "IMPORT" and result["brand"] == "OCTOPUS_ENERGY"][0]
product_api_endpoint = [link["href"] for link in product["links"] if link["rel"] == "self"][0]

# Now get the API endpoint URL by region code
request = requests.get(product_api_endpoint)
json = request.json()
links = json["single_register_electricity_tariffs"][REGION_CODE]["direct_debit_monthly"]["links"]
prices_api_endpoint = [link for link in links if link["rel"] == "standard_unit_rates"][0]["href"]

if DEBUG:
print(f"Got API endpoint: {prices_api_endpoint}")
return prices_api_endpoint


def get_prices():
try:
# We only need the the first 6 elements covering the date and time
t = time.localtime()[0:5]
Expand All @@ -75,6 +101,9 @@ def get_prices():
request = requests.get(f"{API_URL}{request_string}")
json = request.json()

if DEBUG:
print(f"Prices endpoint: {API_URL}{request_string}")

# Finally we return our 3 values
return json['results'][0]['value_inc_vat'], json['results'][1]['value_inc_vat'], json['results'][2]['value_inc_vat']

Expand All @@ -83,6 +112,10 @@ def get_prices():
return 0, 0, 0


# Get the correct API endpoint on start up, if it has not been specified already
if API_URL is None:
API_URL = get_api_endpoint()

# Get the prices on start up, after this we'll only check again at the top of the hour.
next_price, current_price, last_price = get_prices()

Expand Down
Loading