mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Cleanup tag integration (#118241)
* Cleanup tag integration * Fix review comments
This commit is contained in:
parent
c349797938
commit
6067ea2454
@ -14,8 +14,8 @@ from homeassistant.helpers import collection
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.loader import bind_hass
|
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
from .const import DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, TAG_ID
|
||||||
|
|
||||||
@ -24,7 +24,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
LAST_SCANNED = "last_scanned"
|
LAST_SCANNED = "last_scanned"
|
||||||
STORAGE_KEY = DOMAIN
|
STORAGE_KEY = DOMAIN
|
||||||
STORAGE_VERSION = 1
|
STORAGE_VERSION = 1
|
||||||
TAGS = "tags"
|
|
||||||
|
TAG_DATA: HassKey[TagStorageCollection] = HassKey(DOMAIN)
|
||||||
|
|
||||||
CREATE_FIELDS = {
|
CREATE_FIELDS = {
|
||||||
vol.Optional(TAG_ID): cv.string,
|
vol.Optional(TAG_ID): cv.string,
|
||||||
@ -94,9 +95,8 @@ class TagStorageCollection(collection.DictStorageCollection):
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the Tag component."""
|
"""Set up the Tag component."""
|
||||||
hass.data[DOMAIN] = {}
|
|
||||||
id_manager = TagIDManager()
|
id_manager = TagIDManager()
|
||||||
hass.data[DOMAIN][TAGS] = storage_collection = TagStorageCollection(
|
hass.data[TAG_DATA] = storage_collection = TagStorageCollection(
|
||||||
Store(hass, STORAGE_VERSION, STORAGE_KEY),
|
Store(hass, STORAGE_VERSION, STORAGE_KEY),
|
||||||
id_manager,
|
id_manager,
|
||||||
)
|
)
|
||||||
@ -108,7 +108,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
|
||||||
async def async_scan_tag(
|
async def async_scan_tag(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
tag_id: str,
|
tag_id: str,
|
||||||
@ -119,11 +118,11 @@ async def async_scan_tag(
|
|||||||
if DOMAIN not in hass.config.components:
|
if DOMAIN not in hass.config.components:
|
||||||
raise HomeAssistantError("tag component has not been set up.")
|
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
|
# Get name from helper, default value None if not present in data
|
||||||
tag_name = None
|
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)
|
tag_name = tag_data.get(CONF_NAME)
|
||||||
|
|
||||||
hass.bus.async_fire(
|
hass.bus.async_fire(
|
||||||
@ -132,8 +131,12 @@ async def async_scan_tag(
|
|||||||
context=context,
|
context=context,
|
||||||
)
|
)
|
||||||
|
|
||||||
if tag_id in helper.data:
|
if tag_id in storage_collection.data:
|
||||||
await helper.async_update_item(tag_id, {LAST_SCANNED: dt_util.utcnow()})
|
await storage_collection.async_update_item(
|
||||||
|
tag_id, {LAST_SCANNED: dt_util.utcnow()}
|
||||||
|
)
|
||||||
else:
|
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)
|
_LOGGER.debug("Tag: %s scanned by device: %s", tag_id, device_id)
|
||||||
|
@ -19,7 +19,7 @@ TEST_DEVICE_ID = "device id"
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def storage_setup_named_tag(
|
def storage_setup_named_tag(
|
||||||
hass,
|
hass: HomeAssistant,
|
||||||
hass_storage,
|
hass_storage,
|
||||||
):
|
):
|
||||||
"""Storage setup for test case of named tags."""
|
"""Storage setup for test case of named tags."""
|
||||||
@ -67,7 +67,7 @@ async def test_named_tag_scanned_event(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@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."""
|
"""Storage setup for test case of unnamed tags."""
|
||||||
|
|
||||||
async def _storage(items=None):
|
async def _storage(items=None):
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers import collection
|
from homeassistant.helpers import collection
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -13,7 +13,7 @@ from tests.typing import WebSocketGenerator
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def storage_setup(hass, hass_storage):
|
def storage_setup(hass: HomeAssistant, hass_storage):
|
||||||
"""Storage setup."""
|
"""Storage setup."""
|
||||||
|
|
||||||
async def _storage(items=None):
|
async def _storage(items=None):
|
||||||
@ -128,7 +128,7 @@ async def test_tag_id_exists(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test scanning tags."""
|
"""Test scanning tags."""
|
||||||
assert await storage_setup()
|
assert await storage_setup()
|
||||||
changes = track_changes(hass.data[DOMAIN][TAGS])
|
changes = track_changes(hass.data[DOMAIN])
|
||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
|
|
||||||
await client.send_json({"id": 2, "type": f"{DOMAIN}/create", "tag_id": "test tag"})
|
await client.send_json({"id": 2, "type": f"{DOMAIN}/create", "tag_id": "test tag"})
|
||||||
|
@ -18,7 +18,7 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def tag_setup(hass, hass_storage):
|
def tag_setup(hass: HomeAssistant, hass_storage):
|
||||||
"""Tag setup."""
|
"""Tag setup."""
|
||||||
|
|
||||||
async def _storage(items=None):
|
async def _storage(items=None):
|
||||||
@ -37,7 +37,7 @@ def tag_setup(hass, hass_storage):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def calls(hass):
|
def calls(hass: HomeAssistant):
|
||||||
"""Track calls to a mock service."""
|
"""Track calls to a mock service."""
|
||||||
return async_mock_service(hass, "test", "automation")
|
return async_mock_service(hass, "test", "automation")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user