Update category_loader.py

This commit is contained in:
thekiwismarthome
2026-02-14 07:04:22 +13:00
committed by GitHub
parent c7310d4213
commit 61a7678e24
@@ -3,12 +3,13 @@ import json
import logging import logging
import os import os
from typing import List, Dict, Any from typing import List, Dict, Any
import aiofiles
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def load_categories(component_path: str, country_code: str = None) -> List[Dict[str, Any]]: async def load_categories(component_path: str, country_code: str = None) -> List[Dict[str, Any]]:
"""Load categories from JSON file. """Load categories from JSON file asynchronously.
Args: Args:
component_path: Path to the component directory component_path: Path to the component directory
@@ -18,6 +19,8 @@ def load_categories(component_path: str, country_code: str = None) -> List[Dict[
Returns: Returns:
List of category dictionaries List of category dictionaries
""" """
import os
# Try country-specific file first if country_code provided # Try country-specific file first if country_code provided
if country_code: if country_code:
country_file = os.path.join( country_file = os.path.join(
@@ -36,8 +39,9 @@ def load_categories(component_path: str, country_code: str = None) -> List[Dict[
categories_file = os.path.join(component_path, "data", "categories.json") categories_file = os.path.join(component_path, "data", "categories.json")
try: try:
with open(categories_file, "r", encoding="utf-8") as f: async with aiofiles.open(categories_file, "r", encoding="utf-8") as f:
data = json.load(f) content = await f.read()
data = json.loads(content)
_LOGGER.info( _LOGGER.info(
"Loaded categories version %s for region %s", "Loaded categories version %s for region %s",