Cleanup tag integration (#118241)

* Cleanup tag integration

* Fix review comments
This commit is contained in:
G Johansson 2024-05-27 21:53:06 +02:00 committed by GitHub
parent c349797938
commit 6067ea2454
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 17 deletions

View file

@ -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)

View file

@ -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):

View file

@ -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"})

View file

@ -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")