mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-04-19 10:57:14 +00:00
Adjust development documentation with context (#1355)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
e3a42e3cd2
commit
9c52c1f5fd
11
blog/2022-06-02-update_coordinator-context.md
Normal file
11
blog/2022-06-02-update_coordinator-context.md
Normal 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.
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user