diff --git a/custom_components/shopping_list_manager/data/catalog_loader.py b/custom_components/shopping_list_manager/data/catalog_loader.py index 2f168aa..a79719d 100644 --- a/custom_components/shopping_list_manager/data/catalog_loader.py +++ b/custom_components/shopping_list_manager/data/catalog_loader.py @@ -1,15 +1,14 @@ - """Product catalog loader for Shopping List Manager.""" import json import logging -import os from typing import List, Dict, Any +import aiofiles _LOGGER = logging.getLogger(__name__) -def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[Dict[str, Any]]: - """Load product catalog from JSON file. +async def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[Dict[str, Any]]: + """Load product catalog from JSON file asynchronously. Args: component_path: Path to the component directory @@ -18,26 +17,28 @@ def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[ Returns: List of product dictionaries """ + import os + # Try country-specific catalog first if country_code: - country_file = os.path.join( + catalog_file = os.path.join( component_path, "data", f"products_catalog_{country_code.lower()}.json" ) - if os.path.exists(country_file): - catalog_file = country_file - _LOGGER.debug("Using country-specific product catalog: %s", country_code) - else: + if not os.path.exists(catalog_file): _LOGGER.warning( - "No country-specific catalog found for %s", - country_code + "No country-specific catalog found for %s at %s", + country_code, + catalog_file ) return [] else: return [] try: - with open(catalog_file, "r", encoding="utf-8") as f: - data = json.load(f) + # Use aiofiles for async file reading + async with aiofiles.open(catalog_file, "r", encoding="utf-8") as f: + content = await f.read() + data = json.loads(content) _LOGGER.info( "Loaded product catalog version %s for region %s",