diff --git a/README.md b/README.md index dfcc432..4dc735a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ First release using config flow - adding some none check ### [1.2.1](https://github.com/thomasgermain/vaillant-component/releases/tag/1.2.1) - warning log fix +### [1.2.2](https://github.com/thomasgermain/vaillant-component/releases/tag/1.2.2) +- Better error handling +- Component does a reconnection every time an error occurs ## Provided entities - 1 water_heater entity, if any water heater: `water_heater.vaillant_`, basically `water_heater.vaillant_control_dhw` diff --git a/vaillant/config_flow.py b/vaillant/config_flow.py index 7aecfe8..54d150c 100644 --- a/vaillant/config_flow.py +++ b/vaillant/config_flow.py @@ -1,7 +1,6 @@ """Config flow for vaillant integration.""" import logging -from pymultimatic.api import ApiError import voluptuous as vol from homeassistant import config_entries, core, exceptions @@ -37,17 +36,8 @@ async def validate_input(hass: core.HomeAssistant, data): async def validate_authentication(hass, username, password, serial): """Ensure provided credentials are working.""" hub: ApiHub = ApiHub(hass, username, password, serial) - try: - if not await hub.authenticate(): - raise InvalidAuth - except ApiError as err: - resp = await err.response.json() - _LOGGER.exception( - "Unable to authenticate, API says: %s, status: %s", - resp, - err.response.status, - ) - raise InvalidAuth from err + if not await hub.authenticate(): + raise InvalidAuth class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): diff --git a/vaillant/hub.py b/vaillant/hub.py index 234f2a0..455480d 100644 --- a/vaillant/hub.py +++ b/vaillant/hub.py @@ -38,7 +38,11 @@ def __init__(self, hass, username, password, serial): async def authenticate(self): """Try to authenticate to the API.""" - return await self._manager.login(True) + try: + return await self._manager.login(True) + except ApiError as err: + await self._handle_api_error(err) + return False async def _hvac_update(self, trigger=None) -> None: """Request is not on the classic update since it won't fetch data. @@ -50,15 +54,8 @@ async def _hvac_update(self, trigger=None) -> None: _LOGGER.debug("Will request_hvac_update") await self._manager.request_hvac_update() except ApiError as err: - resp = await err.response.json() - _LOGGER.warning( - "Unable to fetch data from vaillant API, API says: %s, status: %s", - resp, - err.response.status, - exc_info=True, - ) - if err.response.status == 409: - await self.authenticate() + await self._handle_api_error(err) + await self.authenticate() async def _update_system(self): """Fetch vaillant system.""" @@ -70,14 +67,8 @@ async def _update_system(self): # update_system can is called by all entities, if it fails for # one entity, it will certainly fail for others. # catching exception so the throttling is occurring - resp = await err.response.json() - _LOGGER.exception( - "Unable to fetch data from vaillant API, API says: %s, status: %s", - resp, - err.response.status, - ) - if err.response.status == 409: - await self.authenticate() + await self._handle_api_error(err) + await self.authenticate() async def logout(self): """Logout from API.""" @@ -89,6 +80,14 @@ async def logout(self): return False return True + async def _handle_api_error(self, api_err): + resp = await api_err.response.json() + _LOGGER.exception( + "Unable to fetch data from vaillant, API says: %s, status: %s", + resp, + api_err.response.status, + ) + def find_component(self, comp): """Find a component in the system with the given id, no IO is done.""" from pymultimatic.model import Zone, Room, HotWater, Circulation