diff --git a/custom_components/shopping_list_manager/__init__.py b/custom_components/shopping_list_manager/__init__.py index 6aa18cd..b8ff12b 100644 --- a/custom_components/shopping_list_manager/__init__.py +++ b/custom_components/shopping_list_manager/__init__.py @@ -200,6 +200,10 @@ async def _async_register_websocket_handlers( hass, handlers.websocket_update_product, ) + websocket_api.async_register_command( + hass, + handlers.websocket_delete_product, + ) websocket_api.async_register_command( hass, handlers.websocket_get_product_substitutes, diff --git a/custom_components/shopping_list_manager/storage.py b/custom_components/shopping_list_manager/storage.py index b2a054b..4b3833a 100644 --- a/custom_components/shopping_list_manager/storage.py +++ b/custom_components/shopping_list_manager/storage.py @@ -618,6 +618,18 @@ class ShoppingListStorage: _LOGGER.info("Reloaded catalog for %s: %d products imported", country_code, count) return count + async def delete_product(self, product_id: str) -> bool: + """Delete a product from the catalog.""" + if product_id not in self._products: + return False + del self._products[product_id] + await self._save_products() + # Rebuild search engine so the product is no longer searchable + products_dict = {pid: p.to_dict() for pid, p in self._products.items()} + self._search_engine = ProductSearch(products_dict) + _LOGGER.debug("Deleted product: %s", product_id) + return True + async def update_product(self, product_id: str, **kwargs) -> Optional[Product]: """Update a product.""" if product_id not in self._products: diff --git a/custom_components/shopping_list_manager/websocket/handlers.py b/custom_components/shopping_list_manager/websocket/handlers.py index 7ca7f5c..bf9cfe4 100644 --- a/custom_components/shopping_list_manager/websocket/handlers.py +++ b/custom_components/shopping_list_manager/websocket/handlers.py @@ -39,6 +39,7 @@ from ..const import ( WS_TYPE_PRODUCTS_SUGGESTIONS, WS_TYPE_PRODUCTS_ADD, WS_TYPE_PRODUCTS_UPDATE, + WS_TYPE_PRODUCTS_DELETE, WS_TYPE_CATEGORIES_GET_ALL, WS_TYPE_LOYALTY_GET_ALL, WS_TYPE_LOYALTY_ADD, @@ -1057,6 +1058,27 @@ async def websocket_update_product( ) +@websocket_api.websocket_command( + { + vol.Required("type"): WS_TYPE_PRODUCTS_DELETE, + vol.Required("product_id"): str, + } +) +@websocket_api.async_response +async def websocket_delete_product( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: Dict[str, Any], +) -> None: + """Handle delete product command.""" + storage = get_storage(hass) + deleted = await storage.delete_product(msg["product_id"]) + if not deleted: + connection.send_error(msg["id"], "not_found", "Product not found") + return + connection.send_result(msg["id"], {"deleted": True}) + + # ============================================================================= # CATEGORY HANDLERS # =============================================================================