mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00
Don't store tag_id in tag storage (#118707)
This commit is contained in:
parent
87a1b8e83c
commit
765114bead
@ -9,7 +9,7 @@ import uuid
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_ID, CONF_NAME
|
||||||
from homeassistant.core import Context, HomeAssistant, callback
|
from homeassistant.core import Context, HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import collection, entity_registry as er
|
from homeassistant.helpers import collection, entity_registry as er
|
||||||
@ -107,7 +107,7 @@ class TagStore(Store[collection.SerializedStorageCollection]):
|
|||||||
# Version 1.2 moves name to entity registry
|
# Version 1.2 moves name to entity registry
|
||||||
for tag in data["items"]:
|
for tag in data["items"]:
|
||||||
# Copy name in tag store to the entity registry
|
# Copy name in tag store to the entity registry
|
||||||
_create_entry(entity_registry, tag[TAG_ID], tag.get(CONF_NAME))
|
_create_entry(entity_registry, tag[CONF_ID], tag.get(CONF_NAME))
|
||||||
tag["migrated"] = True
|
tag["migrated"] = True
|
||||||
|
|
||||||
if old_major_version > 1:
|
if old_major_version > 1:
|
||||||
@ -136,24 +136,26 @@ class TagStorageCollection(collection.DictStorageCollection):
|
|||||||
data = self.CREATE_SCHEMA(data)
|
data = self.CREATE_SCHEMA(data)
|
||||||
if not data[TAG_ID]:
|
if not data[TAG_ID]:
|
||||||
data[TAG_ID] = str(uuid.uuid4())
|
data[TAG_ID] = str(uuid.uuid4())
|
||||||
|
# Move tag id to id
|
||||||
|
data[CONF_ID] = data.pop(TAG_ID)
|
||||||
# make last_scanned JSON serializeable
|
# make last_scanned JSON serializeable
|
||||||
if LAST_SCANNED in data:
|
if LAST_SCANNED in data:
|
||||||
data[LAST_SCANNED] = data[LAST_SCANNED].isoformat()
|
data[LAST_SCANNED] = data[LAST_SCANNED].isoformat()
|
||||||
|
|
||||||
# Create entity in entity_registry when creating the tag
|
# Create entity in entity_registry when creating the tag
|
||||||
# This is done early to store name only once in entity registry
|
# This is done early to store name only once in entity registry
|
||||||
_create_entry(self.entity_registry, data[TAG_ID], data.get(CONF_NAME))
|
_create_entry(self.entity_registry, data[CONF_ID], data.get(CONF_NAME))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _get_suggested_id(self, info: dict[str, str]) -> str:
|
def _get_suggested_id(self, info: dict[str, str]) -> str:
|
||||||
"""Suggest an ID based on the config."""
|
"""Suggest an ID based on the config."""
|
||||||
return info[TAG_ID]
|
return info[CONF_ID]
|
||||||
|
|
||||||
async def _update_data(self, item: dict, update_data: dict) -> dict:
|
async def _update_data(self, item: dict, update_data: dict) -> dict:
|
||||||
"""Return a new updated data object."""
|
"""Return a new updated data object."""
|
||||||
data = {**item, **self.UPDATE_SCHEMA(update_data)}
|
data = {**item, **self.UPDATE_SCHEMA(update_data)}
|
||||||
tag_id = data[TAG_ID]
|
tag_id = item[CONF_ID]
|
||||||
# make last_scanned JSON serializeable
|
# make last_scanned JSON serializeable
|
||||||
if LAST_SCANNED in update_data:
|
if LAST_SCANNED in update_data:
|
||||||
data[LAST_SCANNED] = data[LAST_SCANNED].isoformat()
|
data[LAST_SCANNED] = data[LAST_SCANNED].isoformat()
|
||||||
@ -211,7 +213,7 @@ class TagDictStorageCollectionWebsocket(
|
|||||||
item = {k: v for k, v in item.items() if k != "migrated"}
|
item = {k: v for k, v in item.items() if k != "migrated"}
|
||||||
if (
|
if (
|
||||||
entity_id := self.entity_registry.async_get_entity_id(
|
entity_id := self.entity_registry.async_get_entity_id(
|
||||||
DOMAIN, DOMAIN, item[TAG_ID]
|
DOMAIN, DOMAIN, item[CONF_ID]
|
||||||
)
|
)
|
||||||
) and (entity := self.entity_registry.async_get(entity_id)):
|
) and (entity := self.entity_registry.async_get(entity_id)):
|
||||||
item[CONF_NAME] = entity.name or entity.original_name
|
item[CONF_NAME] = entity.name or entity.original_name
|
||||||
@ -249,14 +251,14 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
)
|
)
|
||||||
if change_type == collection.CHANGE_ADDED:
|
if change_type == collection.CHANGE_ADDED:
|
||||||
# When tags are added to storage
|
# When tags are added to storage
|
||||||
entity = _create_entry(entity_registry, updated_config[TAG_ID], None)
|
entity = _create_entry(entity_registry, updated_config[CONF_ID], None)
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
assert entity.original_name
|
assert entity.original_name
|
||||||
await component.async_add_entities(
|
await component.async_add_entities(
|
||||||
[
|
[
|
||||||
TagEntity(
|
TagEntity(
|
||||||
entity.name or entity.original_name,
|
entity.name or entity.original_name,
|
||||||
updated_config[TAG_ID],
|
updated_config[CONF_ID],
|
||||||
updated_config.get(LAST_SCANNED),
|
updated_config.get(LAST_SCANNED),
|
||||||
updated_config.get(DEVICE_ID),
|
updated_config.get(DEVICE_ID),
|
||||||
)
|
)
|
||||||
@ -267,7 +269,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
# When tags are changed or updated in storage
|
# When tags are changed or updated in storage
|
||||||
async_dispatcher_send(
|
async_dispatcher_send(
|
||||||
hass,
|
hass,
|
||||||
f"{SIGNAL_TAG_CHANGED}-{updated_config[TAG_ID]}",
|
f"{SIGNAL_TAG_CHANGED}-{updated_config[CONF_ID]}",
|
||||||
updated_config.get(DEVICE_ID),
|
updated_config.get(DEVICE_ID),
|
||||||
updated_config.get(LAST_SCANNED),
|
updated_config.get(LAST_SCANNED),
|
||||||
)
|
)
|
||||||
@ -276,7 +278,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
elif change_type == collection.CHANGE_REMOVED:
|
elif change_type == collection.CHANGE_REMOVED:
|
||||||
# When tags are removed from storage
|
# When tags are removed from storage
|
||||||
entity_id = entity_registry.async_get_entity_id(
|
entity_id = entity_registry.async_get_entity_id(
|
||||||
DOMAIN, DOMAIN, updated_config[TAG_ID]
|
DOMAIN, DOMAIN, updated_config[CONF_ID]
|
||||||
)
|
)
|
||||||
if entity_id:
|
if entity_id:
|
||||||
entity_registry.async_remove(entity_id)
|
entity_registry.async_remove(entity_id)
|
||||||
@ -287,13 +289,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
for tag in storage_collection.async_items():
|
for tag in storage_collection.async_items():
|
||||||
if _LOGGER.isEnabledFor(logging.DEBUG):
|
if _LOGGER.isEnabledFor(logging.DEBUG):
|
||||||
_LOGGER.debug("Adding tag: %s", tag)
|
_LOGGER.debug("Adding tag: %s", tag)
|
||||||
entity_id = entity_registry.async_get_entity_id(DOMAIN, DOMAIN, tag[TAG_ID])
|
entity_id = entity_registry.async_get_entity_id(DOMAIN, DOMAIN, tag[CONF_ID])
|
||||||
if entity_id := entity_registry.async_get_entity_id(
|
if entity_id := entity_registry.async_get_entity_id(
|
||||||
DOMAIN, DOMAIN, tag[TAG_ID]
|
DOMAIN, DOMAIN, tag[CONF_ID]
|
||||||
):
|
):
|
||||||
entity = entity_registry.async_get(entity_id)
|
entity = entity_registry.async_get(entity_id)
|
||||||
else:
|
else:
|
||||||
entity = _create_entry(entity_registry, tag[TAG_ID], None)
|
entity = _create_entry(entity_registry, tag[CONF_ID], None)
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
assert entity
|
assert entity
|
||||||
assert entity.original_name
|
assert entity.original_name
|
||||||
@ -301,7 +303,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
entities.append(
|
entities.append(
|
||||||
TagEntity(
|
TagEntity(
|
||||||
name,
|
name,
|
||||||
tag[TAG_ID],
|
tag[CONF_ID],
|
||||||
tag.get(LAST_SCANNED),
|
tag.get(LAST_SCANNED),
|
||||||
tag.get(DEVICE_ID),
|
tag.get(DEVICE_ID),
|
||||||
)
|
)
|
||||||
|
@ -13,11 +13,9 @@
|
|||||||
'device_id': 'some_scanner',
|
'device_id': 'some_scanner',
|
||||||
'id': 'new tag',
|
'id': 'new tag',
|
||||||
'last_scanned': '2024-02-29T13:00:00+00:00',
|
'last_scanned': '2024-02-29T13:00:00+00:00',
|
||||||
'tag_id': 'new tag',
|
|
||||||
}),
|
}),
|
||||||
dict({
|
dict({
|
||||||
'id': '1234567890',
|
'id': '1234567890',
|
||||||
'tag_id': '1234567890',
|
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
}),
|
}),
|
||||||
|
@ -34,11 +34,9 @@ def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]):
|
|||||||
"items": [
|
"items": [
|
||||||
{
|
{
|
||||||
"id": TEST_TAG_ID,
|
"id": TEST_TAG_ID,
|
||||||
"tag_id": TEST_TAG_ID,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": TEST_TAG_ID_2,
|
"id": TEST_TAG_ID_2,
|
||||||
"tag_id": TEST_TAG_ID_2,
|
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -117,6 +115,7 @@ async def test_migration(
|
|||||||
)
|
)
|
||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
|
assert resp["result"] == {"id": "1234567890", "name": "Kitchen tag"}
|
||||||
|
|
||||||
# Trigger store
|
# Trigger store
|
||||||
freezer.tick(11)
|
freezer.tick(11)
|
||||||
@ -137,8 +136,8 @@ async def test_ws_list(
|
|||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
assert 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"},
|
||||||
{"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2},
|
{"id": TEST_TAG_ID_2, "name": "test tag name 2"},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -161,7 +160,7 @@ async def test_ws_update(
|
|||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
item = resp["result"]
|
item = resp["result"]
|
||||||
assert item == {"id": TEST_TAG_ID, "name": "New name", "tag_id": TEST_TAG_ID}
|
assert item == {"id": TEST_TAG_ID, "name": "New name"}
|
||||||
|
|
||||||
|
|
||||||
async def test_tag_scanned(
|
async def test_tag_scanned(
|
||||||
@ -182,8 +181,8 @@ async def test_tag_scanned(
|
|||||||
result = {item["id"]: item for item in resp["result"]}
|
result = {item["id"]: item for item in resp["result"]}
|
||||||
|
|
||||||
assert 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"},
|
||||||
{"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2},
|
{"id": TEST_TAG_ID_2, "name": "test tag name 2"},
|
||||||
]
|
]
|
||||||
|
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
@ -198,14 +197,13 @@ async def test_tag_scanned(
|
|||||||
|
|
||||||
assert len(result) == 3
|
assert len(result) == 3
|
||||||
assert 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"},
|
||||||
{"id": TEST_TAG_ID_2, "name": "test tag name 2", "tag_id": TEST_TAG_ID_2},
|
{"id": TEST_TAG_ID_2, "name": "test tag name 2"},
|
||||||
{
|
{
|
||||||
"device_id": "some_scanner",
|
"device_id": "some_scanner",
|
||||||
"id": "new tag",
|
"id": "new tag",
|
||||||
"last_scanned": now.isoformat(),
|
"last_scanned": now.isoformat(),
|
||||||
"name": "Tag new tag",
|
"name": "Tag new tag",
|
||||||
"tag_id": "new tag",
|
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user