Adjust development documentation with context (#1355)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Joakim Plate 2023-01-03 16:18:25 +01:00 committed by GitHub
parent e3a42e3cd2
commit 9c52c1f5fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -0,0 +1,11 @@
---
author: Joakim Plate
authorURL: https://github.com/elupus
title: "Support context in update coordinator"
---
Starting with Home Assistant 2022.7, the update coordinator supports keeping track of context for each listening entity. This can be used to limit the requests against APIs based on enabled entities.
This could be a breaking change for custom components that rely on update coordinators and inspect the internal variable `self._listeners` and/or overload the method async_remove_listener() to detect when there is no listeners anymore. Switch to using `async_update_listeners()` to trigger updates on all listeners, and overload `_unschedule_refresh()` to detect when there is no listeners.
See the updated [integration fetching documentation](/docs/integration_fetching_data/#polling-api-endpoints) for more information on current use.

View File

@ -95,7 +95,11 @@ class MyCoordinator(DataUpdateCoordinator):
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(10):
return await self.my_api.fetch_data()
# Grab active context variables to limit data required to be fetched from API
# Note: using context is not required if there is no need or ability to limit
# data retrieved from API.
listening_idx = set(self.async_contexts())
return await self.my_api.fetch_data(listening_idx)
except ApiAuthError as err:
# Raising ConfigEntryAuthFailed will cancel future updates
# and start a config flow with SOURCE_REAUTH (async_step_reauth)
@ -117,7 +121,7 @@ class MyEntity(CoordinatorEntity, LightEntity):
def __init__(self, coordinator, idx):
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
super().__init__(coordinator, context=idx)
self.idx = idx
@callback