diff --git a/homeassistant/components/wolflink/__init__.py b/homeassistant/components/wolflink/__init__.py index 7286f28568d..ab078e438c6 100644 --- a/homeassistant/components/wolflink/__init__.py +++ b/homeassistant/components/wolflink/__init__.py @@ -4,7 +4,7 @@ import logging from httpx import ConnectError, ConnectTimeout from wolf_smartset.token_auth import InvalidAuth -from wolf_smartset.wolf_client import FetchFailed, WolfClient +from wolf_smartset.wolf_client import FetchFailed, ParameterReadError, WolfClient from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME @@ -33,6 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: device_name = entry.data[DEVICE_NAME] device_id = entry.data[DEVICE_ID] gateway_id = entry.data[DEVICE_GATEWAY] + refetch_parameters = False _LOGGER.debug( "Setting up wolflink integration for device: %s (ID: %s, gateway: %s)", device_name, @@ -42,17 +43,37 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: wolf_client = WolfClient(username, password) - try: - parameters = await fetch_parameters(wolf_client, gateway_id, device_id) - except InvalidAuth: - _LOGGER.debug("Authentication failed") - return False + parameters = await fetch_parameters_init(wolf_client, gateway_id, device_id) async def async_update_data(): """Update all stored entities for Wolf SmartSet.""" try: - values = await wolf_client.fetch_value(gateway_id, device_id, parameters) - return {v.value_id: v.value for v in values} + nonlocal refetch_parameters + nonlocal parameters + await wolf_client.update_session() + if not wolf_client.fetch_system_state_list(device_id, gateway_id): + refetch_parameters = True + raise UpdateFailed( + "Could not fetch values from server because device is Offline." + ) + if refetch_parameters: + parameters = await fetch_parameters(wolf_client, gateway_id, device_id) + hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters + refetch_parameters = False + values = { + v.value_id: v.value + for v in await wolf_client.fetch_value( + gateway_id, device_id, parameters + ) + } + return { + parameter.parameter_id: ( + parameter.value_id, + values[parameter.value_id], + ) + for parameter in parameters + if parameter.value_id in values + } except ConnectError as exception: raise UpdateFailed( f"Error communicating with API: {exception}" @@ -61,13 +82,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: raise UpdateFailed( f"Could not fetch values from server due to: {exception}" ) from exception + except ParameterReadError as exception: + refetch_parameters = True + raise UpdateFailed( + "Could not fetch values for parameter. Refreshing value IDs." + ) from exception except InvalidAuth as exception: raise UpdateFailed("Invalid authentication during update.") from exception coordinator = DataUpdateCoordinator( hass, _LOGGER, - name="wolflink", + name=DOMAIN, update_method=async_update_data, update_interval=timedelta(minutes=1), ) @@ -100,9 +126,14 @@ async def fetch_parameters(client: WolfClient, gateway_id: int, device_id: int): By default Reglertyp entity is removed because API will not provide value for this parameter. """ + fetched_parameters = await client.fetch_parameters(gateway_id, device_id) + return [param for param in fetched_parameters if param.name != "Reglertyp"] + + +async def fetch_parameters_init(client: WolfClient, gateway_id: int, device_id: int): + """Fetch all available parameters with usage of WolfClient but handles all exceptions and results in ConfigEntryNotReady.""" try: - fetched_parameters = await client.fetch_parameters(gateway_id, device_id) - return [param for param in fetched_parameters if param.name != "Reglertyp"] + return await fetch_parameters(client, gateway_id, device_id) except (ConnectError, ConnectTimeout, FetchFailed) as exception: raise ConfigEntryNotReady( f"Error communicating with API: {exception}" diff --git a/homeassistant/components/wolflink/manifest.json b/homeassistant/components/wolflink/manifest.json index 504419ef0f4..749f7bbc67c 100644 --- a/homeassistant/components/wolflink/manifest.json +++ b/homeassistant/components/wolflink/manifest.json @@ -3,7 +3,7 @@ "name": "Wolf SmartSet Service", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/wolflink", - "requirements": ["wolf_smartset==0.1.8"], + "requirements": ["wolf_smartset==0.1.11"], "codeowners": ["@adamkrol93"], "iot_class": "cloud_polling" } diff --git a/homeassistant/components/wolflink/sensor.py b/homeassistant/components/wolflink/sensor.py index f243160ff59..0d35d4bce5c 100644 --- a/homeassistant/components/wolflink/sensor.py +++ b/homeassistant/components/wolflink/sensor.py @@ -65,8 +65,10 @@ class WolfLinkSensor(CoordinatorEntity, SensorEntity): @property def state(self): """Return the state. Wolf Client is returning only changed values so we need to store old value here.""" - if self.wolf_object.value_id in self.coordinator.data: - self._state = self.coordinator.data[self.wolf_object.value_id] + if self.wolf_object.parameter_id in self.coordinator.data: + new_state = self.coordinator.data[self.wolf_object.parameter_id] + self.wolf_object.value_id = new_state[0] + self._state = new_state[1] return self._state @property diff --git a/requirements_all.txt b/requirements_all.txt index 75937cd3439..3e398156dc7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2368,7 +2368,7 @@ withings-api==2.3.2 wled==0.6.0 # homeassistant.components.wolflink -wolf_smartset==0.1.8 +wolf_smartset==0.1.11 # homeassistant.components.xbee xbee-helper==0.0.7 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 47bd108f6ca..7e982626fe5 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1286,7 +1286,7 @@ withings-api==2.3.2 wled==0.6.0 # homeassistant.components.wolflink -wolf_smartset==0.1.8 +wolf_smartset==0.1.11 # homeassistant.components.xbox xbox-webapi==2.0.11