From 776a9b869145469218d4675f33c651cfdddd825c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 21 Feb 2024 02:34:49 -0600 Subject: [PATCH] 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 --- homeassistant/helpers/device_registry.py | 18 ++++++++++-------- tests/helpers/test_device_registry.py | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 91eb66c46e1..cab47258f67 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -2,7 +2,7 @@ from __future__ import annotations from collections import UserDict -from collections.abc import Coroutine, ValuesView +from collections.abc import ValuesView from enum import StrEnum from functools import partial 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] ) - async def cleanup() -> None: + @callback + def _async_cleanup() -> None: """Cleanup.""" ent_reg = entity_registry.async_get(hass) async_cleanup(hass, dev_reg, ent_reg) - debounced_cleanup: Debouncer[Coroutine[Any, Any, None]] = Debouncer( - hass, _LOGGER, cooldown=CLEANUP_DELAY, immediate=False, function=cleanup + debounced_cleanup: Debouncer[None] = Debouncer( + 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.""" - await debounced_cleanup.async_call() + debounced_cleanup.async_schedule_call() @callback 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: hass.bus.async_listen( entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, - entity_registry_changed, + _async_entity_registry_changed, event_filter=entity_registry_changed_filter, ) return @@ -1134,7 +1136,7 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None: """Clean up on startup.""" hass.bus.async_listen( entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, - entity_registry_changed, + _async_entity_registry_changed, event_filter=entity_registry_changed_filter, ) await debounced_cleanup.async_call() diff --git a/tests/helpers/test_device_registry.py b/tests/helpers/test_device_registry.py index 5af0f6e9075..f317e7b5209 100644 --- a/tests/helpers/test_device_registry.py +++ b/tests/helpers/test_device_registry.py @@ -1590,7 +1590,7 @@ async def test_cleanup_entity_registry_change(hass: HomeAssistant) -> None: ent_reg = er.async_get(hass) with patch( - "homeassistant.helpers.device_registry.Debouncer.async_call" + "homeassistant.helpers.device_registry.Debouncer.async_schedule_call" ) as mock_call: entity = ent_reg.async_get_or_create("light", "hue", "e1") await hass.async_block_till_done()