1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add event helper to dispatch device registry updates by device_id (#93602)

* Add event helper to dispatch device registry updates by device_id

* Update homeassistant/helpers/event.py

* dry

* dry

* reduce

* reduce

* reorder

* reduce

* cleanup stack depth

* dry

* fix double lookup

* remove unused

* collapse
This commit is contained in:
J. Nick Koston
2023-05-27 19:04:09 -05:00
committed by GitHub
parent e1b7d68134
commit 0a9c4f15c4
2 changed files with 315 additions and 207 deletions

View File

@@ -18,12 +18,14 @@ from homeassistant.const import MATCH_ALL
import homeassistant.core as ha
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.helpers.event import (
TrackStates,
TrackTemplate,
TrackTemplateResult,
async_call_later,
async_track_device_registry_updated_event,
async_track_entity_registry_updated_event,
async_track_point_in_time,
async_track_point_in_utc_time,
@@ -4564,3 +4566,103 @@ async def test_async_track_entity_registry_updated_event_with_empty_list(
unsub_single2()
unsub_single()
async def test_async_track_device_registry_updated_event(hass: HomeAssistant) -> None:
"""Test tracking device registry updates for an device_id."""
device_id = "b92c0f06fbc911edacc9eea8ae14f866"
device_id2 = "747bbf22fbca11ed843aeea8ae14f866"
untracked_device_id = "bda93f86fbc911edacc9eea8ae14f866"
single_event_data = []
multiple_event_data = []
@ha.callback
def single_device_id_callback(event: ha.Event) -> None:
single_event_data.append(event.data)
@ha.callback
def multiple_device_id_callback(event: ha.Event) -> None:
multiple_event_data.append(event.data)
unsub1 = async_track_device_registry_updated_event(
hass, device_id, single_device_id_callback
)
unsub2 = async_track_device_registry_updated_event(
hass, [device_id, device_id2], multiple_device_id_callback
)
hass.bus.async_fire(
EVENT_DEVICE_REGISTRY_UPDATED, {"action": "create", "device_id": device_id}
)
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "create", "device_id": untracked_device_id},
)
await hass.async_block_till_done()
assert len(single_event_data) == 1
assert len(multiple_event_data) == 1
hass.bus.async_fire(
EVENT_DEVICE_REGISTRY_UPDATED, {"action": "create", "device_id": device_id2}
)
await hass.async_block_till_done()
assert len(single_event_data) == 1
assert len(multiple_event_data) == 2
unsub1()
unsub2()
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "create", "device_id": device_id}
)
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, {"action": "create", "device_id": device_id2}
)
await hass.async_block_till_done()
assert len(single_event_data) == 1
assert len(multiple_event_data) == 2
async def test_async_track_device_registry_updated_event_with_empty_list(
hass: HomeAssistant,
) -> None:
"""Test async_track_device_registry_updated_event passing an empty list of devices."""
unsub_single = async_track_device_registry_updated_event(
hass, [], ha.callback(lambda event: None)
)
unsub_single2 = async_track_device_registry_updated_event(
hass, [], ha.callback(lambda event: None)
)
unsub_single2()
unsub_single()
async def test_async_track_device_registry_updated_event_with_a_callback_that_throws(
hass: HomeAssistant,
) -> None:
"""Test tracking device registry updates for an device when one callback throws."""
device_id = "b92c0f06fbc911edacc9eea8ae14f866"
event_data = []
@ha.callback
def run_callback(event: ha.Event) -> None:
event_data.append(event.data)
@ha.callback
def failing_callback(event: ha.Event) -> None:
raise ValueError
unsub1 = async_track_device_registry_updated_event(
hass, device_id, failing_callback
)
unsub2 = async_track_device_registry_updated_event(hass, device_id, run_callback)
hass.bus.async_fire(
EVENT_DEVICE_REGISTRY_UPDATED, {"action": "create", "device_id": device_id}
)
await hass.async_block_till_done()
unsub1()
unsub2()
assert event_data[0] == {"action": "create", "device_id": device_id}