Skip to content

Commit

Permalink
Elegant outstanding fetch cancel on reconfigure
Browse files Browse the repository at this point in the history
  • Loading branch information
autoSteve committed Oct 18, 2024
1 parent 882606e commit 95b3b7d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions custom_components/solcast_solar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry):

def tasks_cancel():
try:
# Terminate solcastapi tasks in progress
for task, cancel in coordinator.solcast.tasks.items():
_LOGGER.debug("Cancelling solcastapi task %s", task)
cancel.cancel()
# Terminate coordinator tasks in progress
for task, cancel in coordinator.tasks.items():
_LOGGER.debug("Cancelling coordinator task %s", task)
Expand All @@ -530,10 +534,6 @@ def tasks_cancel():
else:
cancel()
coordinator.tasks = {}
# Terminate solcastapi tasks in progress
for task, cancel in coordinator.solcast.tasks.items():
_LOGGER.debug("Cancelling solcastapi task %s", task)
cancel.cancel()
except Exception as e:
_LOGGER.error("Cancelling tasks failed: %s: %s", e, traceback.format_exc())
coordinator.solcast.tasks = {}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/solcast_solar/solcastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,8 @@ async def __fetch_data(self, hours, path="error", site="", api_key="", cachednam
if FORECAST_DEBUG_LOGGING:
_LOGGER.debug("HTTP session returned: %s", str(d))
return d
except asyncio.exceptions.CancelledError:
_LOGGER.debug('Fetch cancelled')
except ConnectionRefusedError as e:
_LOGGER.error("Connection error in __fetch_data(), connection refused: %s", e)
except ClientConnectionError as e:
Expand Down

0 comments on commit 95b3b7d

Please sign in to comment.