core/homeassistant/components/frontend/storage.py

97 lines
2.7 KiB
Python
Raw Normal View History

"""API for persistent storage for the frontend."""
2021-06-24 14:01:28 +00:00
from __future__ import annotations
from collections.abc import Callable
from functools import wraps
from typing import Any
import voluptuous as vol
from homeassistant.components import websocket_api
2021-06-24 14:01:28 +00:00
from homeassistant.components.websocket_api.connection import ActiveConnection
from homeassistant.core import HomeAssistant
from homeassistant.helpers.storage import Store
2019-07-31 19:25:30 +00:00
DATA_STORAGE = "frontend_storage"
STORAGE_VERSION_USER_DATA = 1
2021-06-24 14:01:28 +00:00
async def async_setup_frontend_storage(hass: HomeAssistant) -> None:
"""Set up frontend storage."""
hass.data[DATA_STORAGE] = ({}, {})
2019-07-31 19:25:30 +00:00
hass.components.websocket_api.async_register_command(websocket_set_user_data)
hass.components.websocket_api.async_register_command(websocket_get_user_data)
2021-06-24 14:01:28 +00:00
def with_store(orig_func: Callable) -> Callable:
"""Decorate function to provide data."""
2019-07-31 19:25:30 +00:00
@wraps(orig_func)
2021-06-24 14:01:28 +00:00
async def with_store_func(
hass: HomeAssistant, connection: ActiveConnection, msg: dict
) -> None:
"""Provide user specific data and store to function."""
stores, data = hass.data[DATA_STORAGE]
user_id = connection.user.id
store = stores.get(user_id)
if store is None:
store = stores[user_id] = hass.helpers.storage.Store(
STORAGE_VERSION_USER_DATA, f"frontend.user_data_{connection.user.id}"
)
if user_id not in data:
data[user_id] = await store.async_load() or {}
2019-07-31 19:25:30 +00:00
await orig_func(hass, connection, msg, store, data[user_id])
return with_store_func
2019-07-31 19:25:30 +00:00
@websocket_api.websocket_command(
{
vol.Required("type"): "frontend/set_user_data",
vol.Required("key"): str,
vol.Required("value"): vol.Any(bool, str, int, float, dict, list, None),
}
)
@websocket_api.async_response
@with_store
2021-06-24 14:01:28 +00:00
async def websocket_set_user_data(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
store: Store,
data: dict[str, Any],
) -> None:
"""Handle set global data command.
Async friendly.
"""
2019-07-31 19:25:30 +00:00
data[msg["key"]] = msg["value"]
await store.async_save(data)
2019-07-31 19:25:30 +00:00
connection.send_message(websocket_api.result_message(msg["id"]))
2019-07-31 19:25:30 +00:00
@websocket_api.websocket_command(
{vol.Required("type"): "frontend/get_user_data", vol.Optional("key"): str}
)
@websocket_api.async_response
@with_store
2021-06-24 14:01:28 +00:00
async def websocket_get_user_data(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
store: Store,
data: dict[str, Any],
) -> None:
"""Handle get global data command.
Async friendly.
"""
2019-07-31 19:25:30 +00:00
connection.send_message(
websocket_api.result_message(
msg["id"], {"value": data.get(msg["key"]) if "key" in msg else data}
)
)