Skip to content

Commit

Permalink
Examples: Agile pricing: Get API endpoint dynamically.
Browse files Browse the repository at this point in the history
Looks like the hard-coded API endpoint has expired, so I added an
option to get it dynamically.

There's now a REGION_CODE const, too, since prices (may?) vary by
region.
  • Loading branch information
Gadgetoid committed Jan 6, 2025
1 parent e4653a9 commit e0bac83
Showing 1 changed file with 35 additions and 2 deletions.
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

0 comments on commit e0bac83

Please sign in to comment.