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.core import HomeAssistant
|
||||
from homeassistant.components import websocket_api as ha_websocket
|
||||
from .websocket_api import websocket_create_list
|
||||
|
||||
|
||||
from .const import DOMAIN
|
||||
from .manager import ShoppingListManager
|
||||
@@ -34,6 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.data[DOMAIN]["manager"] = manager
|
||||
|
||||
# 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_set_qty)
|
||||
ha_websocket.async_register_command(hass, websocket_get_products)
|
||||
@@ -50,4 +53,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload Shopping List Manager."""
|
||||
hass.data[DOMAIN].pop("manager", None)
|
||||
return True
|
||||
return True
|
||||
|
||||
@@ -207,6 +207,34 @@ class ShoppingListManager:
|
||||
# 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(
|
||||
self,
|
||||
list_id: str,
|
||||
@@ -375,4 +403,4 @@ class ShoppingListManager:
|
||||
# and would need updating to support per-list structure:
|
||||
# - async_get_full_state()
|
||||
# - get_product()
|
||||
# - get_active_qty()
|
||||
# - get_active_qty()
|
||||
|
||||
@@ -10,6 +10,33 @@ from .models import InvariantError
|
||||
|
||||
_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({
|
||||
vol.Required("type"): "shopping_list_manager/add_product",
|
||||
@@ -295,4 +322,4 @@ async def websocket_delete_product(
|
||||
|
||||
except Exception as err:
|
||||
_LOGGER.error("Error deleting product from list '%s': %s", list_id, err)
|
||||
connection.send_error(msg["id"], "delete_product_failed", str(err))
|
||||
connection.send_error(msg["id"], "delete_product_failed", str(err))
|
||||
|
||||
Reference in New Issue
Block a user