fix: implement products/delete websocket handler — deleted products were silently ignored

This commit is contained in:
thekiwismarthome
2026-03-25 08:56:25 +13:00
parent 4b7043d075
commit cf3ab90d74
3 changed files with 38 additions and 0 deletions
@@ -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,
@@ -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:
@@ -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
# =============================================================================