Skip to content

Commit

Permalink
chore: More adjustments to debug information
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Dec 11, 2023
1 parent 740629d commit 8a30b38
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 59 deletions.
6 changes: 3 additions & 3 deletions custom_components/octopus_energy/api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,11 @@ async def __async_read_response__(self, response, url):
if response.status >= 400:
if response.status >= 500:
msg = f'DO NOT REPORT - Octopus Energy server error ({url}): {response.status}; {text}'
_LOGGER.debug(msg)
_LOGGER.warn(msg)
raise ServerError(msg)
elif response.status not in [401, 403, 404]:
msg = f'Failed to send request ({url}): {response.status}; {text}'
_LOGGER.debug(msg)
_LOGGER.warn(msg)
raise RequestError(msg, [])
return None

Expand All @@ -1153,7 +1153,7 @@ async def __async_read_response__(self, response, url):
if ("graphql" in url and "errors" in data_as_json):
msg = f'Errors in request ({url}): {data_as_json["errors"]}'
errors = list(map(lambda error: error["message"], data_as_json["errors"]))
_LOGGER.debug(msg)
_LOGGER.warn(msg)
raise RequestError(msg, errors)

return data_as_json
7 changes: 3 additions & 4 deletions custom_components/octopus_energy/coordinators/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ async def async_refresh_account(

return AccountCoordinatorResult(current, 1, account_info)
except:
# count exceptions as failure to retrieve account
_LOGGER.debug('Failed to retrieve account information')

return AccountCoordinatorResult(
result = AccountCoordinatorResult(
previous_request.last_retrieved,
previous_request.request_attempts + 1,
previous_request.account
)
_LOGGER.warn(f'Failed to retrieve account information - using cached version. Next attempt at {result.next_refresh}')
return result

return previous_request

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ async def async_get_live_consumption(
if result is not None:
return CurrentConsumptionCoordinatorResult(current_date, 1, refresh_rate_in_minutes, result)
except:
_LOGGER.debug('Failed to retrieve smart meter consumption data')

result: CurrentConsumptionCoordinatorResult = None
if previous_consumption is not None:
return CurrentConsumptionCoordinatorResult(
result = CurrentConsumptionCoordinatorResult(
previous_consumption.last_retrieved,
previous_consumption.request_attempts + 1,
refresh_rate_in_minutes,
previous_consumption.data
)
_LOGGER.warn(f'Failed to retrieve smart meter consumption data - using cached version. Next attempt at {result.next_refresh}')
else:
result = CurrentConsumptionCoordinatorResult(
# We want to force into our fallback mode
current_date - timedelta(minutes=refresh_rate_in_minutes),
2,
refresh_rate_in_minutes,
None
)
_LOGGER.warn(f'Failed to retrieve smart meter consumption data. Next attempt at {result.next_refresh}')

return CurrentConsumptionCoordinatorResult(
# We want to force into our fallback mode
current_date - timedelta(minutes=refresh_rate_in_minutes),
2,
refresh_rate_in_minutes,
None
)
return result

return previous_consumption

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ async def async_refresh_electricity_rates_data(
EVENT_ELECTRICITY_NEXT_DAY_RATES)

return ElectricityRatesCoordinatorResult(current, 1, new_rates)

elif (existing_rates_result is not None):
_LOGGER.debug(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number}, so using cached rates")
return ElectricityRatesCoordinatorResult(existing_rates_result.last_retrieved, existing_rates_result.request_attempts + 1, existing_rates_result.rates)

result = None
if (existing_rates_result is not None):
result = ElectricityRatesCoordinatorResult(existing_rates_result.last_retrieved, existing_rates_result.request_attempts + 1, existing_rates_result.rates)
_LOGGER.warn(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number} - using cached rates. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return ElectricityRatesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_RATES), 2, None)
result = ElectricityRatesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_RATES), 2, None)
_LOGGER.warn(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number}. Next attempt at {result.next_refresh}")

return result

return existing_rates_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ async def async_refresh_electricity_standing_charges_data(

if new_standing_charge is not None:
return ElectricityStandingChargeCoordinatorResult(current, 1, new_standing_charge)
elif (existing_standing_charges_result is not None):
_LOGGER.debug(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff_code}), so using cached standing charges")
return ElectricityStandingChargeCoordinatorResult(existing_standing_charges_result.last_retrieved, existing_standing_charges_result.request_attempts + 1, existing_standing_charges_result.standing_charge)

