Skip to content

Commit

Permalink
feat: Reduced number of warnings outputted when data can't be retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Nov 29, 2024
1 parent 779d0c3 commit 0a2eb52
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 30 deletions.
5 changes: 4 additions & 1 deletion custom_components/octopus_energy/coordinators/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ async def async_refresh_account(
previous_request.account,
last_error=e
)
_LOGGER.warning(f'Failed to retrieve account information - using cached version. Next attempt at {result.next_refresh}')

if (result.request_attempts == 2):
_LOGGER.warning(f'Failed to retrieve account information - using cached version. See diagnostics sensor for more information.')

return result

return previous_request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async def async_get_live_consumption(
previous_consumption.data,
last_error=e
)
_LOGGER.warning(f'Failed to retrieve smart meter consumption data - using cached version. Next attempt at {result.next_refresh}')

if (result.request_attempts == 2):
_LOGGER.warning(f'Failed to retrieve smart meter consumption data - using cached version. See diagnostics sensor for more information.')
else:
result = CurrentConsumptionCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -63,7 +65,7 @@ async def async_get_live_consumption(
None,
last_error=e
)
_LOGGER.warning(f'Failed to retrieve smart meter consumption data. Next attempt at {result.next_refresh}')
_LOGGER.warning(f'Failed to retrieve smart meter consumption data. See diagnostics sensor for more information.')

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ async def async_get_home_pro_consumption(
REFRESH_RATE_IN_MINUTES_HOME_PRO_CONSUMPTION,
previous_consumption.data
)
_LOGGER.warning(f'Failed to retrieve current consumption data from home pro data - using cached version. Next attempt at {result.next_refresh}')

if (result.request_attempts == 2):
_LOGGER.warning(f'Failed to retrieve current consumption data from home pro data - using cached version. See diagnostics sensor for more information.')
else:
result = CurrentConsumptionCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -54,7 +56,8 @@ async def async_get_home_pro_consumption(
REFRESH_RATE_IN_MINUTES_HOME_PRO_CONSUMPTION,
None
)
_LOGGER.warning(f'Failed to retrieve current consumption data from home pro data. Next attempt at {result.next_refresh}')

_LOGGER.warning(f'Failed to retrieve current consumption data from home pro data. See diagnostics sensor for more information.')

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ async def async_refresh_electricity_rates_data(
existing_rates_result.rates_last_adjusted,
last_error=raised_exception
)
_LOGGER.warning(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number} - using cached rates. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number} - using cached rates. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = ElectricityRatesCoordinatorResult(
Expand All @@ -153,7 +155,7 @@ async def async_refresh_electricity_rates_data(
None,
last_error=raised_exception
)
_LOGGER.warning(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number}. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new electricity rates for {target_mpan}/{target_serial_number}. See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ async def async_refresh_electricity_standing_charges_data(
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, last_error=raised_exception)
_LOGGER.warning(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}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff.code}) - using cached standing charges. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = ElectricityStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff.code}). Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new electricity standing charges for {target_mpan}/{target_serial_number} ({tariff.code}). See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ async def async_refresh_free_electricity_sessions(
existing_free_electricity_sessions_result.events,
last_error=e
)
_LOGGER.warning(f"Failed to retrieve free electricity sessions - using cached data. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve free electricity sessions - using cached data. See diagnostics sensor for more information.")
else:
result = FreeElectricitySessionsCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -93,7 +95,7 @@ async def async_refresh_free_electricity_sessions(
[],
last_error=e
)
_LOGGER.warning(f"Failed to retrieve free electricity sessions. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve free electricity sessions. See diagnostics sensor for more information.")

return result

