From 6067ea2454d6618cd64aa15950ec0b9e44d207db Mon Sep 17 00:00:00 2001 From: G Johansson Date: Mon, 27 May 2024 21:53:06 +0200 Subject: [PATCH] Cleanup tag integration (#118241) * Cleanup tag integration * Fix review comments --- homeassistant/components/tag/__init__.py | 23 +++++++++++++---------- tests/components/tag/test_event.py | 4 ++-- tests/components/tag/test_init.py | 6 +++--- tests/components/tag/test_trigger.py | 4 ++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py index 4fd20fff24b..d91cf080c2a 100644 --- a/homeassistant/components/tag/__init__.py +++ b/homeassistant/components/tag/__init__.py @@ -14,8 +14,8 @@ from homeassistant.helpers import collection import homeassistant.helpers.config_validation as cv from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType -from homeassistant.loader import bind_hass import homeassistant.util.dt as dt_util +from homeassistant.util.hass_dict import HassKey from .const import DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, TAG_ID @@ -24,7 +24,8 @@ _LOGGER = logging.getLogger(__name__) LAST_SCANNED = "last_scanned" STORAGE_KEY = DOMAIN STORAGE_VERSION = 1 -TAGS = "tags" + +TAG_DATA: HassKey[TagStorageCollection] = HassKey(DOMAIN) CREATE_FIELDS = { vol.Optional(TAG_ID): cv.string, @@ -94,9 +95,8 @@ class TagStorageCollection(collection.DictStorageCollection): async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Tag component.""" - hass.data[DOMAIN] = {} id_manager = TagIDManager() - hass.data[DOMAIN][TAGS] = storage_collection = TagStorageCollection( + hass.data[TAG_DATA] = storage_collection = TagStorageCollection( Store(hass, STORAGE_VERSION, STORAGE_KEY), id_manager, ) @@ -108,7 +108,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -@bind_hass async def async_scan_tag( hass: HomeAssistant, tag_id: str, @@ -119,11 +118,11 @@ async def async_scan_tag( if DOMAIN not in hass.config.components: raise HomeAssistantError("tag component has not been set up.") - helper = hass.data[DOMAIN][TAGS] + storage_collection = hass.data[TAG_DATA] # Get name from helper, default value None if not present in data tag_name = None - if tag_data := helper.data.get(tag_id): + if tag_data := storage_collection.data.get(tag_id): tag_name = tag_data.get(CONF_NAME) hass.bus.async_fire( @@ -132,8 +131,12 @@ async def async_scan_tag( context=context, ) - if tag_id in helper.data: - await helper.async_update_item(tag_id, {LAST_SCANNED: dt_util.utcnow()}) + if tag_id in storage_collection.data: + await storage_collection.async_update_item( + tag_id, {LAST_SCANNED: dt_util.utcnow()} + ) else: - await helper.async_create_item({TAG_ID: tag_id, LAST_SCANNED: dt_util.utcnow()}) + await storage_collection.async_create_item( + {TAG_ID: tag_id, LAST_SCANNED: dt_util.utcnow()} + ) _LOGGER.debug("Tag: %s scanned by device: %s", tag_id, device_id) diff --git a/tests/components/tag/test_event.py b/tests/components/tag/test_event.py index 0338ed504d7..ac24e837428 100644 --- a/tests/components/tag/test_event.py +++ b/tests/components/tag/test_event.py @@ -19,7 +19,7 @@ TEST_DEVICE_ID = "device id" @pytest.fixture def storage_setup_named_tag( - hass, + hass: HomeAssistant, hass_storage, ): """Storage setup for test case of named tags.""" @@ -67,7 +67,7 @@ async def test_named_tag_scanned_event( @pytest.fixture -def storage_setup_unnamed_tag(hass, hass_storage): +def storage_setup_unnamed_tag(hass: HomeAssistant, hass_storage): """Storage setup for test case of unnamed tags.""" async def _storage(items=None): diff --git a/tests/components/tag/test_init.py b/tests/components/tag/test_init.py index d7f77c0d2e2..6d300b8ea6e 100644 --- a/tests/components/tag/test_init.py +++ b/tests/components/tag/test_init.py @@ -3,7 +3,7 @@ from freezegun.api import FrozenDateTimeFactory import pytest -from homeassistant.components.tag import DOMAIN, TAGS, async_scan_tag +from homeassistant.components.tag import DOMAIN, async_scan_tag from homeassistant.core import HomeAssistant from homeassistant.helpers import collection from homeassistant.setup import async_setup_component @@ -13,7 +13,7 @@ from tests.typing import WebSocketGenerator @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage): """Storage setup.""" async def _storage(items=None): @@ -128,7 +128,7 @@ async def test_tag_id_exists( ) -> None: """Test scanning tags.""" assert await storage_setup() - changes = track_changes(hass.data[DOMAIN][TAGS]) + changes = track_changes(hass.data[DOMAIN]) client = await hass_ws_client(hass) await client.send_json({"id": 2, "type": f"{DOMAIN}/create", "tag_id": "test tag"}) diff --git a/tests/components/tag/test_trigger.py b/tests/components/tag/test_trigger.py index a034334508f..7af1f364231 100644 --- a/tests/components/tag/test_trigger.py +++ b/tests/components/tag/test_trigger.py @@ -18,7 +18,7 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None: @pytest.fixture -def tag_setup(hass, hass_storage): +def tag_setup(hass: HomeAssistant, hass_storage): """Tag setup.""" async def _storage(items=None): @@ -37,7 +37,7 @@ def tag_setup(hass, hass_storage): @pytest.fixture -def calls(hass): +def calls(hass: HomeAssistant): """Track calls to a mock service.""" return async_mock_service(hass, "test", "automation")