Skip to content

Commit 9409984

Browse files
authored
Improve error handling for planned dispatches
Refactor planned dispatches handling to check for key existence before accessing data.
1 parent f232630 commit 9409984

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

custom_components/octopus_germany/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ async def process_api_data(data, account_number, api):
391391
)
392392

393393
# Handle dispatch data if it exists
394-
planned_dispatches = data.get("plannedDispatches", [])
395-
if planned_dispatches is None: # API error - keep previous cached data
396-
# Preserve existing planned_dispatches from previous successful fetch
394+
# Check if key exists at all - if not, API had error and we should cache
395+
if "plannedDispatches" not in data:
396+
# API error - plannedDispatches was removed from result, use cached data
397397
if (
398398
account_number in self.data
399399
and "planned_dispatches" in self.data[account_number]
@@ -406,6 +406,10 @@ async def process_api_data(data, account_number, api):
406406
)
407407
else:
408408
planned_dispatches = [] # No previous data available
409+
else:
410+
planned_dispatches = data.get("plannedDispatches")
411+
if planned_dispatches is None: # Explicit None from API
412+
planned_dispatches = []
409413
result_data[account_number]["planned_dispatches"] = planned_dispatches
410414

411415
completed_dispatches = data.get("completedDispatches", [])

0 commit comments

Comments
 (0)