From dd1d21c77a915e288b3e06f8182b3557acb59590 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 3 Jun 2024 02:41:16 +0200 Subject: [PATCH] Fix entity state dispatching for Tag entities (#118662) --- homeassistant/components/tag/__init__.py | 4 ++-- tests/components/tag/__init__.py | 2 ++ tests/components/tag/test_init.py | 22 +++++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py index b7c9660ed93..afea86baa93 100644 --- a/homeassistant/components/tag/__init__.py +++ b/homeassistant/components/tag/__init__.py @@ -267,7 +267,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # When tags are changed or updated in storage async_dispatcher_send( hass, - SIGNAL_TAG_CHANGED, + f"{SIGNAL_TAG_CHANGED}-{updated_config[TAG_ID]}", updated_config.get(DEVICE_ID), updated_config.get(LAST_SCANNED), ) @@ -414,7 +414,7 @@ class TagEntity(Entity): self.async_on_remove( async_dispatcher_connect( self.hass, - SIGNAL_TAG_CHANGED, + f"{SIGNAL_TAG_CHANGED}-{self._tag_id}", self.async_handle_event, ) ) diff --git a/tests/components/tag/__init__.py b/tests/components/tag/__init__.py index 66b23073d3e..5c701af5d0a 100644 --- a/tests/components/tag/__init__.py +++ b/tests/components/tag/__init__.py @@ -1,5 +1,7 @@ """Tests for the Tag integration.""" TEST_TAG_ID = "test tag id" +TEST_TAG_ID_2 = "test tag id 2" TEST_TAG_NAME = "test tag name" +TEST_TAG_NAME_2 = "test tag name 2" TEST_DEVICE_ID = "device id" diff --git a/tests/components/tag/test_init.py b/tests/components/tag/test_init.py index 4767cc40fdf..db7e9d5dbc7 100644 --- a/tests/components/tag/test_init.py +++ b/tests/components/tag/test_init.py @@ -14,7 +14,7 @@ from homeassistant.helpers import collection, entity_registry as er from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util -from . import TEST_DEVICE_ID, TEST_TAG_ID, TEST_TAG_NAME +from . import TEST_DEVICE_ID, TEST_TAG_ID, TEST_TAG_ID_2, TEST_TAG_NAME, TEST_TAG_NAME_2 from tests.common import async_fire_time_changed from tests.typing import WebSocketGenerator @@ -35,7 +35,11 @@ def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): { "id": TEST_TAG_ID, "tag_id": TEST_TAG_ID, - } + }, + { + "id": TEST_TAG_ID_2, + "tag_id": TEST_TAG_ID_2, + }, ] }, } @@ -43,6 +47,7 @@ def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): hass_storage[DOMAIN] = items entity_registry = er.async_get(hass) _create_entry(entity_registry, TEST_TAG_ID, TEST_TAG_NAME) + _create_entry(entity_registry, TEST_TAG_ID_2, TEST_TAG_NAME_2) config = {DOMAIN: {}} return await async_setup_component(hass, DOMAIN, config) @@ -132,7 +137,8 @@ async def test_ws_list( resp = await client.receive_json() assert resp["success"] assert resp["result"] == [ - {"id": TEST_TAG_ID, "name": "test tag name", "tag_id": TEST_TAG_ID} + {"id": TEST_TAG_ID, "name": "test tag name", "tag_id": TEST_TAG_ID}, + {"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2}, ] @@ -176,7 +182,8 @@ async def test_tag_scanned( result = {item["id"]: item for item in resp["result"]} assert resp["result"] == [ - {"id": TEST_TAG_ID, "name": "test tag name", "tag_id": TEST_TAG_ID} + {"id": TEST_TAG_ID, "name": "test tag name", "tag_id": TEST_TAG_ID}, + {"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2}, ] now = dt_util.utcnow() @@ -189,9 +196,10 @@ async def test_tag_scanned( result = {item["id"]: item for item in resp["result"]} - assert len(result) == 2 + assert len(result) == 3 assert resp["result"] == [ {"id": TEST_TAG_ID, "name": "test tag name", "tag_id": TEST_TAG_ID}, + {"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2}, { "device_id": "some_scanner", "id": "new tag", @@ -257,6 +265,10 @@ async def test_entity( "friendly_name": "test tag name", } + entity = hass.states.get("tag.test_tag_name_2") + assert entity + assert entity.state == STATE_UNKNOWN + async def test_entity_created_and_removed( caplog: pytest.LogCaptureFixture,