Make device registry cleanup a callback function (#111052)
* Add async_schedule_call to the Debouncer async_schedule_call allows the Debouncer to schedule a call from a callback without having to create tasks to run async_call * Update homeassistant/helpers/debounce.py * Make device registry cleanup all callback function * fix typing, code supported callback functions, but typing did not * fixes * fixes * fix * we had no coverage for other job types * we had no coverage for other job types
This commit is contained in:
parent
c048b840fc
commit
776a9b8691
2 changed files with 11 additions and 9 deletions
|
@ -2,7 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import UserDict
|
from collections import UserDict
|
||||||
from collections.abc import Coroutine, ValuesView
|
from collections.abc import ValuesView
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
|
@ -1098,18 +1098,20 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None:
|
||||||
listener=_handle_label_registry_update, # type: ignore[arg-type]
|
listener=_handle_label_registry_update, # type: ignore[arg-type]
|
||||||
)
|
)
|
||||||
|
|
||||||
async def cleanup() -> None:
|
@callback
|
||||||
|
def _async_cleanup() -> None:
|
||||||
"""Cleanup."""
|
"""Cleanup."""
|
||||||
ent_reg = entity_registry.async_get(hass)
|
ent_reg = entity_registry.async_get(hass)
|
||||||
async_cleanup(hass, dev_reg, ent_reg)
|
async_cleanup(hass, dev_reg, ent_reg)
|
||||||
|
|
||||||
debounced_cleanup: Debouncer[Coroutine[Any, Any, None]] = Debouncer(
|
debounced_cleanup: Debouncer[None] = Debouncer(
|
||||||
hass, _LOGGER, cooldown=CLEANUP_DELAY, immediate=False, function=cleanup
|
hass, _LOGGER, cooldown=CLEANUP_DELAY, immediate=False, function=_async_cleanup
|
||||||
)
|
)
|
||||||
|
|
||||||
async def entity_registry_changed(event: Event) -> None:
|
@callback
|
||||||
|
def _async_entity_registry_changed(event: Event) -> None:
|
||||||
"""Handle entity updated or removed dispatch."""
|
"""Handle entity updated or removed dispatch."""
|
||||||
await debounced_cleanup.async_call()
|
debounced_cleanup.async_schedule_call()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def entity_registry_changed_filter(event: Event) -> bool:
|
def entity_registry_changed_filter(event: Event) -> bool:
|
||||||
|
@ -1125,7 +1127,7 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None:
|
||||||
if hass.is_running:
|
if hass.is_running:
|
||||||
hass.bus.async_listen(
|
hass.bus.async_listen(
|
||||||
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
|
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
|
||||||
entity_registry_changed,
|
_async_entity_registry_changed,
|
||||||
event_filter=entity_registry_changed_filter,
|
event_filter=entity_registry_changed_filter,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
@ -1134,7 +1136,7 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None:
|
||||||
"""Clean up on startup."""
|
"""Clean up on startup."""
|
||||||
hass.bus.async_listen(
|
hass.bus.async_listen(
|
||||||
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
|
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
|
||||||
entity_registry_changed,
|
_async_entity_registry_changed,
|
||||||
event_filter=entity_registry_changed_filter,
|
event_filter=entity_registry_changed_filter,
|
||||||
)
|
)
|
||||||
await debounced_cleanup.async_call()
|
await debounced_cleanup.async_call()
|
||||||
|
|
|
@ -1590,7 +1590,7 @@ async def test_cleanup_entity_registry_change(hass: HomeAssistant) -> None:
|
||||||
ent_reg = er.async_get(hass)
|
ent_reg = er.async_get(hass)
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.helpers.device_registry.Debouncer.async_call"
|
"homeassistant.helpers.device_registry.Debouncer.async_schedule_call"
|
||||||
) as mock_call:
|
) as mock_call:
|
||||||
entity = ent_reg.async_get_or_create("light", "hue", "e1")
|
entity = ent_reg.async_get_or_create("light", "hue", "e1")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
Loading…
Add table
Reference in a new issue