result = None
if (existing_standing_charges_result is not None):
result = ElectricityStandingChargeCoordinatorResult(existing_standing_charges_result.last_retrieved, existing_standing_charges_result.request_attempts + 1, existing_standing_charges_result.standing_charge)
_LOGGER.warn(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff_code}) - using cached standing charges. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return ElectricityStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None)
result = ElectricityStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None)
_LOGGER.warn(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff_code}). Next attempt at {result.next_refresh}")

return result

return existing_standing_charges_result

Expand Down
12 changes: 8 additions & 4 deletions custom_components/octopus_energy/coordinators/gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ async def async_refresh_gas_rates_data(

return GasRatesCoordinatorResult(current, 1, new_rates)

elif (existing_rates_result is not None):
_LOGGER.debug(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number}, so using cached rates")
return GasRatesCoordinatorResult(existing_rates_result.last_retrieved, existing_rates_result.request_attempts + 1, existing_rates_result.rates)
result = None
if (existing_rates_result is not None):
result = GasRatesCoordinatorResult(existing_rates_result.last_retrieved, existing_rates_result.request_attempts + 1, existing_rates_result.rates)
_LOGGER.warn(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number} - using cached rates. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return GasRatesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_RATES), 2, None)
result = GasRatesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_RATES), 2, None)
_LOGGER.warn(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number}. Next attempt at {result.next_refresh}")

return result

return existing_rates_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ async def async_refresh_gas_standing_charges_data(

if new_standing_charge is not None:
return GasStandingChargeCoordinatorResult(current, 1, new_standing_charge)
elif (existing_standing_charges_result is not None):
_LOGGER.debug(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff_code}), so using cached standing charges")
return GasStandingChargeCoordinatorResult(existing_standing_charges_result.last_retrieved, existing_standing_charges_result.request_attempts + 1, existing_standing_charges_result.standing_charge)

result = None
if (existing_standing_charges_result is not None):
result = GasStandingChargeCoordinatorResult(existing_standing_charges_result.last_retrieved, existing_standing_charges_result.request_attempts + 1, existing_standing_charges_result.standing_charge)
_LOGGER.warn(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff_code}) - using cached standing charges. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return GasStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None)
result = GasStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None)
_LOGGER.warn(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff_code}). Next attempt at {result.next_refresh}")

return result

