mirror of
https://github.com/thekiwismarthome/shopping-list-manager.git
synced 2026-05-01 11:46:30 +00:00
Merge pull request #1 from thekiwismarthome/Phase-2.5-–-List-Ownership-&-Visibility-Architecture
Phase 2.5 – list ownership & visibility architecture
This commit is contained in:
@@ -6,6 +6,8 @@ import logging
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.components import websocket_api as ha_websocket
|
from homeassistant.components import websocket_api as ha_websocket
|
||||||
|
from .websocket_api import websocket_create_list
|
||||||
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .manager import ShoppingListManager
|
from .manager import ShoppingListManager
|
||||||
@@ -34,6 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN]["manager"] = manager
|
hass.data[DOMAIN]["manager"] = manager
|
||||||
|
|
||||||
# Register WebSocket commands using Home Assistant's websocket_api
|
# Register WebSocket commands using Home Assistant's websocket_api
|
||||||
|
ha_websocket.async_register_command(hass, websocket_create_list)
|
||||||
ha_websocket.async_register_command(hass, websocket_add_product)
|
ha_websocket.async_register_command(hass, websocket_add_product)
|
||||||
ha_websocket.async_register_command(hass, websocket_set_qty)
|
ha_websocket.async_register_command(hass, websocket_set_qty)
|
||||||
ha_websocket.async_register_command(hass, websocket_get_products)
|
ha_websocket.async_register_command(hass, websocket_get_products)
|
||||||
|
|||||||
@@ -207,6 +207,34 @@ class ShoppingListManager:
|
|||||||
# PUBLIC API - All operations enforce invariants
|
# PUBLIC API - All operations enforce invariants
|
||||||
# ========================================================================
|
# ========================================================================
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
async def async_create_list(
|
||||||
|
self,
|
||||||
|
list_id: str,
|
||||||
|
catalogue: str,
|
||||||
|
owner: str,
|
||||||
|
visibility: str = "shared",
|
||||||
|
):
|
||||||
|
await self._ensure_catalogues_loaded()
|
||||||
|
await self._ensure_lists_loaded()
|
||||||
|
|
||||||
|
if list_id in self._lists:
|
||||||
|
raise ValueError(f"List '{list_id}' already exists")
|
||||||
|
|
||||||
|
if catalogue not in self._catalogues:
|
||||||
|
raise ValueError(f"Catalogue '{catalogue}' does not exist")
|
||||||
|
|
||||||
|
self._lists[list_id] = {
|
||||||
|
"catalogue": catalogue,
|
||||||
|
"owner": owner,
|
||||||
|
"visibility": visibility,
|
||||||
|
"created_at": time.time(),
|
||||||
|
"updated_at": time.time(),
|
||||||
|
}
|
||||||
|
|
||||||
|
await self._store_lists.async_save(self._lists)
|
||||||
|
|
||||||
async def async_add_product(
|
async def async_add_product(
|
||||||
self,
|
self,
|
||||||
list_id: str,
|
list_id: str,
|
||||||
|
|||||||
@@ -10,6 +10,33 @@ from .models import InvariantError
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@websocket_api.websocket_command({
|
||||||
|
vol.Required("type"): "shopping_list_manager/create_list",
|
||||||
|
vol.Required("list_id"): str,
|
||||||
|
vol.Required("catalogue"): str,
|
||||||
|
vol.Optional("visibility", default="shared"): vol.In(["shared", "private"]),
|
||||||
|
})
|
||||||
|
@websocket_api.async_response
|
||||||
|
async def websocket_create_list(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
connection: websocket_api.ActiveConnection,
|
||||||
|
msg: dict,
|
||||||
|
) -> None:
|
||||||
|
manager = hass.data[DOMAIN]["manager"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
await manager.async_create_list(
|
||||||
|
list_id=msg["list_id"],
|
||||||
|
catalogue=msg["catalogue"],
|
||||||
|
owner=connection.user.id,
|
||||||
|
visibility=msg.get("visibility", "shared"),
|
||||||
|
)
|
||||||
|
|
||||||
|
connection.send_result(msg["id"], {"success": True})
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
connection.send_error(msg["id"], "create_list_failed", str(err))
|
||||||
|
|
||||||
|
|
||||||
@websocket_api.websocket_command({
|
@websocket_api.websocket_command({
|
||||||
vol.Required("type"): "shopping_list_manager/add_product",
|
vol.Required("type"): "shopping_list_manager/add_product",
|
||||||
|
|||||||
Reference in New Issue
Block a user