|
| 1 | +"""Fetches vendor info from the CSA.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import logging |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from aiohttp import ClientError, ClientSession |
| 8 | + |
| 9 | +from ..common.helpers.api import api_command |
| 10 | +from ..common.helpers.util import dataclass_from_dict, dataclass_to_dict |
| 11 | +from ..common.models import APICommand, VendorInfo as VendorInfoModel |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from .server import MatterServer |
| 15 | + |
| 16 | +LOGGER = logging.getLogger(__name__) |
| 17 | +PRODUCTION_URL = "https://on.dcl.csa-iot.org" |
| 18 | +DATA_KEY_VENDOR_INFO = "vendor_info" |
| 19 | + |
| 20 | + |
| 21 | +class VendorInfo: |
| 22 | + """Fetches vendor info from the CSA and handles api calls to get it.""" |
| 23 | + |
| 24 | + def __init__(self, server: MatterServer): |
| 25 | + """Initialize the vendor info.""" |
| 26 | + self._data: dict[int, VendorInfoModel] = {} |
| 27 | + self._server = server |
| 28 | + |
| 29 | + async def start(self) -> None: |
| 30 | + """Async initialize the vendor info.""" |
| 31 | + self._load_vendors() |
| 32 | + await self._fetch_vendors() |
| 33 | + self._save_vendors() |
| 34 | + |
| 35 | + def _load_vendors(self) -> None: |
| 36 | + """Load vendor info from storage.""" |
| 37 | + LOGGER.info("Loading vendor info from storage.") |
| 38 | + vendor_count = 0 |
| 39 | + data = self._server.storage.get(DATA_KEY_VENDOR_INFO, {}) |
| 40 | + for vendor_id, vendor_info in data.items(): |
| 41 | + self._data[vendor_id] = dataclass_from_dict(VendorInfoModel, vendor_info) |
| 42 | + vendor_count += 1 |
| 43 | + LOGGER.info("Loaded %s vendors from storage.", vendor_count) |
| 44 | + |
| 45 | + async def _fetch_vendors(self) -> None: |
| 46 | + """Fetch the vendor names from the CSA.""" |
| 47 | + LOGGER.info("Fetching the latest vendor info from DCL.") |
| 48 | + vendors: dict[int, VendorInfoModel] = {} |
| 49 | + try: |
| 50 | + async with ClientSession(raise_for_status=True) as session: |
| 51 | + async with session.get( |
| 52 | + f"{PRODUCTION_URL}/dcl/vendorinfo/vendors" |
| 53 | + ) as response: |
| 54 | + data = await response.json() |
| 55 | + for vendorinfo in data["vendorInfo"]: |
| 56 | + vendors[vendorinfo["vendorID"]] = VendorInfoModel( |
| 57 | + vendor_id=vendorinfo["vendorID"], |
| 58 | + vendor_name=vendorinfo["vendorName"], |
| 59 | + company_legal_name=vendorinfo["companyLegalName"], |
| 60 | + company_preferred_name=vendorinfo["companyPreferredName"], |
| 61 | + vendor_landing_page_url=vendorinfo["vendorLandingPageURL"], |
| 62 | + creator=vendorinfo["creator"], |
| 63 | + ) |
| 64 | + except ClientError as err: |
| 65 | + LOGGER.error("Unable to fetch vendor info from DCL: %s", err) |
| 66 | + else: |
| 67 | + LOGGER.info("Fetched %s vendors from DCL.", len(vendors)) |
| 68 | + |
| 69 | + self._data.update(vendors) |
| 70 | + |
| 71 | + def _save_vendors(self) -> None: |
| 72 | + """Save vendor info to storage.""" |
| 73 | + LOGGER.info("Saving vendor info to storage.") |
| 74 | + self._server.storage.set( |
| 75 | + DATA_KEY_VENDOR_INFO, |
| 76 | + { |
| 77 | + vendor_id: dataclass_to_dict(vendor_info) |
| 78 | + for vendor_id, vendor_info in self._data.items() |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + @api_command(APICommand.GET_VENDOR_NAMES) |
| 83 | + async def get_vendor_names( |
| 84 | + self, filter_vendors: list[int] | None = None |
| 85 | + ) -> dict[int, str]: |
| 86 | + """Get a map of vendor ids to vendor names.""" |
| 87 | + if filter_vendors: |
| 88 | + vendors: dict[int, str] = {} |
| 89 | + for vendor_id in filter_vendors: |
| 90 | + if vendor_id in filter_vendors: |
| 91 | + vendors[vendor_id] = self._data[vendor_id].vendor_name |
| 92 | + return vendors |
| 93 | + |
| 94 | + return { |
| 95 | + vendor_id: vendor_info.vendor_name |
| 96 | + for vendor_id, vendor_info in self._data.items() |
| 97 | + } |
0 commit comments