return existing_standing_charges_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ async def async_refresh_intelligent_dispatches(
if dispatches is not None:
dispatches.completed = await async_merge_dispatch_data(account_id, dispatches.completed)
return IntelligentDispatchesCoordinatorResult(current, 1, dispatches)
elif (existing_intelligent_dispatches_result is not None):
_LOGGER.debug(f"Failed to retrieve new dispatches, so using cached dispatches")
return IntelligentDispatchesCoordinatorResult(

result = None
if (existing_intelligent_dispatches_result is not None):
result = IntelligentDispatchesCoordinatorResult(
existing_intelligent_dispatches_result.last_retrieved,
existing_intelligent_dispatches_result.request_attempts + 1,
existing_intelligent_dispatches_result.dispatches
)
_LOGGER.warn(f"Failed to retrieve new dispatches - using cached dispatches. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return IntelligentDispatchesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None)
result = IntelligentDispatchesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None)
_LOGGER.warn(f"Failed to retrieve new dispatches. Next attempt at {result.next_refresh}")

return result

return existing_intelligent_dispatches_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,21 @@ async def async_refresh_intelligent_settings(

if settings is not None:
return IntelligentCoordinatorResult(current, 1, settings)
elif (existing_intelligent_settings_result is not None):
_LOGGER.debug(f"Failed to retrieve new intelligent settings, so using cached settings")
return IntelligentCoordinatorResult(

result = None
if (existing_intelligent_settings_result is not None):
result = IntelligentCoordinatorResult(
existing_intelligent_settings_result.last_retrieved,
existing_intelligent_settings_result.request_attempts + 1,
existing_intelligent_settings_result.settings
)
_LOGGER.warn(f"Failed to retrieve new intelligent settings - using cached settings. Next attempt at {result.next_refresh}")
else:
# We want to force into our fallback mode
return IntelligentCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None)
result = IntelligentCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None)
_LOGGER.warn(f"Failed to retrieve new intelligent settings. Next attempt at {result.next_refresh}")

return result

return existing_intelligent_settings_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,28 @@ async def async_fetch_consumption_and_rates(
previous_data.standing_charge
)
except:
_LOGGER.debug(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}")

result = None
if previous_data is not None:
return PreviousConsumptionCoordinatorResult(
result = PreviousConsumptionCoordinatorResult(
previous_data.last_retrieved,
previous_data.request_attempts + 1,
previous_data.consumption,
previous_data.rates,
previous_data.standing_charge
)
_LOGGER.warn(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number} - using cached data. Next attempt at {result.next_refresh}")
else:
return PreviousConsumptionCoordinatorResult(
result = PreviousConsumptionCoordinatorResult(
# We want to force into our fallback mode
current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION),
2,
None,
None,
None
)
_LOGGER.warn(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}. Next attempt at {result.next_refresh}")

return result

return previous_data

Expand Down
11 changes: 7 additions & 4 deletions custom_components/octopus_energy/coordinators/saving_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,26 @@ async def async_refresh_saving_sessions(

return SavingSessionsCoordinatorResult(current, 1, available_events, result.joined_events)
except:
_LOGGER.debug('Failed to retrieve saving session information')

result = None
if (existing_saving_sessions_result is not None):
return SavingSessionsCoordinatorResult(
result = SavingSessionsCoordinatorResult(
existing_saving_sessions_result.last_retrieved,
existing_saving_sessions_result.request_attempts + 1,
existing_saving_sessions_result.available_events,
existing_saving_sessions_result.joined_events
)
_LOGGER.warn(f"Failed to retrieve saving sessions - using cached data. Next attempt at {result.next_refresh}")
else:
return SavingSessionsCoordinatorResult(
result = SavingSessionsCoordinatorResult(
# We want to force into our fallback mode
current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_OCTOPLUS_SAVING_SESSIONS),
2,
[],
[]
)
_LOGGER.warn(f"Failed to retrieve saving sessions. Next attempt at {result.next_refresh}")

return result

return existing_saving_sessions_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ async def async_refresh_wheel_of_fortune_spins(

return WheelOfFortuneSpinsCoordinatorResult(current, 1, result)
except:
_LOGGER.debug('Failed to retrieve wheel of fortune spins')

result = None
if (existing_result is not None):
return WheelOfFortuneSpinsCoordinatorResult(existing_result.last_retrieved, existing_result.request_attempts + 1, existing_result.spins)
result = WheelOfFortuneSpinsCoordinatorResult(existing_result.last_retrieved, existing_result.request_attempts + 1, existing_result.spins)
_LOGGER.warn(f'Failed to retrieve wheel of fortune spins - using cached data. Next attempt at {result.next_refresh}')
else:
return WheelOfFortuneSpinsCoordinatorResult(
result = WheelOfFortuneSpinsCoordinatorResult(
# We want to force into our fallback mode
current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_OCTOPLUS_WHEEL_OF_FORTUNE),
2,
None
)
_LOGGER.warn(f'Failed to retrieve wheel of fortune spins. Next attempt at {result.next_refresh}')

return result

return existing_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,13 @@ async def async_update(self):
self._last_retrieved = current
self._next_refresh = calculate_next_refresh(current, self._request_attempts, REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION)
except:
_LOGGER.debug('Failed to retrieve previous accumulative cost override data')
self._request_attempts = self._request_attempts + 1
self._next_refresh = calculate_next_refresh(
self._last_retrieved if self._last_retrieved is not None else current,
self._request_attempts,
REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION
)

_LOGGER.debug(f'last_retrieved: {self._last_retrieved}; request_attempts: {self._request_attempts}; refresh_rate_in_minutes: {REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION}; next_refresh: {self._next_refresh}')
_LOGGER.warn(f'Failed to retrieve previous accumulative cost override data - using cached data. Next attempt at {self._next_refresh}')

if result is not None:
self._attributes["data_last_retrieved"] = result.last_retrieved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,14 @@ async def async_update(self):
self._last_retrieved = current
self._next_refresh = calculate_next_refresh(current, self._request_attempts, REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION)
except:
_LOGGER.debug('Failed to retrieve previous accumulative cost override data')
self._request_attempts = self._request_attempts + 1
self._next_refresh = calculate_next_refresh(
self._last_retrieved if self._last_retrieved is not None else current,
self._request_attempts,
REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION
)
_LOGGER.warn(f'Failed to retrieve previous accumulative cost override data - using cached data. Next attempt at {self._next_refresh}')

_LOGGER.debug(f'last_retrieved: {self._last_retrieved}; request_attempts: {self._request_attempts}; refresh_rate_in_minutes: {REFRESH_RATE_IN_MINUTES_PREVIOUS_CONSUMPTION}; next_refresh: {self._next_refresh}')
if result is not None:
self._attributes["data_last_retrieved"] = result.last_retrieved

Expand Down

0 comments on commit 8a30b38

Please sign in to comment.