mirror of
https://github.com/thekiwismarthome/shopping-list-manager.git
synced 2026-05-01 11:46:30 +00:00
feat: Add websocket command to search for products by barcode.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user