Document using async_on_remove with DataUpdateCoordinator (#456)

This commit is contained in:
Paulus Schoutsen 2020-04-03 11:06:20 -07:00 committed by GitHub
parent 5bc46ac6d0
commit 3699477967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,7 @@ Your integration will need to fetch data from an API to be able to provide this
APIs come in many different shapes and forms but at its core they fall in two categories: push and poll.
With push, we subscribe to an API and we get notified by the API when new data is available. It pushes the changes to us. Push APIs are great because they consume less resources. When a change happens, we can get notified of a change and don't have to re-fetch all the data and find changes. Because entities can be disabled, you should make sure that your entity subscribes inside the `async_added_to_hass` callback and unsubscribes inside `async_will_remove_from_hass`.
With push, we subscribe to an API and we get notified by the API when new data is available. It pushes the changes to us. Push APIs are great because they consume less resources. When a change happens, we can get notified of a change and don't have to re-fetch all the data and find changes. Because entities can be disabled, you should make sure that your entity subscribes inside the `async_added_to_hass` callback and unsubscribes on remove.
With polling, we will fetch the latest data from the API at a specified interval. Your integration will then supply this data to its entity, which is written to Home Assistant.
@ -100,14 +100,10 @@ class MyEntity(entity.Entity):
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self.coordinator.async_add_listener(
self.async_write_ha_state
)
async def async_will_remove_from_hass(self):
"""When entity will be removed from hass."""
self.coordinator.async_remove_listener(
self.async_write_ha_state
)
async def async_turn_on(self, **kwargs):