Expand Down
6 changes: 4 additions & 2 deletions custom_components/octopus_energy/coordinators/gas_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ async def async_refresh_gas_rates_data(
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, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number} - using cached rates. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number} - using cached rates. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = GasRatesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_RATES), 2, None, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number}. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new gas rates for {target_mprn}/{target_serial_number}. See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ async def async_refresh_gas_standing_charges_data(
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, last_error=raised_exception)
_LOGGER.warning(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}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff.code}) - using cached standing charges. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = GasStandingChargeCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_STANDING_CHARGE), 2, None, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff.code}). Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new gas standing charges for {target_mprn}/{target_serial_number} ({tariff.code}). See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ async def async_refresh_greenness_forecast(
result = None
if (existing_result is not None):
result = GreennessForecastCoordinatorResult(existing_result.last_retrieved, existing_result.request_attempts + 1, existing_result.forecast, last_error=e)
_LOGGER.warning(f'Failed to retrieve greenness forecast - using cached data. Next attempt at {result.next_refresh}')

if (result.request_attempts == 2):
_LOGGER.warning(f'Failed to retrieve greenness forecast - using cached data. See diagnostics sensor for more information.')
else:
result = GreennessForecastCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -55,7 +57,7 @@ async def async_refresh_greenness_forecast(
None,
last_error=e
)
_LOGGER.warning(f'Failed to retrieve greenness forecast. Next attempt at {result.next_refresh}')
_LOGGER.warning(f'Failed to retrieve greenness forecast. See diagnostics sensor for more information.')

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ async def async_refresh_intelligent_dispatches(
existing_intelligent_dispatches_result.dispatches,
last_error=raised_exception
)
_LOGGER.warning(f"Failed to retrieve new dispatches - using cached dispatches. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new dispatches - using cached dispatches. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = IntelligentDispatchesCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new dispatches. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new dispatches. See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ async def async_refresh_intelligent_settings(
existing_intelligent_settings_result.settings,
last_error=raised_exception
)
_LOGGER.warning(f"Failed to retrieve new intelligent settings - using cached settings. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve new intelligent settings - using cached settings. See diagnostics sensor for more information.")
else:
# We want to force into our fallback mode
result = IntelligentCoordinatorResult(current - timedelta(minutes=REFRESH_RATE_IN_MINUTES_INTELLIGENT), 2, None, last_error=raised_exception)
_LOGGER.warning(f"Failed to retrieve new intelligent settings. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve new intelligent settings. See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ async def async_get_missing_consumption(

def remove_old_consumptions(consumptions: list, earliest_datetime: datetime):
new_rates = []
for consumption in consumptions:
if (consumption["start"] >= earliest_datetime):
new_rates.append(consumption)
if consumptions is not None:
for consumption in consumptions:
if (consumption["start"] >= earliest_datetime):
new_rates.append(consumption)

return new_rates

Expand Down Expand Up @@ -362,7 +363,9 @@ async def async_fetch_consumption_and_rates(
previous_data.historic_weekend_consumption,
last_error=e
)
_LOGGER.warning(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}. Exception: {e}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number} - using cached data. See diagnostics sensor for more information.. Exception: {e}")
else:
result = PreviousConsumptionCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -375,7 +378,7 @@ async def async_fetch_consumption_and_rates(
None,
last_error=e
)
_LOGGER.warning(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}. Next attempt at {result.next_refresh}. Exception: {e}")
_LOGGER.warning(f"Failed to retrieve previous consumption data for {'electricity' if is_electricity else 'gas'} {identifier}/{serial_number}. See diagnostics sensor for more information.. Exception: {e}")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ async def async_refresh_saving_sessions(
existing_saving_sessions_result.joined_events,
last_error=e
)
_LOGGER.warning(f"Failed to retrieve saving sessions - using cached data. Next attempt at {result.next_refresh}")

if (result.request_attempts == 2):
_LOGGER.warning(f"Failed to retrieve saving sessions - using cached data. See diagnostics sensor for more information.")
else:
result = SavingSessionsCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -136,7 +138,7 @@ async def async_refresh_saving_sessions(
[],
last_error=e
)
_LOGGER.warning(f"Failed to retrieve saving sessions. Next attempt at {result.next_refresh}")
_LOGGER.warning(f"Failed to retrieve saving sessions. See diagnostics sensor for more information.")

return result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ async def async_refresh_wheel_of_fortune_spins(
result = None
if (existing_result is not None):
result = WheelOfFortuneSpinsCoordinatorResult(existing_result.last_retrieved, existing_result.request_attempts + 1, existing_result.spins, last_error=e)
_LOGGER.warning(f'Failed to retrieve wheel of fortune spins - using cached data. Next attempt at {result.next_refresh}')
if (result.request_attempts == 2):
_LOGGER.warning(f'Failed to retrieve wheel of fortune spins - using cached data. See diagnostics sensor for more information.')
else:
result = WheelOfFortuneSpinsCoordinatorResult(
# We want to force into our fallback mode
Expand All @@ -55,7 +56,7 @@ async def async_refresh_wheel_of_fortune_spins(
None,
last_error=e
)
_LOGGER.warning(f'Failed to retrieve wheel of fortune spins. Next attempt at {result.next_refresh}')
_LOGGER.warning(f'Failed to retrieve wheel of fortune spins. See diagnostics sensor for more information.')

return result

Expand Down

0 comments on commit 0a2eb52

Please sign in to comment.