diff --git a/custom_components/shopping_list_manager/__init__.py b/custom_components/shopping_list_manager/__init__.py index c76937f..6d14e14 100644 --- a/custom_components/shopping_list_manager/__init__.py +++ b/custom_components/shopping_list_manager/__init__.py @@ -126,6 +126,11 @@ async def _async_register_websocket_handlers( hass, handlers.websocket_check_item, ) + websocket_api.async_register_command( + hass, + handlers.websocket_increment_item, + ) + websocket_api.async_register_command( hass, handlers.websocket_delete_item, diff --git a/custom_components/shopping_list_manager/websocket/handlers.py b/custom_components/shopping_list_manager/websocket/handlers.py index 02cc2c3..8f8484c 100644 --- a/custom_components/shopping_list_manager/websocket/handlers.py +++ b/custom_components/shopping_list_manager/websocket/handlers.py @@ -44,6 +44,43 @@ _LOGGER = logging.getLogger(__name__) # LIST HANDLERS # ============================================================================= +@websocket_api.websocket_command({ + vol.Required("type"): "shopping_list_manager/items/increment", + vol.Required("item_id"): str, + vol.Required("amount"): vol.Coerce(float), +}) +@websocket_api.async_response +async def websocket_increment_item( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: Dict[str, Any], +) -> None: + """Increment item quantity atomically.""" + + storage = get_storage(hass) + item_id = msg["item_id"] + amount = msg["amount"] + + # Get current item + item = storage.get_item(item_id) + + if not item: + connection.send_error(msg["id"], "not_found", "Item not found") + return + + # Atomic update + item.quantity += amount + + await storage.async_save() + + # Fire update event so all clients refresh + hass.bus.async_fire( + "shopping_list_manager_item_updated", + {"item_id": item.id} + ) + + connection.send_result(msg["id"], {"item": item.to_dict()}) + @websocket_api.websocket_command({ vol.Required("type"): "shopping_list_manager/products/get_by_ids", vol.Required("product_ids"): [str],