Skip to content

Commit

Permalink
fix(sensor): Updated retrieving of rates to try more frequently if ra…
Browse files Browse the repository at this point in the history
…tes are out of date
  • Loading branch information
BottlecapDave committed Jun 25, 2023
1 parent 16c1c75 commit a50e0bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions custom_components/octopus_energy/coordinators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def async_check_valid_tariff(hass, client: OctopusEnergyApiClient, tariff_
)
else:
try:
_LOGGER.debug(f"Retrieving product information for '{tariff_parts.product_code}'")
product = await client.async_get_product(tariff_parts.product_code)
if product is None:
ir.async_create_issue(
Expand Down
19 changes: 12 additions & 7 deletions custom_components/octopus_energy/coordinators/electricity_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ async def async_update_electricity_rates_data():
# Only get data every half hour or if we don't have any data
current = now()
client: OctopusEnergyApiClient = hass.data[DOMAIN][DATA_CLIENT]
if (DATA_ACCOUNT in hass.data[DOMAIN] and
DATA_INTELLIGENT_DISPATCHES in hass.data[DOMAIN] and
DATA_RATES not in hass.data[DOMAIN] or (current.minute % 30) == 0 or hass.data[DOMAIN][DATA_RATES] is None or len(hass.data[DOMAIN][DATA_RATES]) == 0):
if (DATA_ACCOUNT in hass.data[DOMAIN] and DATA_INTELLIGENT_DISPATCHES in hass.data[DOMAIN]):

tariff_codes = await async_get_current_electricity_agreement_tariff_codes(hass, client, account_id)
_LOGGER.debug(f'tariff_codes: {tariff_codes}')
Expand All @@ -50,10 +48,17 @@ async def async_update_electricity_rates_data():
key = meter_point

new_rates = None
try:
new_rates = await client.async_get_electricity_rates(tariff_code, is_smart_meter, period_from, period_to)
except:
_LOGGER.debug('Failed to retrieve electricity rates')
if ((current.minute % 30) == 0 or
DATA_RATES not in hass.data[DOMAIN] or
hass.data[DOMAIN][DATA_RATES] is None or
key not in hass.data[DOMAIN][DATA_RATES] or
hass.data[DOMAIN][DATA_RATES][key][-1]["valid_from"] < period_from):
try:
new_rates = await client.async_get_electricity_rates(tariff_code, is_smart_meter, period_from, period_to)
except:
_LOGGER.debug('Failed to retrieve electricity rates')
else:
new_rates = hass.data[DOMAIN][DATA_RATES][key]

if new_rates is not None:
if is_intelligent_tariff(tariff_code):
Expand Down
9 changes: 7 additions & 2 deletions custom_components/octopus_energy/coordinators/gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ async def async_update_data():
current = utcnow()

rate_key = f'{DATA_GAS_RATES}_{tariff_code}'
if (rate_key not in hass.data[DOMAIN] or (current.minute % 30) == 0 or hass.data[DOMAIN][rate_key] is None or len(hass.data[DOMAIN][rate_key]) == 0):
period_from = as_utc(parse_datetime(current.strftime("%Y-%m-%dT00:00:00Z")))
period_from = as_utc(parse_datetime(current.strftime("%Y-%m-%dT00:00:00Z")))

if (rate_key not in hass.data[DOMAIN] or
(current.minute % 30) == 0 or
hass.data[DOMAIN][rate_key] is None or
len(hass.data[DOMAIN][rate_key]) == 0 or
hass.data[DOMAIN][rate_key][-1]["valid_from"] < period_from):
period_to = as_utc(parse_datetime((current + timedelta(days=1)).strftime("%Y-%m-%dT00:00:00Z")))

try:
Expand Down

0 comments on commit a50e0bc

Please sign in to comment.