mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Data Coordinator to return unsub func (#33588)
This commit is contained in:
parent
d1663f2733
commit
dc7127aacf
@ -35,19 +35,13 @@ class HueEvent(GenericHueDevice):
|
|||||||
self._last_updated = self.sensor.lastupdated
|
self._last_updated = self.sensor.lastupdated
|
||||||
|
|
||||||
# Register callback in coordinator and add job to remove it on bridge reset.
|
# Register callback in coordinator and add job to remove it on bridge reset.
|
||||||
self.bridge.sensor_manager.coordinator.async_add_listener(
|
self.bridge.reset_jobs.append(
|
||||||
self.async_update_callback
|
self.bridge.sensor_manager.coordinator.async_add_listener(
|
||||||
|
self.async_update_callback
|
||||||
|
)
|
||||||
)
|
)
|
||||||
self.bridge.reset_jobs.append(self.async_will_remove_from_hass)
|
|
||||||
_LOGGER.debug("Hue event created: %s", self.event_id)
|
_LOGGER.debug("Hue event created: %s", self.event_id)
|
||||||
|
|
||||||
@callback
|
|
||||||
def async_will_remove_from_hass(self):
|
|
||||||
"""Remove listener on bridge reset."""
|
|
||||||
self.bridge.sensor_manager.coordinator.async_remove_listener(
|
|
||||||
self.async_update_callback
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_update_callback(self):
|
def async_update_callback(self):
|
||||||
"""Fire the event if reason is that state is updated."""
|
"""Fire the event if reason is that state is updated."""
|
||||||
|
@ -118,13 +118,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# We add a listener after fetching the data, so manually trigger listener
|
# We add a listener after fetching the data, so manually trigger listener
|
||||||
light_coordinator.async_add_listener(update_lights)
|
bridge.reset_jobs.append(light_coordinator.async_add_listener(update_lights))
|
||||||
update_lights()
|
update_lights()
|
||||||
|
|
||||||
bridge.reset_jobs.append(
|
|
||||||
lambda: light_coordinator.async_remove_listener(update_lights)
|
|
||||||
)
|
|
||||||
|
|
||||||
api_version = tuple(int(v) for v in bridge.api.config.apiversion.split("."))
|
api_version = tuple(int(v) for v in bridge.api.config.apiversion.split("."))
|
||||||
|
|
||||||
allow_groups = bridge.allow_groups
|
allow_groups = bridge.allow_groups
|
||||||
@ -155,13 +151,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
partial(create_light, HueLight, group_coordinator, bridge, True),
|
partial(create_light, HueLight, group_coordinator, bridge, True),
|
||||||
)
|
)
|
||||||
|
|
||||||
group_coordinator.async_add_listener(update_groups)
|
bridge.reset_jobs.append(group_coordinator.async_add_listener(update_groups))
|
||||||
await group_coordinator.async_refresh()
|
await group_coordinator.async_refresh()
|
||||||
|
|
||||||
bridge.reset_jobs.append(
|
|
||||||
lambda: group_coordinator.async_remove_listener(update_groups)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_safe_fetch(bridge, fetch_method):
|
async def async_safe_fetch(bridge, fetch_method):
|
||||||
"""Safely fetch data."""
|
"""Safely fetch data."""
|
||||||
@ -339,11 +331,9 @@ class HueLight(Light):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
self.coordinator.async_add_listener(self.async_write_ha_state)
|
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):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the specified or all lights on."""
|
"""Turn the specified or all lights on."""
|
||||||
|
@ -76,9 +76,8 @@ class SensorManager:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# We have all components available, start the updating.
|
# We have all components available, start the updating.
|
||||||
self.coordinator.async_add_listener(self.async_update_items)
|
|
||||||
self.bridge.reset_jobs.append(
|
self.bridge.reset_jobs.append(
|
||||||
lambda: self.coordinator.async_remove_listener(self.async_update_items)
|
self.coordinator.async_add_listener(self.async_update_items)
|
||||||
)
|
)
|
||||||
await self.coordinator.async_refresh()
|
await self.coordinator.async_refresh()
|
||||||
|
|
||||||
@ -178,14 +177,10 @@ class GenericHueSensor(GenericHueDevice, entity.Entity):
|
|||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
self.bridge.sensor_manager.coordinator.async_add_listener(
|
self.async_on_remove(
|
||||||
self.async_write_ha_state
|
self.bridge.sensor_manager.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.bridge.sensor_manager.coordinator.async_remove_listener(
|
|
||||||
self.async_write_ha_state
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
|
@ -62,7 +62,7 @@ class DataUpdateCoordinator:
|
|||||||
self._debounced_refresh = request_refresh_debouncer
|
self._debounced_refresh = request_refresh_debouncer
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_add_listener(self, update_callback: CALLBACK_TYPE) -> None:
|
def async_add_listener(self, update_callback: CALLBACK_TYPE) -> Callable[[], None]:
|
||||||
"""Listen for data updates."""
|
"""Listen for data updates."""
|
||||||
schedule_refresh = not self._listeners
|
schedule_refresh = not self._listeners
|
||||||
|
|
||||||
@ -72,6 +72,13 @@ class DataUpdateCoordinator:
|
|||||||
if schedule_refresh:
|
if schedule_refresh:
|
||||||
self._schedule_refresh()
|
self._schedule_refresh()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def remove_listener() -> None:
|
||||||
|
"""Remove update listener."""
|
||||||
|
self.async_remove_listener(update_callback)
|
||||||
|
|
||||||
|
return remove_listener
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_remove_listener(self, update_callback: CALLBACK_TYPE) -> None:
|
def async_remove_listener(self, update_callback: CALLBACK_TYPE) -> None:
|
||||||
"""Remove data update."""
|
"""Remove data update."""
|
||||||
|
@ -18,11 +18,12 @@ LOGGER = logging.getLogger(__name__)
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def crd(hass):
|
def crd(hass):
|
||||||
"""Coordinator mock."""
|
"""Coordinator mock."""
|
||||||
calls = []
|
calls = 0
|
||||||
|
|
||||||
async def refresh():
|
async def refresh():
|
||||||
calls.append(None)
|
nonlocal calls
|
||||||
return len(calls)
|
calls += 1
|
||||||
|
return calls
|
||||||
|
|
||||||
crd = update_coordinator.DataUpdateCoordinator(
|
crd = update_coordinator.DataUpdateCoordinator(
|
||||||
hass,
|
hass,
|
||||||
@ -46,16 +47,19 @@ async def test_async_refresh(crd):
|
|||||||
def update_callback():
|
def update_callback():
|
||||||
updates.append(crd.data)
|
updates.append(crd.data)
|
||||||
|
|
||||||
crd.async_add_listener(update_callback)
|
unsub = crd.async_add_listener(update_callback)
|
||||||
|
|
||||||
await crd.async_refresh()
|
await crd.async_refresh()
|
||||||
|
|
||||||
assert updates == [2]
|
assert updates == [2]
|
||||||
|
|
||||||
crd.async_remove_listener(update_callback)
|
# Test unsubscribing through function
|
||||||
|
unsub()
|
||||||
await crd.async_refresh()
|
await crd.async_refresh()
|
||||||
|
assert updates == [2]
|
||||||
|
|
||||||
|
# Test unsubscribing through method
|
||||||
|
crd.async_add_listener(update_callback)
|
||||||
|
crd.async_remove_listener(update_callback)
|
||||||
|
await crd.async_refresh()
|
||||||
assert updates == [2]
|
assert updates == [2]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user