mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Cache area registry JSON serialize (#117847)
We already cache the entity and device registry, but since I never used area until recently I did not have enough to notice that they were not cached
This commit is contained in:
parent
905692901c
commit
266ce9e268
@ -8,7 +8,7 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.components import websocket_api
|
from homeassistant.components import websocket_api
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.area_registry import AreaEntry, async_get
|
from homeassistant.helpers.area_registry import async_get
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -32,7 +32,7 @@ def websocket_list_areas(
|
|||||||
registry = async_get(hass)
|
registry = async_get(hass)
|
||||||
connection.send_result(
|
connection.send_result(
|
||||||
msg["id"],
|
msg["id"],
|
||||||
[_entry_dict(entry) for entry in registry.async_list_areas()],
|
[entry.json_fragment for entry in registry.async_list_areas()],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ def websocket_create_area(
|
|||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
connection.send_error(msg["id"], "invalid_info", str(err))
|
connection.send_error(msg["id"], "invalid_info", str(err))
|
||||||
else:
|
else:
|
||||||
connection.send_result(msg["id"], _entry_dict(entry))
|
connection.send_result(msg["id"], entry.json_fragment)
|
||||||
|
|
||||||
|
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
@ -140,18 +140,4 @@ def websocket_update_area(
|
|||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
connection.send_error(msg["id"], "invalid_info", str(err))
|
connection.send_error(msg["id"], "invalid_info", str(err))
|
||||||
else:
|
else:
|
||||||
connection.send_result(msg["id"], _entry_dict(entry))
|
connection.send_result(msg["id"], entry.json_fragment)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def _entry_dict(entry: AreaEntry) -> dict[str, Any]:
|
|
||||||
"""Convert entry to API format."""
|
|
||||||
return {
|
|
||||||
"aliases": list(entry.aliases),
|
|
||||||
"area_id": entry.id,
|
|
||||||
"floor_id": entry.floor_id,
|
|
||||||
"icon": entry.icon,
|
|
||||||
"labels": list(entry.labels),
|
|
||||||
"name": entry.name,
|
|
||||||
"picture": entry.picture,
|
|
||||||
}
|
|
||||||
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
from functools import cached_property
|
||||||
from typing import Any, Literal, TypedDict
|
from typing import Any, Literal, TypedDict
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@ -12,6 +13,7 @@ from homeassistant.util.event_type import EventType
|
|||||||
from homeassistant.util.hass_dict import HassKey
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
from . import device_registry as dr, entity_registry as er
|
from . import device_registry as dr, entity_registry as er
|
||||||
|
from .json import json_bytes, json_fragment
|
||||||
from .normalized_name_base_registry import (
|
from .normalized_name_base_registry import (
|
||||||
NormalizedNameBaseRegistryEntry,
|
NormalizedNameBaseRegistryEntry,
|
||||||
NormalizedNameBaseRegistryItems,
|
NormalizedNameBaseRegistryItems,
|
||||||
@ -56,7 +58,7 @@ class EventAreaRegistryUpdatedData(TypedDict):
|
|||||||
area_id: str
|
area_id: str
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)
|
@dataclasses.dataclass(frozen=True, kw_only=True)
|
||||||
class AreaEntry(NormalizedNameBaseRegistryEntry):
|
class AreaEntry(NormalizedNameBaseRegistryEntry):
|
||||||
"""Area Registry Entry."""
|
"""Area Registry Entry."""
|
||||||
|
|
||||||
@ -67,6 +69,23 @@ class AreaEntry(NormalizedNameBaseRegistryEntry):
|
|||||||
labels: set[str] = dataclasses.field(default_factory=set)
|
labels: set[str] = dataclasses.field(default_factory=set)
|
||||||
picture: str | None
|
picture: str | None
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def json_fragment(self) -> json_fragment:
|
||||||
|
"""Return a JSON representation of this AreaEntry."""
|
||||||
|
return json_fragment(
|
||||||
|
json_bytes(
|
||||||
|
{
|
||||||
|
"aliases": list(self.aliases),
|
||||||
|
"area_id": self.id,
|
||||||
|
"floor_id": self.floor_id,
|
||||||
|
"icon": self.icon,
|
||||||
|
"labels": list(self.labels),
|
||||||
|
"name": self.name,
|
||||||
|
"picture": self.picture,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AreaRegistryStore(Store[AreasRegistryStoreData]):
|
class AreaRegistryStore(Store[AreasRegistryStoreData]):
|
||||||
"""Store area registry data."""
|
"""Store area registry data."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user