Compare commits

..

50 Commits

Author SHA1 Message Date
thekiwismarthome 1f86b6f485 Merge pull request #15 from myTselection/config
fix config screen access #7
2026-06-15 12:19:07 +12:00
thekiwismarthome 2d02a68fe3 Merge pull request #16 from myTselection/dutch-translation
Dutch translation
2026-06-15 12:18:43 +12:00
myTselection c971b89779 us belgian openfoodfacts lookup when belgium catalog is used 2026-06-14 23:41:47 +02:00
myTselection 42746c86b8 v2.2.1 2026-06-14 23:28:25 +02:00
myTselection 6a344914a2 allow categories reload for different language 2026-06-14 23:28:10 +02:00
myTselection 8d9dd5bf6a v2.2.0 2026-06-14 22:51:36 +02:00
myTselection c48c96e133 belgium dutch categories and products 2026-06-14 22:51:16 +02:00
myTselection 1dea960449 v2.1.0 2026-06-14 22:46:59 +02:00
myTselection fd5b17ced8 fix startup warning Detected blocking call to open with args ('/config/custom_components/shopping_list_manager/manifest.json',) 2026-06-14 22:43:37 +02:00
myTselection 0468893919 base nl translation 2026-06-14 22:29:04 +02:00
myTselection 55d2f2d31d fix config screen access #7 2026-06-14 20:45:52 +02:00
thekiwismarthome d50bd39210 feat: expose installed version from manifest via get_integration_settings WS 2026-05-19 22:49:56 +12:00
thekiwismarthome 8652996b65 fix: add translations/en.json to resolve 500 on options flow 2026-05-18 20:29:48 +12:00
thekiwismarthome d5c43fe3b5 Merge pull request #12 from thekiwismarthome/v2.5.0---Improvements-and-Tweaks
V2.5.0   improvements and tweaks
2026-05-11 14:39:37 +12:00
thekiwismarthome 380bba0408 fix: proxy OpenFoodFacts API through HA backend to fix browser CORS errors; implement products/delete handler 2026-03-31 19:57:01 +13:00
thekiwismarthome cf3ab90d74 fix: implement products/delete websocket handler — deleted products were silently ignored 2026-03-25 08:56:25 +13:00
thekiwismarthome 4b7043d075 feat: product match review step when adding ingredients to SLM shopping list 2026-03-25 08:34:50 +13:00
thekiwismarthome e7306275e4 refactor: standardize image storage location and migrate existing files and URLs. 2026-03-03 22:59:44 +13:00
thekiwismarthome 9430811cda Merge pull request #6 from thekiwismarthome/2.3.0---Product-Barcode-Scanner-w/-OpenFoodFacts-Lookup
2.3.0   product barcode scanner w/ open food facts lookup
2026-03-03 17:52:21 +13:00
thekiwismarthome 2b11632253 feat: Improve image download robustness with a User-Agent header and extended timeout, and enhance WebP conversion by ensuring images are in RGB mode. 2026-03-01 22:24:14 +13:00
thekiwismarthome ae7717a8eb feat: Add WebSocket endpoint to download, process, and save product images locally as WebP. 2026-03-01 21:20:09 +13:00
thekiwismarthome 433c03035b feat: Add websocket command to search for products by barcode. 2026-03-01 20:15:29 +13:00
thekiwismarthome 8eb403ed8e docs: extensively update README with detailed features, requirements, and installation instructions. 2026-02-28 22:29:19 +13:00
thekiwismarthome 03fb9a9a67 Merge pull request #5 from thekiwismarthome/v2.1.0---Themes
V2.1.0   themes
2026-02-28 22:16:05 +13:00
thekiwismarthome ae133ae59b feat: Implement comprehensive access control for shopping list and item WebSocket handlers and refine country code validation. 2026-02-28 10:34:40 +13:00
thekiwismarthome 2a8b12a07e Add compiled Python bytecode files for the shopping list manager custom component. 2026-02-28 00:23:52 +13:00
thekiwismarthome a36933c4c6 Merge pull request #4 from thekiwismarthome/v2.0.5-Country-Specific-Product-Catalogues
V2.0.5 country specific product catalogues
2026-02-27 17:57:50 +13:00
thekiwismarthome 402881c687 feat: Add barcode_type field to the Card model and its WebSocket handlers for creation and updates. 2026-02-27 17:35:49 +13:00
thekiwismarthome d412764cba fix: Change the default value of the loyalty card 'private' field to true in the websocket handler. 2026-02-27 14:49:30 +13:00
thekiwismarthome 752f9e5622 feat: add loyalty card management with dedicated storage, data model, and websocket API endpoints. 2026-02-27 13:00:08 +13:00
thekiwismarthome 86896ba4af feat: Introduce private shopping lists with owner and member management, and add Home Assistant user lookup. 2026-02-26 14:41:43 +13:00
thekiwismarthome 36a8939ebc feat: Implement backup/restore functionality for user data via new WebSocket handlers and automatic config backups. 2026-02-26 09:53:05 +13:00
thekiwismarthome 57b6d52ddf feat: Add country selection for product catalogs, including new WebSocket handlers and catalog reloading logic. 2026-02-26 06:51:26 +13:00
thekiwismarthome 03249df651 Merge pull request #3 from thekiwismarthome/v2.0.4---Lots-of-Enhancements
V2.0.4 - Bug Fixes and New Features
2026-02-25 22:40:58 +13:00
thekiwismarthome 11180db0e3 feat: rebuild product search engine immediately after adding a new product 2026-02-25 21:54:33 +13:00
thekiwismarthome 9bdaea0b1b refactor: Replace storage.get_all_products() with storage.get_products() for product retrieval. 2026-02-25 08:19:29 +13:00
thekiwismarthome ec0f44f109 style: update color codes for Health & Beauty and Baby categories. 2026-02-19 11:32:39 +13:00
thekiwismarthome 88b3f2d435 Update handlers.py 2026-02-17 20:58:37 +13:00
thekiwismarthome d93ea86d72 duplicate websocket_search_products in handlers.py 2026-02-17 20:13:17 +13:00
thekiwismarthome 5b3dcb65b4 another fix 2026-02-17 19:14:23 +13:00
thekiwismarthome 94cdede3b9 non-admin event handler 2026-02-17 18:57:40 +13:00
thekiwismarthome e6117073be Sync Bug 2026-02-17 14:53:06 +13:00
thekiwismarthome e76fad5a92 Correct Increment Handler 2026-02-16 22:17:16 +13:00
thekiwismarthome bcfde6df9a Fix the Broken Increment Handler 2026-02-16 22:00:20 +13:00
thekiwismarthome 85e0e68af9 spelling error 2026-02-16 21:39:31 +13:00
thekiwismarthome 11eb698c20 Product Items VACA Sync Bug Fix 2026-02-16 21:27:23 +13:00
thekiwismarthome 0e3fcd56f5 Update __init__.py 2026-02-16 20:14:23 +13:00
thekiwismarthome 9d8fd3f63e Update handlers.py 2026-02-16 20:09:54 +13:00
thekiwismarthome c9c1d16f08 Update handlers.py 2026-02-16 20:05:22 +13:00
thekiwismarthome 3b0cff3476 Update handlers.py 2026-02-16 20:00:58 +13:00
26 changed files with 2517 additions and 106 deletions
+82 -17
View File
@@ -1,33 +1,98 @@
## 1. Installation (HACS) # Shopping List Manager Integration for Home Assistant
### Recommended The backend integration that powers the Shopping List Manager. Provides persistent multi-list storage, a 500+ product catalog, real-time WebSocket events, and a full API for the Lovelace card — all running natively inside Home Assistant.
> **Pair with the [Shopping List Manager Card](https://github.com/thekiwismarthome/shopping-list-manager-card)** for the full UI experience.
[![Open your Home Assistant instance and open this repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thekiwismarthome&repository=shopping-list-manager&category=integration) [![Open your Home Assistant instance and open this repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thekiwismarthome&repository=shopping-list-manager&category=integration)
1. Click the button above. ---
2. Confirm adding the repository to HACS.
3. Install **Shopping List Manager** from **HACS → Integrations**. ## Features
4. Restart Home Assistant.
### 🛒 Multi-List Management
- Create and manage multiple shopping lists
- Private or shared lists with per-member access control
- Active list state shared across all connected devices and users
- List total price calculation
### 📦 Items
- Add, update, check, and delete items with quantity and unit
- Atomic quantity increment / decrement
- Bulk check and clear checked items
- Per-item pricing, notes, and category assignment
### 🔍 Product Catalog
- **500+ products** (NZ-focused, extensible to AU, US, GB, CA)
- Fuzzy search with alias matching
- Recently-used product suggestions
- Custom product creation
- Allergen filtering and product substitute groups
- Product images (WebP, 200×200px, optimised)
### 🗂️ Categories
- 13 default categories — Produce, Dairy, Meat, Bakery, Pantry, Frozen, Beverages, Snacks, Household, Health, Pet, Baby, Other
- Category colour coding and emoji icons
- Per-list category ordering
### 💳 Loyalty Cards
- Store loyalty and rewards card data
- Private or shared card access per user
### 🔄 Real-Time Events
- All changes fire events on the Home Assistant bus
- Custom WebSocket subscription proxy so **non-admin users** receive live updates without requiring HA admin privileges
--- ---
### Manual Repository URL ## Requirements
https://github.com/thekiwismarthome/shopping-list-manager | Component | Minimum Version |
|---|---|
Repository type: **Integration** | Home Assistant | 2024.1 |
| HACS | 2.x |
--- ---
## 2. Manual Installation (Optional) ## Installation
1. Copy the folder: ### Via HACS (Recommended)
custom_components/shopping_list_manager
2. Paste it into: [![Open your Home Assistant instance and open this repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thekiwismarthome&repository=shopping-list-manager&category=integration)
/config/custom_components/
3. Restart Home Assistant. 1. Click the button above
2. Confirm adding the repository to HACS
3. Install **Shopping List Manager** from **HACS → Integrations**
4. Restart Home Assistant
5. Go to **Settings → Devices & Services → Add Integration** and search for **Shopping List Manager**
### Manual Installation
1. Copy the `custom_components/shopping_list_manager/` folder into your HA `/config/custom_components/` directory
2. Restart Home Assistant
3. Go to **Settings → Devices & Services → Add Integration** and search for **Shopping List Manager**
---
## Lovelace Card
Install the companion card to get the full shopping UI:
## 3. Shopping List Card to go with this Integration
[![Open your Home Assistant instance and open this repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thekiwismarthome&repository=shopping-list-manager-card&category=plugin) [![Open your Home Assistant instance and open this repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thekiwismarthome&repository=shopping-list-manager-card&category=plugin)
---
## Documentation
Full documentation is available in the [Wiki](https://github.com/thekiwismarthome/shopping-list-manager/wiki).
## Support & Feedback
- [Open an Issue](https://github.com/thekiwismarthome/shopping-list-manager/issues)
- [Home Assistant Community Forum](https://community.home-assistant.io)
---
## License
MIT — see [LICENSE](LICENSE) for details.
@@ -1,4 +1,5 @@
"""Shopping List Manager integration for Home Assistant.""" """Shopping List Manager integration for Home Assistant."""
import json
import logging import logging
import os import os
@@ -6,7 +7,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN from .const import DOMAIN, EVENT_ITEM_ADDED, EVENT_ITEM_UPDATED, EVENT_ITEM_CHECKED, EVENT_ITEM_DELETED, EVENT_LIST_UPDATED, EVENT_LIST_DELETED
from .storage import ShoppingListStorage from .storage import ShoppingListStorage
from .utils.images import ImageHandler from .utils.images import ImageHandler
@@ -16,6 +17,14 @@ _LOGGER = logging.getLogger(__name__)
DATA_STORAGE = f"{DOMAIN}_storage" DATA_STORAGE = f"{DOMAIN}_storage"
def _load_manifest_version(component_path: str) -> str:
"""Load the integration version from manifest.json."""
with open(os.path.join(component_path, "manifest.json"), encoding="utf-8") as manifest_file:
manifest = json.load(manifest_file)
return manifest.get("version", "unknown")
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Shopping List Manager component from yaml (not used).""" """Set up the Shopping List Manager component from yaml (not used)."""
# This integration doesn't support YAML configuration # This integration doesn't support YAML configuration
@@ -23,7 +32,6 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
# In async_setup_entry function, after storage initialization:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Shopping List Manager from a config entry.""" """Set up Shopping List Manager from a config entry."""
_LOGGER.info("Setting up Shopping List Manager") _LOGGER.info("Setting up Shopping List Manager")
@@ -43,11 +51,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Initialize image handler # Initialize image handler
image_handler = ImageHandler(hass, config_path) image_handler = ImageHandler(hass, config_path)
# Read installed version from manifest
version = await hass.async_add_executor_job(_load_manifest_version, component_path)
# Store instances in hass.data # Store instances in hass.data
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][DATA_STORAGE] = storage hass.data[DOMAIN][DATA_STORAGE] = storage
hass.data[DOMAIN]["image_handler"] = image_handler hass.data[DOMAIN]["image_handler"] = image_handler
hass.data[DOMAIN]["country"] = country hass.data[DOMAIN]["country"] = country
hass.data[DOMAIN]["version"] = version
# Register update listener for options changes # Register update listener for options changes
entry.async_on_unload(entry.add_update_listener(update_listener)) entry.async_on_unload(entry.add_update_listener(update_listener))
@@ -58,7 +70,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Register frontend resources # Register frontend resources
await _async_register_frontend(hass) await _async_register_frontend(hass)
_LOGGER.info("Shopping List Manager setup complete") # CRITICAL: Register event listeners so non-admin users can subscribe
# Without these, non-admin users cannot receive real-time updates
def _dummy_listener(event):
"""Dummy listener to enable event subscription for all users."""
pass
hass.bus.async_listen(EVENT_ITEM_ADDED, _dummy_listener)
hass.bus.async_listen(EVENT_ITEM_UPDATED, _dummy_listener)
hass.bus.async_listen(EVENT_ITEM_CHECKED, _dummy_listener)
hass.bus.async_listen(EVENT_ITEM_DELETED, _dummy_listener)
hass.bus.async_listen(EVENT_LIST_UPDATED, _dummy_listener)
hass.bus.async_listen(EVENT_LIST_DELETED, _dummy_listener)
_LOGGER.info("Shopping List Manager setup complete with event subscriptions enabled")
return True return True
@@ -88,6 +114,10 @@ async def _async_register_websocket_handlers(
from .websocket import handlers from .websocket import handlers
# Lists handlers # Lists handlers
websocket_api.async_register_command(
hass,
handlers.websocket_subscribe,
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_get_lists, handlers.websocket_get_lists,
@@ -126,6 +156,11 @@ async def _async_register_websocket_handlers(
hass, hass,
handlers.websocket_check_item, handlers.websocket_check_item,
) )
websocket_api.async_register_command(
hass,
handlers.websocket_increment_item,
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_delete_item, handlers.websocket_delete_item,
@@ -148,10 +183,23 @@ async def _async_register_websocket_handlers(
) )
# Products handlers # Products handlers
websocket_api.async_register_command(
hass,
handlers.websocket_download_product_image,
)
websocket_api.async_register_command(
hass,
handlers.websocket_search_by_barcode,
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_search_products, handlers.websocket_search_products,
) )
websocket_api.async_register_command(
hass,
handlers.ws_get_products_by_ids,
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_get_product_suggestions, handlers.websocket_get_product_suggestions,
@@ -164,17 +212,81 @@ async def _async_register_websocket_handlers(
hass, hass,
handlers.websocket_update_product, handlers.websocket_update_product,
) )
websocket_api.async_register_command(
hass,
handlers.websocket_delete_product,
)
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_get_product_substitutes, handlers.websocket_get_product_substitutes,
) )
# OpenFoodFacts proxy
websocket_api.async_register_command(
hass,
handlers.websocket_off_fetch,
)
# Categories handlers # Categories handlers
websocket_api.async_register_command( websocket_api.async_register_command(
hass, hass,
handlers.websocket_get_categories, handlers.websocket_get_categories,
) )
# Integration settings handlers
websocket_api.async_register_command(
hass,
handlers.websocket_get_integration_settings,
)
websocket_api.async_register_command(
hass,
handlers.websocket_set_country,
)
# Backup / Restore handlers
websocket_api.async_register_command(
hass,
handlers.websocket_export_data,
)
websocket_api.async_register_command(
hass,
handlers.websocket_import_data,
)
# List members handler
websocket_api.async_register_command(
hass,
handlers.websocket_update_list_members,
)
# HA users handler
websocket_api.async_register_command(
hass,
handlers.websocket_get_ha_users,
)
# Loyalty card handlers
websocket_api.async_register_command(
hass,
handlers.websocket_get_loyalty_cards,
)
websocket_api.async_register_command(
hass,
handlers.websocket_add_loyalty_card,
)
websocket_api.async_register_command(
hass,
handlers.websocket_update_loyalty_card,
)
websocket_api.async_register_command(
hass,
handlers.websocket_delete_loyalty_card,
)
websocket_api.async_register_command(
hass,
handlers.websocket_update_loyalty_card_members,
)
_LOGGER.debug("WebSocket handlers registered") _LOGGER.debug("WebSocket handlers registered")
@@ -184,8 +296,6 @@ async def _async_register_frontend(hass: HomeAssistant) -> None:
# The frontend card registers itself independently # The frontend card registers itself independently
_LOGGER.debug("Frontend resources skipped (separate HACS module)") _LOGGER.debug("Frontend resources skipped (separate HACS module)")
_LOGGER.debug("Frontend resources registered")
def get_storage(hass: HomeAssistant) -> ShoppingListStorage: def get_storage(hass: HomeAssistant) -> ShoppingListStorage:
"""Get the storage instance from hass.data. """Get the storage instance from hass.data.
@@ -50,7 +50,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry): def __init__(self, config_entry):
"""Initialize options flow.""" """Initialize options flow."""
self.config_entry = config_entry self._config_entry = config_entry
async def async_step_init(self, user_input=None): async def async_step_init(self, user_input=None):
"""Manage the options.""" """Manage the options."""
@@ -59,9 +59,9 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
return self.async_create_entry(title="", data=user_input) return self.async_create_entry(title="", data=user_input)
# Get current settings # Get current settings
current_country = self.config_entry.options.get( current_country = self._config_entry.options.get(
"country", "country",
self.config_entry.data.get("country", "NZ") self._config_entry.data.get("country", "NZ")
) )
return self.async_show_form( return self.async_show_form(
@@ -73,18 +73,19 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
"US": "United States", "US": "United States",
"GB": "United Kingdom", "GB": "United Kingdom",
"CA": "Canada", "CA": "Canada",
"BE": "Belgium (Dutch)",
}), }),
vol.Optional( vol.Optional(
"enable_price_tracking", "enable_price_tracking",
default=self.config_entry.options.get("enable_price_tracking", True) default=self._config_entry.options.get("enable_price_tracking", True)
): bool, ): bool,
vol.Optional( vol.Optional(
"enable_image_search", "enable_image_search",
default=self.config_entry.options.get("enable_image_search", True) default=self._config_entry.options.get("enable_image_search", True)
): bool, ): bool,
vol.Optional( vol.Optional(
"metric_units_only", "metric_units_only",
default=self.config_entry.options.get("metric_units_only", True) default=self._config_entry.options.get("metric_units_only", True)
): bool, ): bool,
}), }),
description_placeholders={ description_placeholders={
@@ -9,6 +9,7 @@ STORAGE_KEY_LISTS = f"{DOMAIN}.lists"
STORAGE_KEY_ITEMS = f"{DOMAIN}.items" STORAGE_KEY_ITEMS = f"{DOMAIN}.items"
STORAGE_KEY_PRODUCTS = f"{DOMAIN}.products" STORAGE_KEY_PRODUCTS = f"{DOMAIN}.products"
STORAGE_KEY_CATEGORIES = f"{DOMAIN}.categories" STORAGE_KEY_CATEGORIES = f"{DOMAIN}.categories"
STORAGE_KEY_LOYALTY_CARDS = f"{DOMAIN}.loyalty_cards"
# WebSocket Commands - Lists # WebSocket Commands - Lists
WS_TYPE_LISTS_GET_ALL = f"{DOMAIN}/lists/get_all" WS_TYPE_LISTS_GET_ALL = f"{DOMAIN}/lists/get_all"
@@ -16,6 +17,10 @@ WS_TYPE_LISTS_CREATE = f"{DOMAIN}/lists/create"
WS_TYPE_LISTS_UPDATE = f"{DOMAIN}/lists/update" WS_TYPE_LISTS_UPDATE = f"{DOMAIN}/lists/update"
WS_TYPE_LISTS_DELETE = f"{DOMAIN}/lists/delete" WS_TYPE_LISTS_DELETE = f"{DOMAIN}/lists/delete"
WS_TYPE_LISTS_SET_ACTIVE = f"{DOMAIN}/lists/set_active" WS_TYPE_LISTS_SET_ACTIVE = f"{DOMAIN}/lists/set_active"
WS_TYPE_LISTS_UPDATE_MEMBERS = f"{DOMAIN}/lists/update_members"
# WebSocket Commands - Users
WS_TYPE_USERS_GET_ALL = f"{DOMAIN}/users/get_all"
# WebSocket Commands - Items # WebSocket Commands - Items
WS_TYPE_ITEMS_GET = f"{DOMAIN}/items/get" WS_TYPE_ITEMS_GET = f"{DOMAIN}/items/get"
@@ -39,6 +44,13 @@ WS_TYPE_PRODUCTS_DELETE = f"{DOMAIN}/products/delete"
WS_TYPE_CATEGORIES_GET_ALL = f"{DOMAIN}/categories/get_all" WS_TYPE_CATEGORIES_GET_ALL = f"{DOMAIN}/categories/get_all"
WS_TYPE_CATEGORIES_REORDER = f"{DOMAIN}/categories/reorder" WS_TYPE_CATEGORIES_REORDER = f"{DOMAIN}/categories/reorder"
# WebSocket Commands - Loyalty Cards
WS_TYPE_LOYALTY_GET_ALL = f"{DOMAIN}/loyalty/get_all"
WS_TYPE_LOYALTY_ADD = f"{DOMAIN}/loyalty/add"
WS_TYPE_LOYALTY_UPDATE = f"{DOMAIN}/loyalty/update"
WS_TYPE_LOYALTY_DELETE = f"{DOMAIN}/loyalty/delete"
WS_TYPE_LOYALTY_UPDATE_MEMBERS = f"{DOMAIN}/loyalty/update_members"
# WebSocket Commands - Subscriptions # WebSocket Commands - Subscriptions
WS_TYPE_SUBSCRIBE = f"{DOMAIN}/subscribe" WS_TYPE_SUBSCRIBE = f"{DOMAIN}/subscribe"
WS_TYPE_UNSUBSCRIBE = f"{DOMAIN}/unsubscribe" WS_TYPE_UNSUBSCRIBE = f"{DOMAIN}/unsubscribe"
@@ -64,7 +76,10 @@ IMAGE_FORMAT = "webp"
IMAGE_SIZE = 200 # 200x200px IMAGE_SIZE = 200 # 200x200px
IMAGE_QUALITY = 85 IMAGE_QUALITY = 85
IMAGE_MAX_SIZE_KB = 15 IMAGE_MAX_SIZE_KB = 15
IMAGES_LOCAL_DIR = "www/shopping_list_manager/images" IMAGES_LOCAL_DIR = "www/images/shopping_list_manager"
LEGACY_IMAGES_LOCAL_DIR = "www/shopping_list_manager/images"
LOCAL_IMAGE_URL_PREFIX = "/local/images/shopping_list_manager/"
LEGACY_IMAGE_URL_PREFIX = "/local/shopping_list_manager/images/"
# Placeholder image (inline SVG) # Placeholder image (inline SVG)
PLACEHOLDER_IMAGE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Crect width='200' height='200' fill='%23f0f0f0'/%3E%3Ctext x='50%25' y='50%25' dominant-baseline='middle' text-anchor='middle' font-family='Arial' font-size='16' fill='%23999'%3ENo Image%3C/text%3E%3C/svg%3E" PLACEHOLDER_IMAGE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Crect width='200' height='200' fill='%23f0f0f0'/%3E%3Ctext x='50%25' y='50%25' dominant-baseline='middle' text-anchor='middle' font-family='Arial' font-size='16' fill='%23999'%3ENo Image%3C/text%3E%3C/svg%3E"
@@ -78,7 +78,7 @@
"id": "health", "id": "health",
"name": "Health & Beauty", "name": "Health & Beauty",
"icon": "mdi:heart-pulse", "icon": "mdi:heart-pulse",
"color": "#E91E63", "color": "#009688",
"sort_order": 10, "sort_order": 10,
"system": true "system": true
}, },
@@ -94,7 +94,7 @@
"id": "baby", "id": "baby",
"name": "Baby", "name": "Baby",
"icon": "mdi:baby-face", "icon": "mdi:baby-face",
"color": "#FFEB3B", "color": "#F48FB1",
"sort_order": 12, "sort_order": 12,
"system": true "system": true
}, },
@@ -0,0 +1,111 @@
{
"version": "1.0.0",
"region": "BE",
"language": "nl-BE",
"categories": [
{
"id": "produce",
"name": "Groenten & Fruit",
"icon": "mdi:fruit-cherries",
"color": "#4CAF50",
"sort_order": 1,
"system": true
},
{
"id": "dairy",
"name": "Zuivel & Eieren",
"icon": "mdi:cheese",
"color": "#FFC107",
"sort_order": 2,
"system": true
},
{
"id": "meat",
"name": "Vlees, Vis & Gevogelte",
"icon": "mdi:food-steak",
"color": "#F44336",
"sort_order": 3,
"system": true
},
{
"id": "bakery",
"name": "Bakkerij",
"icon": "mdi:bread-slice",
"color": "#FF9800",
"sort_order": 4,
"system": true
},
{
"id": "frozen",
"name": "Diepvries",
"icon": "mdi:snowflake",
"color": "#2196F3",
"sort_order": 5,
"system": true
},
{
"id": "pantry",
"name": "Voorraadkast",
"icon": "mdi:package-variant",
"color": "#795548",
"sort_order": 6,
"system": true
},
{
"id": "beverages",
"name": "Dranken",
"icon": "mdi:cup",
"color": "#00BCD4",
"sort_order": 7,
"system": true
},
{
"id": "snacks",
"name": "Snacks & Koekjes",
"icon": "mdi:food-apple",
"color": "#E91E63",
"sort_order": 8,
"system": true
},
{
"id": "household",
"name": "Huishouden",
"icon": "mdi:spray-bottle",
"color": "#9C27B0",
"sort_order": 9,
"system": true
},
{
"id": "health",
"name": "Verzorging",
"icon": "mdi:heart-pulse",
"color": "#009688",
"sort_order": 10,
"system": true
},
{
"id": "pet",
"name": "Dieren",
"icon": "mdi:paw",
"color": "#FF5722",
"sort_order": 11,
"system": true
},
{
"id": "baby",
"name": "Baby",
"icon": "mdi:baby-face",
"color": "#F48FB1",
"sort_order": 12,
"system": true
},
{
"id": "other",
"name": "Overig",
"icon": "mdi:dots-horizontal",
"color": "#9E9E9E",
"sort_order": 99,
"system": true
}
]
}
@@ -0,0 +1,941 @@
{
"version": "1.0.0",
"region": "BE",
"language": "nl-BE",
"currency": "EUR",
"last_updated": "2026-06-14",
"description": "Belgium Dutch grocery catalog with common Belgian supermarket products and local brands",
"products": [
{
"id": "prod_be_halfvolle_melk",
"name": "Halfvolle melk",
"category_id": "dairy",
"aliases": ["melk", "halfvolle melk", "halfvol"],
"default_unit": "L",
"default_quantity": 1,
"price": 1.15,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "halfvolle_melk",
"tags": ["basis"],
"allergens": ["milk"],
"substitution_group": "milk_group",
"priority_level": 5
},
{
"id": "prod_be_volle_melk",
"name": "Volle melk",
"category_id": "dairy",
"aliases": ["melk", "volle melk", "vollemelk"],
"default_unit": "L",
"default_quantity": 1,
"price": 1.25,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "volle_melk",
"tags": ["basis"],
"allergens": ["milk"],
"substitution_group": "milk_group",
"priority_level": 5
},
{
"id": "prod_be_havermelk",
"name": "Havermelk",
"category_id": "dairy",
"aliases": ["haverdrink", "havermelk", "plantaardige melk"],
"default_unit": "L",
"default_quantity": 1,
"price": 2.29,
"brands": ["Alpro", "Boni", "Oatly"],
"image_hint": "havermelk",
"tags": ["plantaardig"],
"allergens": ["gluten"],
"substitution_group": "milk_group",
"priority_level": 4
},
{
"id": "prod_be_boter",
"name": "Boter",
"category_id": "dairy",
"aliases": ["boter", "hoeveboter", "roomboter"],
"default_unit": "g",
"default_quantity": 250,
"price": 2.85,
"brands": ["Boni", "Balade", "Carlsbourg"],
"image_hint": "boter",
"tags": ["basis"],
"allergens": ["milk"],
"substitution_group": "butter_group",
"priority_level": 4
},
{
"id": "prod_be_eieren",
"name": "Eieren",
"category_id": "dairy",
"aliases": ["eieren", "eitjes", "vrije uitloop eieren"],
"default_unit": "stuks",
"default_quantity": 10,
"price": 3.25,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "eieren",
"tags": ["basis"],
"allergens": ["egg"],
"substitution_group": "eggs_group",
"priority_level": 5
},
{
"id": "prod_be_jonge_kaas",
"name": "Jonge kaas",
"category_id": "dairy",
"aliases": ["kaas", "jonge kaas", "sneetjes kaas"],
"default_unit": "g",
"default_quantity": 300,
"price": 3.79,
"brands": ["Boni", "Maredsous", "Passendale"],
"image_hint": "jonge_kaas",
"tags": ["broodbeleg"],
"allergens": ["milk"],
"substitution_group": "cheese_group",
"priority_level": 4
},
{
"id": "prod_be_yoghurt_natuur",
"name": "Yoghurt natuur",
"category_id": "dairy",
"aliases": ["yoghurt", "natuuryoghurt", "yoghurt natuur"],
"default_unit": "g",
"default_quantity": 500,
"price": 1.89,
"brands": ["Boni", "Danone", "Delhaize"],
"image_hint": "yoghurt_natuur",
"tags": [],
"allergens": ["milk"],
"substitution_group": "yoghurt_group",
"priority_level": 4
},
{
"id": "prod_be_griekse_yoghurt",
"name": "Griekse yoghurt",
"category_id": "dairy",
"aliases": ["griekse yoghurt", "yoghurt grieks"],
"default_unit": "g",
"default_quantity": 500,
"price": 2.65,
"brands": ["Boni", "Oikos", "Delhaize"],
"image_hint": "griekse_yoghurt",
"tags": [],
"allergens": ["milk"],
"substitution_group": "yoghurt_group",
"priority_level": 3
},
{
"id": "prod_be_appels",
"name": "Appels",
"category_id": "produce",
"aliases": ["appels", "jonagold", "elstar"],
"default_unit": "kg",
"default_quantity": 1,
"price": 2.49,
"brands": ["BelOrta", "Boni"],
"image_hint": "appels",
"tags": ["fruit"],
"allergens": [],
"substitution_group": "apple_group",
"priority_level": 5
},
{
"id": "prod_be_bananen",
"name": "Bananen",
"category_id": "produce",
"aliases": ["bananen", "banaan"],
"default_unit": "kg",
"default_quantity": 1,
"price": 1.89,
"brands": ["Chiquita", "Boni", "Delhaize"],
"image_hint": "bananen",
"tags": ["fruit"],
"allergens": [],
"substitution_group": "banana_group",
"priority_level": 5
},
{
"id": "prod_be_peren",
"name": "Peren",
"category_id": "produce",
"aliases": ["peren", "conference peer", "conference peren"],
"default_unit": "kg",
"default_quantity": 1,
"price": 2.59,
"brands": ["BelOrta", "Boni"],
"image_hint": "peren",
"tags": ["fruit"],
"allergens": [],
"substitution_group": "pear_group",
"priority_level": 4
},
{
"id": "prod_be_aardbeien",
"name": "Aardbeien",
"category_id": "produce",
"aliases": ["aardbeien", "belgische aardbeien"],
"default_unit": "g",
"default_quantity": 500,
"price": 4.49,
"brands": ["BelOrta", "Hoogstraten"],
"image_hint": "aardbeien",
"tags": ["fruit", "seizoen"],
"allergens": [],
"substitution_group": "berry_group",
"priority_level": 4
},
{
"id": "prod_be_aardappelen",
"name": "Aardappelen",
"category_id": "produce",
"aliases": ["aardappelen", "patatten", "vastkokende aardappelen"],
"default_unit": "kg",
"default_quantity": 2.5,
"price": 3.25,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "aardappelen",
"tags": ["groenten", "basis"],
"allergens": [],
"substitution_group": "potato_group",
"priority_level": 5
},
{
"id": "prod_be_tomaten",
"name": "Tomaten",
"category_id": "produce",
"aliases": ["tomaten", "trostomaten", "tomaat"],
"default_unit": "g",
"default_quantity": 500,
"price": 2.49,
"brands": ["BelOrta", "Boni", "Delhaize"],
"image_hint": "tomaten",
"tags": ["groenten"],
"allergens": [],
"substitution_group": "tomato_group",
"priority_level": 5
},
{
"id": "prod_be_witloof",
"name": "Witloof",
"category_id": "produce",
"aliases": ["witloof", "witlof", "grondwitloof"],
"default_unit": "g",
"default_quantity": 500,
"price": 2.99,
"brands": ["BelOrta", "Boni"],
"image_hint": "witloof",
"tags": ["groenten", "belgisch"],
"allergens": [],
"substitution_group": "chicory_group",
"priority_level": 4
},
{
"id": "prod_be_preien",
"name": "Prei",
"category_id": "produce",
"aliases": ["prei", "preien"],
"default_unit": "stuks",
"default_quantity": 3,
"price": 1.99,
"brands": ["Boni", "Delhaize"],
"image_hint": "prei",
"tags": ["groenten"],
"allergens": [],
"substitution_group": "leek_group",
"priority_level": 3
},
{
"id": "prod_be_wortelen",
"name": "Wortelen",
"category_id": "produce",
"aliases": ["wortelen", "peen", "wortels"],
"default_unit": "kg",
"default_quantity": 1,
"price": 1.49,
"brands": ["Boni", "Carrefour"],
"image_hint": "wortelen",
"tags": ["groenten"],
"allergens": [],
"substitution_group": "carrot_group",
"priority_level": 4
},
{
"id": "prod_be_uien",
"name": "Uien",
"category_id": "produce",
"aliases": ["uien", "ajuin", "gele uien"],
"default_unit": "kg",
"default_quantity": 1,
"price": 1.39,
"brands": ["Boni", "Delhaize"],
"image_hint": "uien",
"tags": ["groenten", "basis"],
"allergens": [],
"substitution_group": "onion_group",
"priority_level": 5
},
{
"id": "prod_be_sla",
"name": "Sla",
"category_id": "produce",
"aliases": ["sla", "kropsla", "salade"],
"default_unit": "stuks",
"default_quantity": 1,
"price": 1.59,
"brands": ["Boni", "Delhaize"],
"image_hint": "sla",
"tags": ["groenten"],
"allergens": [],
"substitution_group": "lettuce_group",
"priority_level": 3
},
{
"id": "prod_be_brood_wit",
"name": "Wit brood",
"category_id": "bakery",
"aliases": ["wit brood", "brood", "gesneden wit brood"],
"default_unit": "stuks",
"default_quantity": 1,
"price": 2.39,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "wit_brood",
"tags": ["basis"],
"allergens": ["gluten"],
"substitution_group": "bread_group",
"priority_level": 5
},
{
"id": "prod_be_brood_grijs",
"name": "Grijs brood",
"category_id": "bakery",
"aliases": ["grijs brood", "bruin brood", "tarwebrood"],
"default_unit": "stuks",
"default_quantity": 1,
"price": 2.49,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "grijs_brood",
"tags": ["basis"],
"allergens": ["gluten"],
"substitution_group": "bread_group",
"priority_level": 5
},
{
"id": "prod_be_pistolets",
"name": "Pistolets",
"category_id": "bakery",
"aliases": ["pistolets", "broodjes", "witte pistolets"],
"default_unit": "stuks",
"default_quantity": 6,
"price": 2.19,
"brands": ["Delhaize", "Carrefour", "Boni"],
"image_hint": "pistolets",
"tags": ["belgisch"],
"allergens": ["gluten"],
"substitution_group": "rolls_group",
"priority_level": 4
},
{
"id": "prod_be_sandwiches",
"name": "Sandwiches",
"category_id": "bakery",
"aliases": ["sandwiches", "melkbroodjes", "zachte broodjes"],
"default_unit": "stuks",
"default_quantity": 6,
"price": 2.69,
"brands": ["Boni", "Delhaize"],
"image_hint": "sandwiches",
"tags": ["belgisch"],
"allergens": ["gluten", "milk", "egg"],
"substitution_group": "rolls_group",
"priority_level": 3
},
{
"id": "prod_be_luikse_wafels",
"name": "Luikse wafels",
"category_id": "bakery",
"aliases": ["luikse wafels", "suikerwafels", "wafels"],
"default_unit": "stuks",
"default_quantity": 6,
"price": 3.49,
"brands": ["Lotus", "Boni", "Milcamps"],
"image_hint": "luikse_wafels",
"tags": ["belgisch"],
"allergens": ["gluten", "milk", "egg"],
"substitution_group": "waffle_group",
"priority_level": 3
},
{
"id": "prod_be_kipfilet",
"name": "Kipfilet",
"category_id": "meat",
"aliases": ["kipfilet", "kip", "kippenfilet"],
"default_unit": "g",
"default_quantity": 600,
"price": 7.49,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "kipfilet",
"tags": [],
"allergens": [],
"substitution_group": "chicken_group",
"priority_level": 5
},
{
"id": "prod_be_gehakt",
"name": "Gemengd gehakt",
"category_id": "meat",
"aliases": ["gehakt", "gemengd gehakt", "varkens runds gehakt"],
"default_unit": "g",
"default_quantity": 500,
"price": 4.99,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "gehakt",
"tags": [],
"allergens": [],
"substitution_group": "mince_group",
"priority_level": 4
},
{
"id": "prod_be_spekblokjes",
"name": "Spekblokjes",
"category_id": "meat",
"aliases": ["spekblokjes", "spekreepjes", "spek"],
"default_unit": "g",
"default_quantity": 200,
"price": 2.99,
"brands": ["Herta", "Boni", "Delhaize"],
"image_hint": "spekblokjes",
"tags": [],
"allergens": [],
"substitution_group": "bacon_group",
"priority_level": 3
},
{
"id": "prod_be_hesp",
"name": "Hesp",
"category_id": "meat",
"aliases": ["hesp", "ham", "gekookte hesp"],
"default_unit": "g",
"default_quantity": 150,
"price": 2.89,
"brands": ["Boni", "Aoste", "Delhaize"],
"image_hint": "hesp",
"tags": ["broodbeleg"],
"allergens": [],
"substitution_group": "ham_group",
"priority_level": 4
},
{
"id": "prod_be_zalmfilet",
"name": "Zalmfilet",
"category_id": "meat",
"aliases": ["zalm", "zalmfilet", "verse zalm"],
"default_unit": "g",
"default_quantity": 250,
"price": 6.99,
"brands": ["Delhaize", "Carrefour", "Boni"],
"image_hint": "zalmfilet",
"tags": ["vis"],
"allergens": ["fish"],
"substitution_group": "salmon_group",
"priority_level": 3
},
{
"id": "prod_be_frietjes_dieprvries",
"name": "Diepvriesfrieten",
"category_id": "frozen",
"aliases": ["diepvriesfrieten", "frieten", "frietjes", "frites"],
"default_unit": "kg",
"default_quantity": 1,
"price": 2.39,
"brands": ["Boni", "Lutosa", "McCain"],
"image_hint": "diepvriesfrieten",
"tags": ["belgisch"],
"allergens": [],
"substitution_group": "fries_group",
"priority_level": 5
},
{
"id": "prod_be_groentenmix_dieprvries",
"name": "Diepvriesgroenten",
"category_id": "frozen",
"aliases": ["diepvriesgroenten", "groentenmix", "diepvries groentemix"],
"default_unit": "g",
"default_quantity": 600,
"price": 2.49,
"brands": ["Boni", "Delhaize", "Carrefour"],
"image_hint": "diepvriesgroenten",
"tags": ["groenten"],
"allergens": [],
"substitution_group": "frozen_vegetables_group",
"priority_level": 4
},
{
"id": "prod_be_vanille_ijs",
"name": "Vanille-ijs",
"category_id": "frozen",
"aliases": ["vanille ijs", "vanille-ijs", "roomijs"],
"default_unit": "L",
"default_quantity": 1,
"price": 3.49,
"brands": ["Boni", "Ijsboerke", "Delhaize"],
"image_hint": "vanille_ijs",
"tags": [],
"allergens": ["milk", "egg"],
"substitution_group": "ice_cream_group",
"priority_level": 2
},
{
"id": "prod_be_pasta",
"name": "Pasta",
"category_id": "pantry",
"aliases": ["pasta", "spaghetti", "penne"],
"default_unit": "g",
"default_quantity": 500,
"price": 1.39,
"brands": ["Boni", "Barilla", "Delhaize"],
"image_hint": "pasta",
"tags": ["basis"],
"allergens": ["gluten"],
"substitution_group": "pasta_group",
"priority_level": 5
},
{
"id": "prod_be_rijst",
"name": "Rijst",
"category_id": "pantry",
"aliases": ["rijst", "basmatirijst", "lange korrel rijst"],
"default_unit": "kg",
"default_quantity": 1,
"price": 2.19,
"brands": ["Boni", "Uncle Ben's", "Delhaize"],
"image_hint": "rijst",
"tags": ["basis"],
"allergens": [],
"substitution_group": "rice_group",
"priority_level": 5
},
{
"id": "prod_be_bloem",
"name": "Bloem",
"category_id": "pantry",
"aliases": ["bloem", "tarwebloem", "zelfrijzende bloem"],
"default_unit": "kg",
"default_quantity": 1,
"price": 0.99,
"brands": ["Boni", "Anco", "Delhaize"],
"image_hint": "bloem",
"tags": ["bakken", "basis"],
"allergens": ["gluten"],
"substitution_group": "flour_group",
"priority_level": 4
},
{
"id": "prod_be_suiker",
"name": "Suiker",
"category_id": "pantry",
"aliases": ["suiker", "kristalsuiker", "witte suiker"],
"default_unit": "kg",
"default_quantity": 1,
"price": 1.29,
"brands": ["Tienen-Tirlemont", "Boni", "Delhaize"],
"image_hint": "suiker",
"tags": ["bakken", "basis"],
"allergens": [],
"substitution_group": "sugar_group",
"priority_level": 4
},
{
"id": "prod_be_koffie",
"name": "Koffie",
"category_id": "pantry",
"aliases": ["koffie", "gemalen koffie", "koffiebonen"],
"default_unit": "g",
"default_quantity": 500,
"price": 5.49,
"brands": ["Douwe Egberts", "Rombouts", "Boni"],
"image_hint": "koffie",
"tags": ["ontbijt"],
"allergens": [],
"substitution_group": "coffee_group",
"priority_level": 5
},
{
"id": "prod_be_choco",
"name": "Choco",
"category_id": "pantry",
"aliases": ["choco", "chocopasta", "hazelnootpasta"],
"default_unit": "g",
"default_quantity": 400,
"price": 2.99,
"brands": ["Nutella", "Boni", "Kwatta"],
"image_hint": "choco",
"tags": ["broodbeleg"],
"allergens": ["milk", "nuts"],
"substitution_group": "spread_group",
"priority_level": 4
},
{
"id": "prod_be_confituur",
"name": "Confituur",
"category_id": "pantry",
"aliases": ["confituur", "jam", "aardbeienconfituur"],
"default_unit": "g",
"default_quantity": 450,
"price": 2.39,
"brands": ["Materne", "Boni", "Delhaize"],
"image_hint": "confituur",
"tags": ["broodbeleg"],
"allergens": [],
"substitution_group": "jam_group",
"priority_level": 3
},
{
"id": "prod_be_mayonaise",
"name": "Mayonaise",
"category_id": "pantry",
"aliases": ["mayonaise", "mayo", "fritessaus"],
"default_unit": "ml",
"default_quantity": 500,
"price": 2.89,
"brands": ["Devos Lemmens", "Boni", "Delhaize"],
"image_hint": "mayonaise",
"tags": ["belgisch"],
"allergens": ["egg", "mustard"],
"substitution_group": "mayonnaise_group",
"priority_level": 5
},
{
"id": "prod_be_pickles",
"name": "Pickles",
"category_id": "pantry",
"aliases": ["pickles", "belgische pickles", "piccalilly"],
"default_unit": "g",
"default_quantity": 350,
"price": 2.49,
"brands": ["Devos Lemmens", "Bister", "Boni"],
"image_hint": "pickles",
"tags": ["belgisch"],
"allergens": ["mustard"],
"substitution_group": "sauce_group",
"priority_level": 3
},
{
"id": "prod_be_olijfolie",
"name": "Olijfolie",
"category_id": "pantry",
"aliases": ["olijfolie", "extra vierge olijfolie"],
"default_unit": "ml",
"default_quantity": 750,
"price": 6.49,
"brands": ["Boni", "Bertolli", "Delhaize"],
"image_hint": "olijfolie",
"tags": ["basis"],
"allergens": [],
"substitution_group": "oil_group",
"priority_level": 4
},
{
"id": "prod_be_zout",
"name": "Zout",
"category_id": "pantry",
"aliases": ["zout", "keukenzout", "zeezout"],
"default_unit": "g",
"default_quantity": 500,
"price": 0.79,
"brands": ["Boni", "Jozo", "Delhaize"],
"image_hint": "zout",
"tags": ["basis"],
"allergens": [],
"substitution_group": "salt_group",
"priority_level": 3
},
{
"id": "prod_be_peper",
"name": "Peper",
"category_id": "pantry",
"aliases": ["peper", "zwarte peper", "pepermolen"],
"default_unit": "g",
"default_quantity": 50,
"price": 2.29,
"brands": ["Boni", "Ducros", "Delhaize"],
"image_hint": "peper",
"tags": ["basis"],
"allergens": [],
"substitution_group": "spice_group",
"priority_level": 3
},
{
"id": "prod_be_spa_bruis",
"name": "Bruiswater",
"category_id": "beverages",
"aliases": ["bruiswater", "spuitwater", "water bruis"],
"default_unit": "L",
"default_quantity": 1.5,
"price": 0.89,
"brands": ["Spa", "Chaudfontaine", "Boni"],
"image_hint": "bruiswater",
"tags": [],
"allergens": [],
"substitution_group": "water_group",
"priority_level": 5
},
{
"id": "prod_be_plat_water",
"name": "Plat water",
"category_id": "beverages",
"aliases": ["plat water", "mineraalwater", "water"],
"default_unit": "L",
"default_quantity": 1.5,
"price": 0.75,
"brands": ["Spa", "Chaudfontaine", "Boni"],
"image_hint": "plat_water",
"tags": [],
"allergens": [],
"substitution_group": "water_group",
"priority_level": 5
},
{
"id": "prod_be_sinaasappelsap",
"name": "Sinaasappelsap",
"category_id": "beverages",
"aliases": ["sinaasappelsap", "appelsiensap", "fruitsap"],
"default_unit": "L",
"default_quantity": 1,
"price": 2.19,
"brands": ["Minute Maid", "Boni", "Delhaize"],
"image_hint": "sinaasappelsap",
"tags": [],
"allergens": [],
"substitution_group": "juice_group",
"priority_level": 3
},
{
"id": "prod_be_pils",
"name": "Pils",
"category_id": "beverages",
"aliases": ["pils", "bier", "pintjes"],
"default_unit": "stuks",
"default_quantity": 6,
"price": 5.99,
"brands": ["Jupiler", "Stella Artois", "Maes"],
"image_hint": "pils",
"tags": ["belgisch", "alcohol"],
"allergens": ["gluten"],
"substitution_group": "beer_group",
"priority_level": 3
},
{
"id": "prod_be_coladrank",
"name": "Cola",
"category_id": "beverages",
"aliases": ["cola", "coca cola", "frisdrank"],
"default_unit": "L",
"default_quantity": 1.5,
"price": 2.19,
"brands": ["Coca-Cola", "Pepsi", "Boni"],
"image_hint": "cola",
"tags": [],
"allergens": [],
"substitution_group": "soft_drink_group",
"priority_level": 3
},
{
"id": "prod_be_speculoos",
"name": "Speculoos",
"category_id": "snacks",
"aliases": ["speculoos", "speculaas", "lotus koekjes"],
"default_unit": "g",
"default_quantity": 250,
"price": 2.29,
"brands": ["Lotus", "Boni", "Delhaize"],
"image_hint": "speculoos",
"tags": ["belgisch"],
"allergens": ["gluten"],
"substitution_group": "biscuit_group",
"priority_level": 4
},
{
"id": "prod_be_chocolade",
"name": "Chocolade",
"category_id": "snacks",
"aliases": ["chocolade", "tablet chocolade", "melkchocolade"],
"default_unit": "g",
"default_quantity": 200,
"price": 2.49,
"brands": ["Cote d'Or", "Jacques", "Boni"],
"image_hint": "chocolade",
"tags": ["belgisch"],
"allergens": ["milk", "soy", "nuts"],
"substitution_group": "chocolate_group",
"priority_level": 4
},
{
"id": "prod_be_chips_paprika",
"name": "Paprikachips",
"category_id": "snacks",
"aliases": ["paprikachips", "chips paprika", "chips"],
"default_unit": "g",
"default_quantity": 200,
"price": 1.79,
"brands": ["Lay's", "Boni", "Croky"],
"image_hint": "paprikachips",
"tags": [],
"allergens": [],
"substitution_group": "chips_group",
"priority_level": 3
},
{
"id": "prod_be_wasmiddel",
"name": "Wasmiddel",
"category_id": "household",
"aliases": ["wasmiddel", "vloeibaar wasmiddel", "waspoeder"],
"default_unit": "L",
"default_quantity": 1.5,
"price": 8.49,
"brands": ["Dash", "Ariel", "Boni"],
"image_hint": "wasmiddel",
"tags": [],
"allergens": [],
"substitution_group": "laundry_group",
"priority_level": 3
},
{
"id": "prod_be_afwasmiddel",
"name": "Afwasmiddel",
"category_id": "household",
"aliases": ["afwasmiddel", "dreft", "vaatwasmiddel handwas"],
"default_unit": "ml",
"default_quantity": 500,
"price": 1.99,
"brands": ["Dreft", "Boni", "Delhaize"],
"image_hint": "afwasmiddel",
"tags": [],
"allergens": [],
"substitution_group": "dish_soap_group",
"priority_level": 4
},
{
"id": "prod_be_wc_papier",
"name": "Toiletpapier",
"category_id": "household",
"aliases": ["toiletpapier", "wc papier", "wc-papier"],
"default_unit": "rollen",
"default_quantity": 12,
"price": 4.99,
"brands": ["Page", "Boni", "Delhaize"],
"image_hint": "toiletpapier",
"tags": ["basis"],
"allergens": [],
"substitution_group": "toilet_paper_group",
"priority_level": 5
},
{
"id": "prod_be_keukenpapier",
"name": "Keukenpapier",
"category_id": "household",
"aliases": ["keukenpapier", "keukenrol", "huishoudpapier"],
"default_unit": "rollen",
"default_quantity": 2,
"price": 2.29,
"brands": ["Boni", "Page", "Delhaize"],
"image_hint": "keukenpapier",
"tags": [],
"allergens": [],
"substitution_group": "paper_towel_group",
"priority_level": 3
},
{
"id": "prod_be_tandpasta",
"name": "Tandpasta",
"category_id": "health",
"aliases": ["tandpasta", "fluoride tandpasta"],
"default_unit": "ml",
"default_quantity": 75,
"price": 2.49,
"brands": ["Colgate", "Signal", "Oral-B"],
"image_hint": "tandpasta",
"tags": [],
"allergens": [],
"substitution_group": "toothpaste_group",
"priority_level": 3
},
{
"id": "prod_be_shampoo",
"name": "Shampoo",
"category_id": "health",
"aliases": ["shampoo", "haarshampoo"],
"default_unit": "ml",
"default_quantity": 300,
"price": 3.99,
"brands": ["Nivea", "Dove", "Garnier"],
"image_hint": "shampoo",
"tags": [],
"allergens": [],
"substitution_group": "shampoo_group",
"priority_level": 2
},
{
"id": "prod_be_luiers",
"name": "Luiers",
"category_id": "baby",
"aliases": ["luiers", "pamper", "pampers"],
"default_unit": "stuks",
"default_quantity": 36,
"price": 9.99,
"brands": ["Pampers", "Boni", "Delhaize"],
"image_hint": "luiers",
"tags": [],
"allergens": [],
"substitution_group": "diaper_group",
"priority_level": 4
},
{
"id": "prod_be_babydoekjes",
"name": "Babydoekjes",
"category_id": "baby",
"aliases": ["babydoekjes", "vochtige doekjes", "billendoekjes"],
"default_unit": "stuks",
"default_quantity": 64,
"price": 1.99,
"brands": ["Pampers", "Boni", "Zwitsal"],
"image_hint": "babydoekjes",
"tags": [],
"allergens": [],
"substitution_group": "baby_wipes_group",
"priority_level": 3
},
{
"id": "prod_be_kattenvoer",
"name": "Kattenvoer",
"category_id": "pet",
"aliases": ["kattenvoer", "kattenbrokken", "kat voeding"],
"default_unit": "kg",
"default_quantity": 1.5,
"price": 5.99,
"brands": ["Whiskas", "Felix", "Boni"],
"image_hint": "kattenvoer",
"tags": [],
"allergens": [],
"substitution_group": "cat_food_group",
"priority_level": 3
},
{
"id": "prod_be_hondenvoer",
"name": "Hondenvoer",
"category_id": "pet",
"aliases": ["hondenvoer", "hondenbrokken", "hond voeding"],
"default_unit": "kg",
"default_quantity": 3,
"price": 8.49,
"brands": ["Pedigree", "Cesar", "Boni"],
"image_hint": "hondenvoer",
"tags": [],
"allergens": [],
"substitution_group": "dog_food_group",
"priority_level": 3
}
],
"country": "Belgium"
}
@@ -1,7 +1,7 @@
{ {
"domain": "shopping_list_manager", "domain": "shopping_list_manager",
"name": "Shopping List Manager", "name": "Shopping List Manager",
"version": "2.0.0", "version": "2.2.2",
"documentation": "https://github.com/thekiwismarthome/shopping-list-manager", "documentation": "https://github.com/thekiwismarthome/shopping-list-manager",
"issue_tracker": "https://github.com/thekiwismarthome/shopping-list-manager/issues", "issue_tracker": "https://github.com/thekiwismarthome/shopping-list-manager/issues",
"requirements": [ "requirements": [
@@ -96,6 +96,28 @@ class Item:
self.estimated_total = self.quantity * self.price self.estimated_total = self.quantity * self.price
@dataclass
class LoyaltyCard:
"""Loyalty card model."""
id: str
name: str
number: str
barcode: str = ""
barcode_type: str = "barcode" # "barcode" or "qrcode"
logo: str = ""
notes: str = ""
color: str = "#9fa8da"
created_at: str = field(default_factory=current_timestamp)
updated_at: str = field(default_factory=current_timestamp)
# Ownership: None = visible to all users; set = private to owner + allowed_users
owner_id: Optional[str] = None
allowed_users: List[str] = field(default_factory=list)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return asdict(self)
@dataclass @dataclass
class ShoppingList: class ShoppingList:
"""Shopping list model.""" """Shopping list model."""
@@ -107,6 +129,9 @@ class ShoppingList:
item_order: List[str] = field(default_factory=list) item_order: List[str] = field(default_factory=list)
category_order: List[str] = field(default_factory=list) category_order: List[str] = field(default_factory=list)
active: bool = False active: bool = False
# Ownership: None = visible to all users; set = private to owner + allowed_users
owner_id: Optional[str] = None
allowed_users: List[str] = field(default_factory=list)
def to_dict(self) -> Dict[str, Any]: def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary.""" """Convert to dictionary."""
@@ -1,5 +1,10 @@
"""Storage management for Shopping List Manager.""" """Storage management for Shopping List Manager."""
import json
import logging import logging
import os
import shutil
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, List, Optional, Any from typing import Dict, List, Optional, Any
from .utils.search import ProductSearch from .utils.search import ProductSearch
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@@ -11,9 +16,14 @@ from .const import (
STORAGE_KEY_ITEMS, STORAGE_KEY_ITEMS,
STORAGE_KEY_PRODUCTS, STORAGE_KEY_PRODUCTS,
STORAGE_KEY_CATEGORIES, STORAGE_KEY_CATEGORIES,
STORAGE_KEY_LOYALTY_CARDS,
IMAGES_LOCAL_DIR,
LEGACY_IMAGES_LOCAL_DIR,
LOCAL_IMAGE_URL_PREFIX,
LEGACY_IMAGE_URL_PREFIX,
) )
from .data.catalog_loader import load_product_catalog from .data.catalog_loader import load_product_catalog
from .models import ShoppingList, Item, Product, Category, generate_id from .models import ShoppingList, Item, Product, Category, LoyaltyCard, generate_id
from .data.category_loader import load_categories from .data.category_loader import load_categories
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -37,12 +47,16 @@ class ShoppingListStorage:
self._store_items = Store(hass, STORAGE_VERSION, STORAGE_KEY_ITEMS) self._store_items = Store(hass, STORAGE_VERSION, STORAGE_KEY_ITEMS)
self._store_products = Store(hass, STORAGE_VERSION, STORAGE_KEY_PRODUCTS) self._store_products = Store(hass, STORAGE_VERSION, STORAGE_KEY_PRODUCTS)
self._store_categories = Store(hass, STORAGE_VERSION, STORAGE_KEY_CATEGORIES) self._store_categories = Store(hass, STORAGE_VERSION, STORAGE_KEY_CATEGORIES)
self._store_loyalty_cards = Store(hass, STORAGE_VERSION, STORAGE_KEY_LOYALTY_CARDS)
self._lists: Dict[str, ShoppingList] = {} self._lists: Dict[str, ShoppingList] = {}
self._items: Dict[str, List[Item]] = {} self._items: Dict[str, List[Item]] = {}
self._products: Dict[str, Product] = {} self._products: Dict[str, Product] = {}
self._categories: List[Category] = [] self._categories: List[Category] = []
self._loyalty_cards: Dict[str, LoyaltyCard] = {}
self._search_engine: Optional[ProductSearch] = None self._search_engine: Optional[ProductSearch] = None
self._images_dir = Path(hass.config.path(IMAGES_LOCAL_DIR))
self._legacy_images_dir = Path(hass.config.path(LEGACY_IMAGES_LOCAL_DIR))
async def async_load(self) -> None: async def async_load(self) -> None:
"""Load data from storage.""" """Load data from storage."""
@@ -84,21 +98,12 @@ class ShoppingListStorage:
} }
_LOGGER.debug("Loaded %d products", len(self._products)) _LOGGER.debug("Loaded %d products", len(self._products))
# Load categories # Ensure image directory exists and migrate legacy image paths/URLs.
categories_data = await self._store_categories.async_load() self._images_dir.mkdir(parents=True, exist_ok=True)
if categories_data: await self._migrate_legacy_images_and_urls()
self._categories = [Category(**cat_data) for cat_data in categories_data]
_LOGGER.debug("Loaded %d categories", len(self._categories)) # Load country-specific system categories so labels follow the selected country.
else: await self._load_categories_for_country(self._country)
# Initialize with default categories from JSON file
default_categories = await load_categories(self._component_path, self._country) # Use self._country
self._categories = [Category(**cat) for cat in default_categories]
await self._save_categories()
_LOGGER.info(
"Initialized %d default categories for country: %s",
len(self._categories),
self._country # Use self._country
)
# Load product catalog if products are empty # Load product catalog if products are empty
if not self._products: if not self._products:
@@ -141,6 +146,15 @@ class ShoppingListStorage:
await self._save_products() await self._save_products()
_LOGGER.info("Successfully imported %d products from catalog", len(self._products)) _LOGGER.info("Successfully imported %d products from catalog", len(self._products))
# Load loyalty cards
loyalty_data = await self._store_loyalty_cards.async_load()
if loyalty_data:
self._loyalty_cards = {
card_id: LoyaltyCard(**card_data)
for card_id, card_data in loyalty_data.items()
}
_LOGGER.debug("Loaded %d loyalty cards", len(self._loyalty_cards))
# Initialize search engine after products are loaded # Initialize search engine after products are loaded
if self._products: if self._products:
products_dict = {pid: p.to_dict() for pid, p in self._products.items()} products_dict = {pid: p.to_dict() for pid, p in self._products.items()}
@@ -156,9 +170,21 @@ class ShoppingListStorage:
data = {list_id: lst.to_dict() for list_id, lst in self._lists.items()} data = {list_id: lst.to_dict() for list_id, lst in self._lists.items()}
await self._store_lists.async_save(data) await self._store_lists.async_save(data)
def get_lists(self) -> List[ShoppingList]: def get_lists(self, user_id: str = None, is_admin: bool = False) -> List[ShoppingList]:
"""Get all lists.""" """Get lists visible to the specified user.
return list(self._lists.values())
Global lists (owner_id=None) are visible to everyone.
Private lists are visible to their owner, anyone in allowed_users, and admins.
"""
all_lists = list(self._lists.values())
if is_admin or user_id is None:
return all_lists
return [
lst for lst in all_lists
if lst.owner_id is None
or lst.owner_id == user_id
or user_id in (lst.allowed_users or [])
]
def get_list(self, list_id: str) -> Optional[ShoppingList]: def get_list(self, list_id: str) -> Optional[ShoppingList]:
"""Get a specific list.""" """Get a specific list."""
@@ -171,17 +197,19 @@ class ShoppingListStorage:
return lst return lst
return None return None
async def create_list(self, name: str, icon: str = "mdi:cart") -> ShoppingList: async def create_list(self, name: str, icon: str = "mdi:cart", owner_id: str = None) -> ShoppingList:
"""Create a new list.""" """Create a new list. Pass owner_id to make the list private to that user."""
new_list = ShoppingList( new_list = ShoppingList(
id=generate_id(), id=generate_id(),
name=name, name=name,
icon=icon, icon=icon,
category_order=[cat.id for cat in self._categories] category_order=[cat.id for cat in self._categories],
owner_id=owner_id,
) )
self._lists[new_list.id] = new_list self._lists[new_list.id] = new_list
self._items[new_list.id] = [] self._items[new_list.id] = []
await self._save_lists() await self._save_lists()
await self._write_config_backup()
_LOGGER.info("Created new list: %s", name) _LOGGER.info("Created new list: %s", name)
return new_list return new_list
@@ -202,6 +230,18 @@ class ShoppingListStorage:
_LOGGER.debug("Updated list: %s", list_id) _LOGGER.debug("Updated list: %s", list_id)
return lst return lst
async def update_list_members(self, list_id: str, allowed_users: List[str]) -> Optional[ShoppingList]:
"""Update the allowed_users for a private list."""
if list_id not in self._lists:
return None
lst = self._lists[list_id]
lst.allowed_users = allowed_users
from .models import current_timestamp
lst.updated_at = current_timestamp()
await self._save_lists()
_LOGGER.debug("Updated members for list: %s", list_id)
return lst
async def delete_list(self, list_id: str) -> bool: async def delete_list(self, list_id: str) -> bool:
"""Delete a list.""" """Delete a list."""
if list_id not in self._lists: if list_id not in self._lists:
@@ -379,6 +419,56 @@ class ShoppingListStorage:
data = {product_id: product.to_dict() for product_id, product in self._products.items()} data = {product_id: product.to_dict() for product_id, product in self._products.items()}
await self._store_products.async_save(data) await self._store_products.async_save(data)
async def _migrate_legacy_images_and_urls(self) -> None:
"""Move old image files and rewrite stored URLs to the new path."""
moved_files = 0
updated_product_urls = 0
updated_item_urls = 0
if self._legacy_images_dir.exists() and self._legacy_images_dir != self._images_dir:
for src in self._legacy_images_dir.glob("*"):
if not src.is_file():
continue
dest = self._images_dir / src.name
if dest.exists():
continue
try:
shutil.move(str(src), str(dest))
moved_files += 1
except Exception as err:
_LOGGER.debug("Could not move legacy image %s: %s", src, err)
for product in self._products.values():
image_url = product.image_url or ""
if image_url.startswith(LEGACY_IMAGE_URL_PREFIX):
product.image_url = image_url.replace(
LEGACY_IMAGE_URL_PREFIX, LOCAL_IMAGE_URL_PREFIX, 1
)
updated_product_urls += 1
for items in self._items.values():
for item in items:
image_url = item.image_url or ""
if image_url.startswith(LEGACY_IMAGE_URL_PREFIX):
item.image_url = image_url.replace(
LEGACY_IMAGE_URL_PREFIX, LOCAL_IMAGE_URL_PREFIX, 1
)
updated_item_urls += 1
if updated_product_urls:
await self._save_products()
if updated_item_urls:
await self._save_items()
if moved_files or updated_product_urls or updated_item_urls:
_LOGGER.info(
"Migrated shopping list images to %s (moved_files=%d, updated_product_urls=%d, updated_item_urls=%d)",
IMAGES_LOCAL_DIR,
moved_files,
updated_product_urls,
updated_item_urls,
)
def get_products(self) -> List[Product]: def get_products(self) -> List[Product]:
"""Get all products.""" """Get all products."""
return list(self._products.values()) return list(self._products.values())
@@ -460,9 +550,75 @@ class ShoppingListStorage:
) )
self._products[new_product.id] = new_product self._products[new_product.id] = new_product
await self._save_products() await self._save_products()
await self._write_config_backup()
# Rebuild search engine so the new product is immediately searchable
products_dict = {pid: p.to_dict() for pid, p in self._products.items()}
self._search_engine = ProductSearch(products_dict)
_LOGGER.debug("Added product: %s", new_product.name) _LOGGER.debug("Added product: %s", new_product.name)
return new_product return new_product
async def reload_catalog(self, country_code: str) -> int:
"""Replace catalog-sourced products with those from a new country's catalog.
Products with source='user' are preserved."""
catalog_ids = [
pid for pid, p in self._products.items()
if getattr(p, 'source', 'user') == 'catalog'
]
for pid in catalog_ids:
del self._products[pid]
self._country = country_code
await self._load_categories_for_country(country_code)
catalog_products = await load_product_catalog(self._component_path, country_code)
count = 0
for prod_data in catalog_products:
try:
product = Product(
id=prod_data.get("id", generate_id()),
name=prod_data["name"],
category_id=prod_data.get("category_id", "other"),
aliases=prod_data.get("aliases", []),
default_unit=prod_data.get("default_unit", "units"),
default_quantity=prod_data.get("default_quantity", 1),
price=prod_data.get("price") or prod_data.get("typical_price"),
currency=self.hass.config.currency,
barcode=prod_data.get("barcode"),
brands=prod_data.get("brands", []),
image_url=prod_data.get("image_url", ""),
custom=False,
source="catalog",
tags=prod_data.get("tags", []),
collections=prod_data.get("collections", []),
taxonomy=prod_data.get("taxonomy", {}),
allergens=prod_data.get("allergens", []),
substitution_group=prod_data.get("substitution_group", ""),
priority_level=prod_data.get("priority_level", 0),
image_hint=prod_data.get("image_hint", "")
)
self._products[product.id] = product
count += 1
except Exception as err:
_LOGGER.error("Failed to import product %s: %s", prod_data.get("name"), err)
await self._save_products()
products_dict = {pid: p.to_dict() for pid, p in self._products.items()}
self._search_engine = ProductSearch(products_dict)
_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]: async def update_product(self, product_id: str, **kwargs) -> Optional[Product]:
"""Update a product.""" """Update a product."""
if product_id not in self._products: if product_id not in self._products:
@@ -474,10 +630,114 @@ class ShoppingListStorage:
setattr(product, key, value) setattr(product, key, value)
await self._save_products() await self._save_products()
await self._write_config_backup()
_LOGGER.debug("Updated product: %s", product_id) _LOGGER.debug("Updated product: %s", product_id)
return product return product
# ---------------------------------------------------------------------------
# Backup / Restore
# ---------------------------------------------------------------------------
async def export_user_data(self) -> dict:
"""Return a serialisable snapshot of all user-created data."""
user_products = [
p.to_dict() for p in self._products.values()
if getattr(p, "source", "user") == "user"
]
lists = [lst.to_dict() for lst in self._lists.values()]
items = {
list_id: [item.to_dict() for item in items_list]
for list_id, items_list in self._items.items()
}
return {
"slm_backup_version": "1.0",
"exported_at": datetime.now(timezone.utc).isoformat(),
"country": self._country,
"user_products": user_products,
"lists": lists,
"items": items,
}
async def import_user_data(self, data: dict) -> dict:
"""Merge a backup into live storage. Skips anything already present by ID."""
imported_products = 0
imported_lists = 0
imported_items = 0
for prod_data in data.get("user_products", []):
prod_id = prod_data.get("id")
if prod_id and prod_id not in self._products:
try:
self._products[prod_id] = Product(**prod_data)
imported_products += 1
except Exception as err:
_LOGGER.warning("Skipped product during import: %s", err)
if imported_products:
await self._save_products()
products_dict = {pid: p.to_dict() for pid, p in self._products.items()}
self._search_engine = ProductSearch(products_dict)
for list_data in data.get("lists", []):
list_id = list_data.get("id")
if list_id and list_id not in self._lists:
try:
lst = ShoppingList(**list_data)
lst.active = False
self._lists[list_id] = lst
imported_lists += 1
except Exception as err:
_LOGGER.warning("Skipped list during import: %s", err)
backup_items = data.get("items", {})
for list_id, items_list in backup_items.items():
if list_id in self._lists and list_id not in self._items:
try:
self._items[list_id] = [Item(**d) for d in items_list]
imported_items += len(self._items[list_id])
except Exception as err:
_LOGGER.warning("Skipped items for list %s: %s", list_id, err)
if imported_lists or imported_items:
await self._save_lists()
await self._save_items()
_LOGGER.info(
"Import complete: %d products, %d lists, %d items",
imported_products, imported_lists, imported_items,
)
return {"products": imported_products, "lists": imported_lists, "items": imported_items}
async def _write_config_backup(self) -> None:
"""Silently write a backup JSON to the HA config directory."""
try:
backup_path = os.path.join(
self.hass.config.config_dir,
"shopping_list_manager_backup.json",
)
data = await self.export_user_data()
def _write() -> None:
with open(backup_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
await self.hass.async_add_executor_job(_write)
_LOGGER.debug("Auto-backup written to %s", backup_path)
except Exception as err:
_LOGGER.warning("Failed to write config backup: %s", err)
# Categories methods # Categories methods
async def _load_categories_for_country(self, country_code: str) -> None:
"""Load and persist system categories for the selected country."""
categories = await load_categories(self._component_path, country_code)
self._categories = [Category(**cat_data) for cat_data in categories]
await self._save_categories()
_LOGGER.info(
"Loaded %d categories for country: %s",
len(self._categories),
country_code,
)
async def _save_categories(self) -> None: async def _save_categories(self) -> None:
"""Save categories to storage.""" """Save categories to storage."""
data = [cat.to_dict() for cat in self._categories] data = [cat.to_dict() for cat in self._categories]
@@ -486,3 +746,81 @@ class ShoppingListStorage:
def get_categories(self) -> List[Category]: def get_categories(self) -> List[Category]:
"""Get all categories.""" """Get all categories."""
return self._categories return self._categories
# Loyalty card methods
async def _save_loyalty_cards(self) -> None:
"""Save loyalty cards to storage."""
data = {card_id: card.to_dict() for card_id, card in self._loyalty_cards.items()}
await self._store_loyalty_cards.async_save(data)
def get_loyalty_cards(self, user_id: str = None, is_admin: bool = False) -> List[LoyaltyCard]:
"""Get loyalty cards visible to the specified user.
Global cards (owner_id=None) are visible to everyone.
Private cards are visible to their owner, anyone in allowed_users, and admins.
"""
all_cards = list(self._loyalty_cards.values())
if is_admin or user_id is None:
return all_cards
return [
card for card in all_cards
if card.owner_id is None
or card.owner_id == user_id
or user_id in (card.allowed_users or [])
]
def get_loyalty_card(self, card_id: str) -> Optional[LoyaltyCard]:
"""Get a specific loyalty card."""
return self._loyalty_cards.get(card_id)
async def create_loyalty_card(self, owner_id: str = None, **kwargs) -> LoyaltyCard:
"""Create a new loyalty card."""
from .models import current_timestamp
new_card = LoyaltyCard(
id=generate_id(),
owner_id=owner_id,
**kwargs
)
self._loyalty_cards[new_card.id] = new_card
await self._save_loyalty_cards()
_LOGGER.debug("Created loyalty card: %s", new_card.name)
return new_card
async def update_loyalty_card(self, card_id: str, **kwargs) -> Optional[LoyaltyCard]:
"""Update a loyalty card."""
if card_id not in self._loyalty_cards:
return None
card = self._loyalty_cards[card_id]
for key, value in kwargs.items():
if hasattr(card, key):
setattr(card, key, value)
from .models import current_timestamp
card.updated_at = current_timestamp()
await self._save_loyalty_cards()
_LOGGER.debug("Updated loyalty card: %s", card_id)
return card
async def delete_loyalty_card(self, card_id: str) -> bool:
"""Delete a loyalty card."""
if card_id not in self._loyalty_cards:
return False
del self._loyalty_cards[card_id]
await self._save_loyalty_cards()
_LOGGER.debug("Deleted loyalty card: %s", card_id)
return True
async def update_loyalty_card_members(self, card_id: str, allowed_users: List[str]) -> Optional[LoyaltyCard]:
"""Update the allowed_users for a private loyalty card."""
if card_id not in self._loyalty_cards:
return None
card = self._loyalty_cards[card_id]
card.allowed_users = allowed_users
from .models import current_timestamp
card.updated_at = current_timestamp()
await self._save_loyalty_cards()
_LOGGER.debug("Updated members for loyalty card: %s", card_id)
return card
@@ -0,0 +1,33 @@
{
"config": {
"step": {
"user": {
"title": "Shopping List Manager",
"description": "Set up the Shopping List Manager integration. Country and other settings can be configured after setup via the Configure button."
}
},
"abort": {
"single_instance_allowed": "Only a single instance of Shopping List Manager is allowed."
}
},
"options": {
"step": {
"init": {
"title": "Shopping List Manager Options",
"description": "Changing country will reload the product catalog on next restart.",
"data": {
"country": "Country",
"enable_price_tracking": "Enable price tracking",
"enable_image_search": "Enable image search",
"metric_units_only": "Metric units only"
},
"data_description": {
"country": "Used to localise product catalog and pricing.",
"enable_price_tracking": "Track and display product prices.",
"enable_image_search": "Search for product images automatically.",
"metric_units_only": "Show only metric units (g, kg, ml, L)."
}
}
}
}
}
@@ -0,0 +1,33 @@
{
"config": {
"step": {
"user": {
"title": "Shopping List Manager",
"description": "Stel de Shopping List Manager-integratie in. Land en andere instellingen kunnen na de setup worden geconfigureerd via de knop Configureren."
}
},
"abort": {
"single_instance_allowed": "Slechts één exemplaar van Shopping List Manager is toegestaan."
}
},
"options": {
"step": {
"init": {
"title": "Shopping List Manager Opties",
"description": "Het wijzigen van land zal de productcatalogus bij de volgende herstart herladen.",
"data": {
"country": "Land",
"enable_price_tracking": "Prijstracking inschakelen",
"enable_image_search": "Afbeeldingen zoeken inschakelen",
"metric_units_only": "Alleen metrische eenheden"
},
"data_description": {
"country": "Wordt gebruikt om de productcatalogus en prijzen te lokaliseren.",
"enable_price_tracking": "Productprijzen bijhouden en weergeven.",
"enable_image_search": "Automatisch naar productafbeeldingen zoeken.",
"metric_units_only": "Alleen metrische eenheden weergeven (g, kg, ml, L)."
}
}
}
}
}
@@ -1,9 +1,15 @@
"""Image handling utilities for Shopping List Manager.""" """Image handling utilities for Shopping List Manager."""
import logging import logging
import os import shutil
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
from ..const import (
IMAGES_LOCAL_DIR,
LEGACY_IMAGES_LOCAL_DIR,
LOCAL_IMAGE_URL_PREFIX,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -18,12 +24,29 @@ class ImageHandler:
config_path: Path to HA config directory config_path: Path to HA config directory
""" """
self.hass = hass self.hass = hass
# Images stored in /config/www/shopping_list_manager/images/ # Images stored in /config/www/images/shopping_list_manager/
self._local_images_dir = Path(config_path) / "www" / "shopping_list_manager" / "images" self._local_images_dir = Path(hass.config.path(IMAGES_LOCAL_DIR))
self._legacy_images_dir = Path(hass.config.path(LEGACY_IMAGES_LOCAL_DIR))
self._local_images_dir.mkdir(parents=True, exist_ok=True) self._local_images_dir.mkdir(parents=True, exist_ok=True)
self._migrate_legacy_files()
_LOGGER.info("Image directory: %s", self._local_images_dir) _LOGGER.info("Image directory: %s", self._local_images_dir)
def _migrate_legacy_files(self) -> None:
"""Move legacy image files to the new standardized directory."""
if not self._legacy_images_dir.exists() or self._legacy_images_dir == self._local_images_dir:
return
for src in self._legacy_images_dir.glob("*"):
if not src.is_file():
continue
dest = self._local_images_dir / src.name
if dest.exists():
continue
try:
shutil.move(str(src), str(dest))
except Exception as err:
_LOGGER.debug("Could not move legacy image %s: %s", src, err)
def get_image_url(self, product_name: str, external_url: Optional[str] = None) -> str: def get_image_url(self, product_name: str, external_url: Optional[str] = None) -> str:
"""Get image URL for a product. """Get image URL for a product.
@@ -73,11 +96,11 @@ class ImageHandler:
# Check exact match # Check exact match
image_file = self._local_images_dir / f"{normalized_name}{ext}" image_file = self._local_images_dir / f"{normalized_name}{ext}"
if image_file.exists(): if image_file.exists():
return f"/local/shopping_list_manager/images/{normalized_name}{ext}" return f"{LOCAL_IMAGE_URL_PREFIX}{normalized_name}{ext}"
# Check for files starting with the product name # Check for files starting with the product name
for file in self._local_images_dir.glob(f"{normalized_name}*{ext}"): for file in self._local_images_dir.glob(f"{normalized_name}*{ext}"):
return f"/local/shopping_list_manager/images/{file.name}" return f"{LOCAL_IMAGE_URL_PREFIX}{file.name}"
return None return None
@@ -1,18 +1,31 @@
"""WebSocket API handlers for Shopping List Manager.""" """WebSocket API handlers for Shopping List Manager."""
import io
import logging import logging
import re
from pathlib import Path
from typing import Any, Dict from typing import Any, Dict
import voluptuous as vol import voluptuous as vol
from aiohttp import ClientTimeout
from PIL import Image
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from ..const import DOMAIN
from ..const import ( from ..const import (
IMAGE_SIZE,
IMAGE_QUALITY,
IMAGES_LOCAL_DIR,
LOCAL_IMAGE_URL_PREFIX,
WS_TYPE_LISTS_GET_ALL, WS_TYPE_LISTS_GET_ALL,
WS_TYPE_LISTS_CREATE, WS_TYPE_LISTS_CREATE,
WS_TYPE_LISTS_UPDATE, WS_TYPE_LISTS_UPDATE,
WS_TYPE_LISTS_DELETE, WS_TYPE_LISTS_DELETE,
WS_TYPE_LISTS_SET_ACTIVE, WS_TYPE_LISTS_SET_ACTIVE,
WS_TYPE_LISTS_UPDATE_MEMBERS,
WS_TYPE_USERS_GET_ALL,
WS_TYPE_ITEMS_GET, WS_TYPE_ITEMS_GET,
WS_TYPE_ITEMS_ADD, WS_TYPE_ITEMS_ADD,
WS_TYPE_ITEMS_UPDATE, WS_TYPE_ITEMS_UPDATE,
@@ -26,7 +39,15 @@ from ..const import (
WS_TYPE_PRODUCTS_SUGGESTIONS, WS_TYPE_PRODUCTS_SUGGESTIONS,
WS_TYPE_PRODUCTS_ADD, WS_TYPE_PRODUCTS_ADD,
WS_TYPE_PRODUCTS_UPDATE, WS_TYPE_PRODUCTS_UPDATE,
WS_TYPE_PRODUCTS_DELETE,
WS_TYPE_OFF_FETCH,
WS_TYPE_CATEGORIES_GET_ALL, WS_TYPE_CATEGORIES_GET_ALL,
WS_TYPE_LOYALTY_GET_ALL,
WS_TYPE_LOYALTY_ADD,
WS_TYPE_LOYALTY_UPDATE,
WS_TYPE_LOYALTY_DELETE,
WS_TYPE_LOYALTY_UPDATE_MEMBERS,
WS_TYPE_SUBSCRIBE,
EVENT_ITEM_ADDED, EVENT_ITEM_ADDED,
EVENT_ITEM_UPDATED, EVENT_ITEM_UPDATED,
EVENT_ITEM_CHECKED, EVENT_ITEM_CHECKED,
@@ -38,11 +59,216 @@ from .. import get_storage
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
OPENFOODFACTS_DEFAULT_BASE_URL = "https://world.openfoodfacts.org"
OPENFOODFACTS_BASE_URL_BY_COUNTRY = {
"BE": "https://be.openfoodfacts.org",
}
OPENFOODFACTS_ACCEPT_LANGUAGE_BY_COUNTRY = {
"BE": "nl-BE,nl;q=0.9,en;q=0.7",
}
# =============================================================================
# ACCESS-CHECK HELPERS
# =============================================================================
def _get_openfoodfacts_request_config(hass: HomeAssistant) -> tuple[str, Dict[str, str]]:
"""Return the OpenFoodFacts base URL and headers for the active catalog."""
country = hass.data.get(DOMAIN, {}).get("country", "NZ")
base_url = OPENFOODFACTS_BASE_URL_BY_COUNTRY.get(
country,
OPENFOODFACTS_DEFAULT_BASE_URL,
)
headers = {
"User-Agent": "HomeAssistant/ShoppingListManager (contact@homeassistant.io)",
}
if accept_language := OPENFOODFACTS_ACCEPT_LANGUAGE_BY_COUNTRY.get(country):
headers["Accept-Language"] = accept_language
return base_url, headers
def _user_can_access_list(lst, user) -> bool:
"""Return True if the user may read or write to this list.
Global lists (owner_id=None) are accessible to everyone.
Private lists are accessible to their owner, anyone in allowed_users, and admins.
"""
if lst.owner_id is None:
return True
if user is None:
return False
if user.is_admin or user.id == lst.owner_id:
return True
return user.id in (lst.allowed_users or [])
def _check_list_access(storage, connection, msg, list_id, require_owner=False):
"""Verify the connected user may access list_id.
Sends the appropriate WebSocket error if access is denied.
Returns the ShoppingList object on success, or None if an error was sent.
Args:
require_owner: When True, only the list owner (or an admin) is allowed.
Use for destructive/administrative operations.
"""
lst = storage.get_list(list_id)
if lst is None:
connection.send_error(msg["id"], "not_found", "List not found")
return None
user = connection.user
if require_owner:
if lst.owner_id is not None and not (user and (user.is_admin or user.id == lst.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the list owner can perform this action")
return None
else:
if not _user_can_access_list(lst, user):
connection.send_error(msg["id"], "forbidden", "You do not have access to this list")
return None
return lst
def _find_item_list_id(storage, item_id):
"""Return the list_id that contains item_id, or None if not found."""
for list_id, items in storage._items.items():
for item in items:
if item.id == item_id:
return list_id
return None
# ============================================================================= # =============================================================================
# LIST HANDLERS # LIST HANDLERS
# ============================================================================= # =============================================================================
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_SUBSCRIBE,
})
@websocket_api.async_response
async def websocket_subscribe(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict,
) -> None:
"""Subscribe to shopping list manager events via WebSocket."""
storage = get_storage(hass)
@callback
def forward_event(event):
"""Forward HA bus event to WebSocket connection.
Events that reference a list_id are only forwarded if the connected
user has access to that list, preventing cross-user data leakage.
"""
data = event.data
list_id = data.get("list_id")
if list_id:
lst = storage.get_list(list_id)
if lst and not _user_can_access_list(lst, connection.user):
return # skip — user cannot see this list
connection.send_message(
websocket_api.event_message(
msg["id"],
{
"event_type": event.event_type,
"data": data,
}
)
)
# Subscribe to all SLM events on the HA bus (backend has permission)
unsubs = []
unsubs.append(hass.bus.async_listen(EVENT_ITEM_ADDED, forward_event))
unsubs.append(hass.bus.async_listen(EVENT_ITEM_UPDATED, forward_event))
unsubs.append(hass.bus.async_listen(EVENT_ITEM_CHECKED, forward_event))
unsubs.append(hass.bus.async_listen(EVENT_ITEM_DELETED, forward_event))
unsubs.append(hass.bus.async_listen(EVENT_LIST_UPDATED, forward_event))
unsubs.append(hass.bus.async_listen(EVENT_LIST_DELETED, forward_event))
# Clean up when connection closes
connection.subscriptions[msg["id"]] = lambda: [unsub() for unsub in unsubs]
connection.send_message(websocket_api.result_message(msg["id"]))
@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"]
# First get current item
for list_id, items in storage._items.items():
for item in items:
if item.id == item_id:
new_quantity = item.quantity + amount
if new_quantity < 1:
new_quantity = 1
updated_item = await storage.update_item(
item_id,
quantity=new_quantity
)
if updated_item:
hass.bus.async_fire(
EVENT_ITEM_UPDATED,
{
"list_id": updated_item.list_id,
"item_id": item_id,
"item": updated_item.to_dict()
}
)
connection.send_result(msg["id"], {
"item": updated_item.to_dict()
})
return
connection.send_error(msg["id"], "not_found", "Item not found")
@websocket_api.websocket_command({
vol.Required("type"): "shopping_list_manager/products/get_by_ids",
vol.Required("product_ids"): [str],
})
@websocket_api.async_response
async def ws_get_products_by_ids(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Return products matching given product IDs."""
storage = get_storage(hass)
product_ids = set(msg["product_ids"])
# Get all products from storage
all_products = storage.get_products()
products = [
product.to_dict()
for product in all_products
if product.id in product_ids
]
connection.send_result(msg["id"], {"products": products})
@websocket_api.websocket_command( @websocket_api.websocket_command(
{ {
vol.Required("type"): WS_TYPE_LISTS_GET_ALL, vol.Required("type"): WS_TYPE_LISTS_GET_ALL,
@@ -56,7 +282,10 @@ def websocket_get_lists(
) -> None: ) -> None:
"""Handle get all lists command.""" """Handle get all lists command."""
storage = get_storage(hass) storage = get_storage(hass)
lists = storage.get_lists() user = connection.user
user_id = user.id if user else None
is_admin = user.is_admin if user else False
lists = storage.get_lists(user_id=user_id, is_admin=is_admin)
connection.send_result( connection.send_result(
msg["id"], msg["id"],
@@ -71,6 +300,7 @@ def websocket_get_lists(
vol.Required("type"): WS_TYPE_LISTS_CREATE, vol.Required("type"): WS_TYPE_LISTS_CREATE,
vol.Required("name"): str, vol.Required("name"): str,
vol.Optional("icon", default="mdi:cart"): str, vol.Optional("icon", default="mdi:cart"): str,
vol.Optional("private", default=True): bool,
} }
) )
@websocket_api.async_response @websocket_api.async_response
@@ -82,9 +312,14 @@ async def websocket_create_list(
"""Handle create list command.""" """Handle create list command."""
storage = get_storage(hass) storage = get_storage(hass)
# Private lists are owned by the creating user; global lists have no owner.
is_private = msg.get("private", True)
owner_id = connection.user.id if is_private and connection.user else None
new_list = await storage.create_list( new_list = await storage.create_list(
name=msg["name"], name=msg["name"],
icon=msg.get("icon", "mdi:cart") icon=msg.get("icon", "mdi:cart"),
owner_id=owner_id,
) )
# Fire event # Fire event
@@ -118,6 +353,9 @@ async def websocket_update_list(
storage = get_storage(hass) storage = get_storage(hass)
list_id = msg["list_id"] list_id = msg["list_id"]
if _check_list_access(storage, connection, msg, list_id, require_owner=True) is None:
return
# Build update kwargs # Build update kwargs
update_data = {} update_data = {}
if "name" in msg: if "name" in msg:
@@ -161,6 +399,18 @@ async def websocket_delete_list(
storage = get_storage(hass) storage = get_storage(hass)
list_id = msg["list_id"] list_id = msg["list_id"]
lst = storage.get_list(list_id)
if lst is None:
connection.send_error(msg["id"], "not_found", "List not found")
return
# Only the owner or an admin may delete a private list
if lst.owner_id is not None:
user = connection.user
if not (user and (user.is_admin or user.id == lst.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the list owner can delete this list")
return
success = await storage.delete_list(list_id) success = await storage.delete_list(list_id)
if not success: if not success:
@@ -192,6 +442,9 @@ async def websocket_set_active_list(
storage = get_storage(hass) storage = get_storage(hass)
list_id = msg["list_id"] list_id = msg["list_id"]
if _check_list_access(storage, connection, msg, list_id) is None:
return
success = await storage.set_active_list(list_id) success = await storage.set_active_list(list_id)
if not success: if not success:
@@ -227,6 +480,9 @@ def websocket_get_items(
storage = get_storage(hass) storage = get_storage(hass)
list_id = msg["list_id"] list_id = msg["list_id"]
if _check_list_access(storage, connection, msg, list_id) is None:
return
items = storage.get_items(list_id) items = storage.get_items(list_id)
connection.send_result( connection.send_result(
@@ -242,7 +498,7 @@ def websocket_get_items(
vol.Required("type"): WS_TYPE_ITEMS_ADD, vol.Required("type"): WS_TYPE_ITEMS_ADD,
vol.Required("list_id"): str, vol.Required("list_id"): str,
vol.Required("name"): str, vol.Required("name"): str,
vol.Required("category_id"): str, vol.Optional("category_id", default="other"): str,
vol.Optional("product_id"): str, vol.Optional("product_id"): str,
vol.Optional("quantity", default=1): vol.Coerce(float), vol.Optional("quantity", default=1): vol.Coerce(float),
vol.Optional("unit", default="units"): str, vol.Optional("unit", default="units"): str,
@@ -262,6 +518,9 @@ async def websocket_add_item(
storage = get_storage(hass) storage = get_storage(hass)
list_id = msg["list_id"] list_id = msg["list_id"]
if _check_list_access(storage, connection, msg, list_id) is None:
return
# Build item data # Build item data
item_data = { item_data = {
"name": msg["name"], "name": msg["name"],
@@ -321,6 +580,13 @@ async def websocket_update_item(
storage = get_storage(hass) storage = get_storage(hass)
item_id = msg["item_id"] item_id = msg["item_id"]
list_id = _find_item_list_id(storage, item_id)
if list_id is None:
connection.send_error(msg["id"], "not_found", "Item not found")
return
if _check_list_access(storage, connection, msg, list_id) is None:
return
# Build update data # Build update data
update_data = {} update_data = {}
update_fields = ["name", "quantity", "unit", "note", "price", "category_id", "image_url"] update_fields = ["name", "quantity", "unit", "note", "price", "category_id", "image_url"]
@@ -406,6 +672,13 @@ async def websocket_delete_item(
storage = get_storage(hass) storage = get_storage(hass)
item_id = msg["item_id"] item_id = msg["item_id"]
list_id = _find_item_list_id(storage, item_id)
if list_id is None:
connection.send_error(msg["id"], "not_found", "Item not found")
return
if _check_list_access(storage, connection, msg, list_id) is None:
return
success = await storage.delete_item(item_id) success = await storage.delete_item(item_id)
if not success: if not success:
@@ -547,39 +820,83 @@ def websocket_get_list_total(
@websocket_api.websocket_command( @websocket_api.websocket_command(
{ {
vol.Required("type"): WS_TYPE_PRODUCTS_SEARCH, vol.Required("type"): "shopping_list_manager/products/download_image",
vol.Required("query"): str, vol.Required("image_url"): str,
vol.Optional("limit", default=10): int, vol.Required("product_name"): str,
vol.Optional("exclude_allergens", default=None): vol.Any(None, [str]),
vol.Optional("include_tags", default=None): vol.Any(None, [str]),
vol.Optional("substitution_group", default=None): vol.Any(None, str),
} }
) )
@callback @websocket_api.async_response
def websocket_search_products( async def websocket_download_product_image(
hass: HomeAssistant, hass: HomeAssistant,
connection: websocket_api.ActiveConnection, connection: websocket_api.ActiveConnection,
msg: Dict[str, Any], msg: Dict[str, Any],
) -> None: ) -> None:
"""Handle search products command with enhanced filters.""" """Download a remote image and save it as WebP to the local images directory."""
storage = get_storage(hass) raw_url: str = msg["image_url"]
product_name: str = msg["product_name"]
safe_stem = re.sub(r"[^a-z0-9_]", "", product_name.lower().replace(" ", "_")) or "product"
filename = f"{safe_stem}.webp"
images_dir = Path(hass.config.path(IMAGES_LOCAL_DIR))
images_dir.mkdir(parents=True, exist_ok=True)
dest = images_dir / filename
try: try:
results = storage.search_products( session = async_get_clientsession(hass)
query=msg["query"], headers = {"User-Agent": "Mozilla/5.0 (compatible; HomeAssistant/ShoppingListManager)"}
limit=msg.get("limit", 10), async with session.get(raw_url, timeout=ClientTimeout(total=15), headers=headers) as resp:
exclude_allergens=msg.get("exclude_allergens"), if resp.status != 200:
include_tags=msg.get("include_tags"), connection.send_error(msg["id"], "download_failed", f"HTTP {resp.status}")
substitution_group=msg.get("substitution_group"), return
) raw = await resp.read()
except Exception as exc: # noqa: BLE001
connection.send_error(msg["id"], "download_failed", str(exc))
return
try:
img = Image.open(io.BytesIO(raw))
# Convert to RGB for reliable lossy WebP encoding
# (RGBA, palette, grayscale modes can fail or produce oversized files)
if img.mode == "RGBA":
bg = Image.new("RGB", img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[3])
img = bg
elif img.mode != "RGB":
img = img.convert("RGB")
img.thumbnail((IMAGE_SIZE, IMAGE_SIZE), Image.LANCZOS)
out = io.BytesIO()
img.save(out, format="WEBP", quality=IMAGE_QUALITY)
dest.write_bytes(out.getvalue())
except Exception as exc: # noqa: BLE001
connection.send_error(msg["id"], "conversion_failed", str(exc))
return
connection.send_result( connection.send_result(
msg["id"], msg["id"],
{"products": [product.to_dict() for product in results]} {"local_url": f"{LOCAL_IMAGE_URL_PREFIX}{filename}"},
) )
except Exception as err:
_LOGGER.error("Error searching products: %s", err)
connection.send_error(msg["id"], "search_failed", str(err)) @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( @websocket_api.websocket_command(
@@ -766,6 +1083,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 # CATEGORY HANDLERS
# ============================================================================= # =============================================================================
@@ -791,3 +1129,381 @@ def websocket_get_categories(
"categories": [cat.to_dict() for cat in categories] "categories": [cat.to_dict() for cat in categories]
} }
) )
# =============================================================================
# OPENFOODFACTS PROXY HANDLERS
# =============================================================================
@websocket_api.websocket_command(
{
vol.Required("type"): WS_TYPE_OFF_FETCH,
vol.Optional("query"): str,
vol.Optional("barcode"): str,
vol.Optional("page_size", default=5): int,
}
)
@websocket_api.async_response
async def websocket_off_fetch(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Proxy OpenFoodFacts requests through HA to avoid browser CORS restrictions."""
session = async_get_clientsession(hass)
base_url, headers = _get_openfoodfacts_request_config(hass)
try:
if msg.get("barcode"):
barcode = msg["barcode"]
fields = "product_name,categories_tags,image_front_thumb_url,image_front_url,image_url,price"
url = f"{base_url}/api/v2/product/{barcode}.json?fields={fields}"
async with session.get(url, timeout=ClientTimeout(total=10), headers=headers) as resp:
if not resp.ok:
connection.send_result(msg["id"], {"status": 0})
return
data = await resp.json(content_type=None)
connection.send_result(msg["id"], {
"status": data.get("status", 0),
"product": data.get("product"),
})
else:
query = msg.get("query", "")
page_size = msg.get("page_size", 5)
fields = "product_name,categories_tags,image_front_thumb_url,image_front_url,image_url,price"
url = (
f"{base_url}/api/v2/search"
f"?search_terms={query}&fields={fields}&page_size={page_size}"
)
async with session.get(url, timeout=ClientTimeout(total=10), headers=headers) as resp:
if not resp.ok:
connection.send_result(msg["id"], {"products": []})
return
data = await resp.json(content_type=None)
connection.send_result(msg["id"], {"products": data.get("products", [])})
except Exception as err:
_LOGGER.warning("OpenFoodFacts proxy request failed: %s", err)
connection.send_error(msg["id"], "fetch_failed", str(err))
# =============================================================================
# INTEGRATION SETTINGS HANDLERS
# =============================================================================
@websocket_api.websocket_command(
{
vol.Required("type"): "shopping_list_manager/get_integration_settings",
}
)
@callback
def websocket_get_integration_settings(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Return current country and available country options."""
country = hass.data[DOMAIN].get("country", "NZ")
version = hass.data[DOMAIN].get("version", "unknown")
connection.send_result(
msg["id"],
{
"country": country,
"version": version,
"available_countries": {
"NZ": "New Zealand",
"AU": "Australia",
"US": "United States",
"GB": "United Kingdom",
"CA": "Canada",
"BE": "Belgium (Dutch)",
},
}
)
_VALID_COUNTRIES = ["NZ", "AU", "US", "GB", "CA", "BE"]
@websocket_api.websocket_command(
{
vol.Required("type"): "shopping_list_manager/set_country",
vol.Required("country"): vol.In(_VALID_COUNTRIES),
}
)
@websocket_api.async_response
async def websocket_set_country(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Switch to a different country catalog. Preserves user-added products."""
country = msg["country"].upper()
storage = get_storage(hass)
count = await storage.reload_catalog(country)
# Persist to HA config entry so country survives restart
entries = hass.config_entries.async_entries(DOMAIN)
if entries:
entry = entries[0]
hass.config_entries.async_update_entry(entry, options={**entry.options, "country": country})
hass.data[DOMAIN]["country"] = country
connection.send_result(
msg["id"],
{"success": True, "country": country, "products_loaded": count}
)
# =============================================================================
# BACKUP / RESTORE HANDLERS
# =============================================================================
@websocket_api.websocket_command(
{
vol.Required("type"): "shopping_list_manager/export_data",
}
)
@websocket_api.async_response
async def websocket_export_data(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Export all user-created data as a JSON-serialisable dict."""
storage = get_storage(hass)
data = await storage.export_user_data()
connection.send_result(msg["id"], data)
@websocket_api.websocket_command(
{
vol.Required("type"): "shopping_list_manager/import_data",
vol.Required("data"): dict,
}
)
@websocket_api.async_response
async def websocket_import_data(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Import user data from a backup payload."""
storage = get_storage(hass)
counts = await storage.import_user_data(msg["data"])
connection.send_result(msg["id"], {"success": True, "imported": counts})
# =============================================================================
# LIST MEMBERS HANDLER
# =============================================================================
@websocket_api.websocket_command(
{
vol.Required("type"): WS_TYPE_LISTS_UPDATE_MEMBERS,
vol.Required("list_id"): str,
vol.Required("allowed_users"): [str],
}
)
@websocket_api.async_response
async def websocket_update_list_members(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Update the allowed_users for a private list."""
storage = get_storage(hass)
list_id = msg["list_id"]
lst = storage.get_list(list_id)
if lst is None:
connection.send_error(msg["id"], "not_found", "List not found")
return
# Only the owner or an admin may manage members
user = connection.user
if lst.owner_id is not None and not (user and (user.is_admin or user.id == lst.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the list owner can manage members")
return
updated = await storage.update_list_members(list_id, msg["allowed_users"])
hass.bus.async_fire(
EVENT_LIST_UPDATED,
{"list_id": list_id, "action": "members_updated"}
)
connection.send_result(msg["id"], {"list": updated.to_dict()})
# =============================================================================
# HA USERS HANDLER
# =============================================================================
@websocket_api.websocket_command(
{
vol.Required("type"): WS_TYPE_USERS_GET_ALL,
}
)
@websocket_api.async_response
async def websocket_get_ha_users(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Return all active, non-system HA users."""
users = await hass.auth.async_get_users()
result = [
{"id": u.id, "name": u.name}
for u in users
if not u.system_generated and u.is_active
]
connection.send_result(msg["id"], {"users": result})
# =============================================================================
# LOYALTY CARD HANDLERS
# =============================================================================
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_LOYALTY_GET_ALL,
})
@websocket_api.async_response
async def websocket_get_loyalty_cards(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Return all loyalty cards visible to the current user."""
storage = get_storage(hass)
user = connection.user
user_id = user.id if user else None
is_admin = user.is_admin if user else False
cards = storage.get_loyalty_cards(user_id=user_id, is_admin=is_admin)
connection.send_result(msg["id"], {"cards": [c.to_dict() for c in cards]})
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_LOYALTY_ADD,
vol.Required("name"): str,
vol.Required("number"): str,
vol.Optional("barcode", default=""): str,
vol.Optional("barcode_type", default="barcode"): str,
vol.Optional("logo", default=""): str,
vol.Optional("notes", default=""): str,
vol.Optional("color", default="#9fa8da"): str,
vol.Optional("private", default=True): bool,
})
@websocket_api.async_response
async def websocket_add_loyalty_card(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Add a new loyalty card."""
storage = get_storage(hass)
user = connection.user
owner_id = user.id if (user and msg.get("private")) else None
card = await storage.create_loyalty_card(
owner_id=owner_id,
name=msg["name"],
number=msg["number"],
barcode=msg.get("barcode", ""),
barcode_type=msg.get("barcode_type", "barcode"),
logo=msg.get("logo", ""),
notes=msg.get("notes", ""),
color=msg.get("color", "#9fa8da"),
)
connection.send_result(msg["id"], {"card": card.to_dict()})
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_LOYALTY_UPDATE,
vol.Required("card_id"): str,
vol.Optional("name"): str,
vol.Optional("number"): str,
vol.Optional("barcode"): str,
vol.Optional("barcode_type"): str,
vol.Optional("logo"): str,
vol.Optional("notes"): str,
vol.Optional("color"): str,
})
@websocket_api.async_response
async def websocket_update_loyalty_card(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Update an existing loyalty card."""
storage = get_storage(hass)
card_id = msg["card_id"]
card = storage.get_loyalty_card(card_id)
if card is None:
connection.send_error(msg["id"], "not_found", "Loyalty card not found")
return
user = connection.user
if card.owner_id is not None and not (user and (user.is_admin or user.id == card.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the card owner can update it")
return
fields = {k: v for k, v in msg.items() if k not in ("type", "id", "card_id")}
updated = await storage.update_loyalty_card(card_id, **fields)
connection.send_result(msg["id"], {"card": updated.to_dict()})
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_LOYALTY_DELETE,
vol.Required("card_id"): str,
})
@websocket_api.async_response
async def websocket_delete_loyalty_card(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Delete a loyalty card."""
storage = get_storage(hass)
card_id = msg["card_id"]
card = storage.get_loyalty_card(card_id)
if card is None:
connection.send_error(msg["id"], "not_found", "Loyalty card not found")
return
user = connection.user
if card.owner_id is not None and not (user and (user.is_admin or user.id == card.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the card owner can delete it")
return
await storage.delete_loyalty_card(card_id)
connection.send_result(msg["id"], {"success": True})
@websocket_api.websocket_command({
vol.Required("type"): WS_TYPE_LOYALTY_UPDATE_MEMBERS,
vol.Required("card_id"): str,
vol.Required("allowed_users"): [str],
})
@websocket_api.async_response
async def websocket_update_loyalty_card_members(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: Dict[str, Any],
) -> None:
"""Update the allowed_users for a private loyalty card."""
storage = get_storage(hass)
card_id = msg["card_id"]
card = storage.get_loyalty_card(card_id)
if card is None:
connection.send_error(msg["id"], "not_found", "Loyalty card not found")
return
user = connection.user
if card.owner_id is not None and not (user and (user.is_admin or user.id == card.owner_id)):
connection.send_error(msg["id"], "forbidden", "Only the card owner can manage members")
return
updated = await storage.update_loyalty_card_members(card_id, msg["allowed_users"])
connection.send_result(msg["id"], {"card": updated.to_dict()})