diff --git a/custom_components/econet300/api.py b/custom_components/econet300/api.py index f3eb772..573b4f2 100644 --- a/custom_components/econet300/api.py +++ b/custom_components/econet300/api.py @@ -289,24 +289,6 @@ async def get_param_limits(self, param: str): curr_limits = limits[param] return Limits(curr_limits["min"], curr_limits["max"]) - async def fetch_reg_params(self) -> dict[str, Any]: - """Fetch and return the regParam data from ip/econet/regParams endpoint.""" - _LOGGER.info("Calling fetch_reg_params method") - regParams = await self._client.get_params(API_REG_PARAMS_URI) - _LOGGER.debug("Fetched regParams data: %s", regParams) - - if API_REG_PARAMS_PARAM_DATA in regParams: - _LOGGER.info( - "Response contains expected keys. API_REG_PARAMS_PARAM_DATA: %s", - regParams[API_REG_PARAMS_PARAM_DATA], - ) - else: - _LOGGER.warning( - "Response does not contain expected keys. API_REG_PARAMS_PARAM_DATA is missing." - ) - - return regParams - async def fetch_data(self) -> dict[str, Any]: """Fetch data from regParamsData.""" regParamsData = await self._fetch_reg_key( @@ -343,6 +325,43 @@ async def _fetch_reg_key(self, reg, data_key: str | None = None): return data[data_key] + async def fetch_reg_params(self) -> dict[str, Any]: + """Fetch and return the regParam data from ip/econet/regParams endpoint.""" + _LOGGER.info("Calling fetch_reg_params method") + regParams = await self._fetch_reg_names( + API_REG_PARAMS_URI, API_REG_PARAMS_PARAM_DATA + ) + _LOGGER.debug("Fetched regParams data: %s", regParams) + + if API_REG_PARAMS_PARAM_DATA in regParams: + _LOGGER.info( + "Response contains expected keys. API_REG_PARAMS_PARAM_DATA: %s", + regParams[API_REG_PARAMS_PARAM_DATA], + ) + else: + _LOGGER.warning( + "Response does not contain expected keys. API_REG_PARAMS_PARAM_DATA is missing." + ) + _LOGGER.debug("Full response data: %s", regParams) + + return regParams + + async def _fetch_reg_names(self, reg, data_key_key_name: str | None = None): + """Fetch a key from the json-encoded data returned by the API for a given registry If key is None, then return whole data.""" + data = await self._client.get_params(reg) + + if data is None: + raise DataError(f"Data fetched by API for reg: {reg} is None") + + if data_key_key_name is None: + return data + + if data_key_key_name not in data: + _LOGGER.debug(data) + raise DataError(f"Data for key: {data_key_key_name} does not exist") + + return data[data_key_key_name] + async def make_api(hass: HomeAssistant, cache: MemCache, data: dict): """Create api object.""" diff --git a/custom_components/econet300/common.py b/custom_components/econet300/common.py index 039b539..f347914 100644 --- a/custom_components/econet300/common.py +++ b/custom_components/econet300/common.py @@ -1,4 +1,5 @@ """Common code for econet300 integration.""" + import asyncio from datetime import timedelta import logging @@ -43,7 +44,9 @@ async def _async_update_data(self): # Note: asyncio.TimeoutError and aiohttp.ClientError are already # handled by the data update coordinator. async with asyncio.timeout(10): - return await self._api.fetch_data() + data = await self._api.fetch_data() + reg_params = await self._api.fetch_reg_params() + return data, reg_params except AuthError as err: raise ConfigEntryAuthFailed from err except ApiError as err: