From 7533895a3d72dfddbe36838d7322b5d89246e9ff Mon Sep 17 00:00:00 2001 From: glanch <49610590+glanch@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:58:40 +0100 Subject: [PATCH] Add tag name to `tag_scanned` event data (#97553) * Add tag name to tag scanned event data * Make name in event data optional, add test cases for events * Simplify sanity None check of tag data Co-authored-by: Robert Resch * Apply suggestions from code review --------- Co-authored-by: Robert Resch Co-authored-by: Erik Montnemery --- homeassistant/components/tag/__init__.py | 15 +++- tests/components/tag/test_event.py | 106 +++++++++++++++++++++++ 2 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 tests/components/tag/test_event.py diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py index e82083f73ec..59b0fa995e4 100644 --- a/homeassistant/components/tag/__init__.py +++ b/homeassistant/components/tag/__init__.py @@ -118,10 +118,19 @@ async def async_scan_tag( if DOMAIN not in hass.config.components: raise HomeAssistantError("tag component has not been set up.") - hass.bus.async_fire( - EVENT_TAG_SCANNED, {TAG_ID: tag_id, DEVICE_ID: device_id}, context=context - ) helper = hass.data[DOMAIN][TAGS] + + # Get name from helper, default value None if not present in data + tag_name = None + if tag_data := helper.data.get(tag_id): + tag_name = tag_data.get(CONF_NAME) + + hass.bus.async_fire( + EVENT_TAG_SCANNED, + {TAG_ID: tag_id, CONF_NAME: tag_name, DEVICE_ID: device_id}, + context=context, + ) + if tag_id in helper.data: await helper.async_update_item(tag_id, {LAST_SCANNED: dt_util.utcnow()}) else: diff --git a/tests/components/tag/test_event.py b/tests/components/tag/test_event.py new file mode 100644 index 00000000000..7112a0cda4f --- /dev/null +++ b/tests/components/tag/test_event.py @@ -0,0 +1,106 @@ +"""Tests for the tag component.""" +from unittest.mock import patch + +import pytest + +from homeassistant.components.tag import DOMAIN, EVENT_TAG_SCANNED, async_scan_tag +from homeassistant.const import CONF_NAME +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component +from homeassistant.util import dt as dt_util + +from tests.common import async_capture_events +from tests.typing import WebSocketGenerator + +TEST_TAG_ID = "test tag id" +TEST_TAG_NAME = "test tag name" +TEST_DEVICE_ID = "device id" + + +@pytest.fixture +def storage_setup_named_tag( + hass, + hass_storage, +): + """Storage setup for test case of named tags.""" + + async def _storage(items=None): + if items is None: + hass_storage[DOMAIN] = { + "key": DOMAIN, + "version": 1, + "data": {"items": [{"id": TEST_TAG_ID, CONF_NAME: TEST_TAG_NAME}]}, + } + else: + hass_storage[DOMAIN] = items + config = {DOMAIN: {}} + return await async_setup_component(hass, DOMAIN, config) + + return _storage + + +async def test_named_tag_scanned_event( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup_named_tag +) -> None: + """Test scanning named tag triggering event.""" + assert await storage_setup_named_tag() + + await hass_ws_client(hass) + + events = async_capture_events(hass, EVENT_TAG_SCANNED) + + now = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow", return_value=now): + await async_scan_tag(hass, TEST_TAG_ID, TEST_DEVICE_ID) + + assert len(events) == 1 + + event = events[0] + event_data = event.data + + assert event_data["name"] == TEST_TAG_NAME + assert event_data["device_id"] == TEST_DEVICE_ID + assert event_data["tag_id"] == TEST_TAG_ID + + +@pytest.fixture +def storage_setup_unnamed_tag(hass, hass_storage): + """Storage setup for test case of unnamed tags.""" + + async def _storage(items=None): + if items is None: + hass_storage[DOMAIN] = { + "key": DOMAIN, + "version": 1, + "data": {"items": [{"id": TEST_TAG_ID}]}, + } + else: + hass_storage[DOMAIN] = items + config = {DOMAIN: {}} + return await async_setup_component(hass, DOMAIN, config) + + return _storage + + +async def test_unnamed_tag_scanned_event( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator, storage_setup_unnamed_tag +) -> None: + """Test scanning named tag triggering event.""" + assert await storage_setup_unnamed_tag() + + await hass_ws_client(hass) + + events = async_capture_events(hass, EVENT_TAG_SCANNED) + + now = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow", return_value=now): + await async_scan_tag(hass, TEST_TAG_ID, TEST_DEVICE_ID) + + assert len(events) == 1 + + event = events[0] + event_data = event.data + + assert event_data["name"] is None + assert event_data["device_id"] == TEST_DEVICE_ID + assert event_data["tag_id"] == TEST_TAG_ID