diff --git a/custom_components/shopping_list_manager/__init__.py b/custom_components/shopping_list_manager/__init__.py index f1b66a5..64f928a 100644 --- a/custom_components/shopping_list_manager/__init__.py +++ b/custom_components/shopping_list_manager/__init__.py @@ -171,6 +171,10 @@ async def _async_register_websocket_handlers( ) # Products handlers + websocket_api.async_register_command( + hass, + handlers.websocket_search_by_barcode, + ) websocket_api.async_register_command( hass, handlers.websocket_search_products, diff --git a/custom_components/shopping_list_manager/websocket/handlers.py b/custom_components/shopping_list_manager/websocket/handlers.py index f00c527..0892478 100644 --- a/custom_components/shopping_list_manager/websocket/handlers.py +++ b/custom_components/shopping_list_manager/websocket/handlers.py @@ -782,6 +782,28 @@ def websocket_get_list_total( # PRODUCT HANDLERS # ============================================================================= +@websocket_api.websocket_command( + { + vol.Required("type"): "shopping_list_manager/products/search_by_barcode", + vol.Required("barcode"): str, + } +) +@callback +def websocket_search_by_barcode( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: Dict[str, Any], +) -> None: + """Find a single product by exact barcode match.""" + storage = get_storage(hass) + barcode = msg["barcode"].strip() + match = next( + (p for p in storage._products.values() if p.barcode and p.barcode == barcode), + None, + ) + connection.send_result(msg["id"], {"product": match.to_dict() if match else None}) + + @websocket_api.websocket_command( { vol.Required("type"): "shopping_list_manager/products/substitutes",