Skip to content

Commit

Permalink
Save work draft separates api by controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jontofront committed Nov 15, 2024
1 parent ece7413 commit aeba8a5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"cSpell.words": [
"aiohttp",
"econet",
"hass",
"homeassistant",
"MILLIWATT",
"PARAMSUNITSNAMES"
],
}
39 changes: 39 additions & 0 deletions custom_components/econet300/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
API_EDITABLE_PARAMS_LIMITS_URI,
API_REG_PARAMS_DATA_PARAM_DATA,
API_REG_PARAMS_DATA_URI,
API_REG_PARAMS_PARAM_DATA,
API_REG_PARAMS_URI,
API_SYS_PARAMS_PARAM_HW_VER,
API_SYS_PARAMS_PARAM_MODEL_ID,
API_SYS_PARAMS_PARAM_SW_REV,
Expand Down Expand Up @@ -323,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."""
Expand Down
5 changes: 4 additions & 1 deletion custom_components/econet300/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common code for econet300 integration."""

import asyncio
from datetime import timedelta
import logging
Expand Down Expand Up @@ -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 {"sysParams": data, "regParams": reg_params}
except AuthError as err:
raise ConfigEntryAuthFailed from err
except ApiError as err:
Expand Down
12 changes: 7 additions & 5 deletions custom_components/econet300/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def _handle_coordinator_update(self) -> None:
"Update EconetEntity, entity name: %s", self.entity_description.name
)

if self.coordinator.data[self.entity_description.key] is None:
if self.coordinator.data["sysParams"][self.entity_description.key] is None:
return

value = self.coordinator.data[self.entity_description.key]
value = self.self.coordinator.data["sysParams"][self.entity_description.key]

self._sync_state(value)

Expand All @@ -77,18 +77,20 @@ async def async_added_to_hass(self):

if (
not self.coordinator.has_data(self.entity_description.key)
or self.coordinator.data[self.entity_description.key] is None
or self.coordinator.data["sysParams"][self.entity_description.key] is None
):
_LOGGER.warning(
"Data key: %s was expected to exist but it doesn't",
self.entity_description.key,
)
_LOGGER.debug("Coordinator available data: %s", self.coordinator.data)
_LOGGER.debug(
"Coordinator available data: %s", self.coordinator.data["sysParams"]
)

_LOGGER.debug("Exiting async_added_to_hass method")
return

value = self.coordinator.data[self.entity_description.key]
value = self.coordinator.data["sysParams"][self.entity_description.key]

await super().async_added_to_hass()
self._sync_state(value)
Expand Down
8 changes: 5 additions & 3 deletions custom_components/econet300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def create_entity_description(key: str) -> EconetSensorEntityDescription:
def create_controller_sensors(coordinator: EconetDataCoordinator, api: Econet300Api):
"""Create controller sensor entities."""
entities: list[EconetSensor] = []
coordinator_data = coordinator.data
coordinator_data = coordinator.data["sysParams"]
for data_key in SENSOR_MAP:
if data_key in coordinator_data:
entities.append(
Expand All @@ -127,9 +127,11 @@ def create_controller_sensors(coordinator: EconetDataCoordinator, api: Econet300
def can_add_mixer(key: str, coordinator: EconetDataCoordinator):
"""Check if a mixer can be added."""
_LOGGER.debug(
"Checking if mixer can be added for key: %s, data %s", key, coordinator.data
"Checking if mixer can be added for key: %s, data %s",
key,
coordinator.data["sysParams"],
)
return coordinator.has_data(key) and coordinator.data[key] is not None
return coordinator.has_data(key) and coordinator.data["sysParams"][key] is not None


def create_mixer_sensor_entity_description(
Expand Down

0 comments on commit aeba8a5

Please sign in to comment.