From 1be9c7e069bf380536154ebb70d8c6a7171484ff Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Tue, 27 Oct 2020 09:43:17 +0100 Subject: [PATCH] Improve handling of errors when Somfy server is in maintenance. (#306) Co-authored-by: Thibaut Etienne --- custom_components/tahoma/__init__.py | 9 ++++++++- custom_components/tahoma/config_flow.py | 11 ++++++++++- custom_components/tahoma/coordinator.py | 3 +++ custom_components/tahoma/manifest.json | 11 ++++++++--- custom_components/tahoma/strings.json | 1 + custom_components/tahoma/translations/en.json | 1 + custom_components/tahoma/translations/fr.json | 1 + custom_components/tahoma/translations/nl.json | 1 + requirements.test.txt | 2 +- 9 files changed, 34 insertions(+), 6 deletions(-) diff --git a/custom_components/tahoma/__init__.py b/custom_components/tahoma/__init__.py index 3ff041dc6..424f023fe 100644 --- a/custom_components/tahoma/__init__.py +++ b/custom_components/tahoma/__init__.py @@ -12,7 +12,11 @@ from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import aiohttp_client, config_validation as cv from pyhoma.client import TahomaClient -from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException +from pyhoma.exceptions import ( + BadCredentialsException, + MaintenanceException, + TooManyRequestsException, +) from pyhoma.models import Command import voluptuous as vol @@ -95,6 +99,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): except (TimeoutError, ClientError, ServerDisconnectedError) as exception: _LOGGER.error("cannot_connect") raise ConfigEntryNotReady from exception + except MaintenanceException as exception: + _LOGGER.error("server_in_maintenance") + raise ConfigEntryNotReady from exception except Exception as exception: # pylint: disable=broad-except _LOGGER.exception(exception) return False diff --git a/custom_components/tahoma/config_flow.py b/custom_components/tahoma/config_flow.py index 06993cec4..299e5ee68 100644 --- a/custom_components/tahoma/config_flow.py +++ b/custom_components/tahoma/config_flow.py @@ -7,7 +7,11 @@ from homeassistant.core import callback from homeassistant.helpers import config_validation as cv from pyhoma.client import TahomaClient -from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException +from pyhoma.exceptions import ( + BadCredentialsException, + MaintenanceException, + TooManyRequestsException, +) import voluptuous as vol from .const import CONF_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL, MIN_UPDATE_INTERVAL @@ -57,6 +61,8 @@ async def async_step_user(self, user_input=None): errors["base"] = "invalid_auth" except (TimeoutError, ClientError): errors["base"] = "cannot_connect" + except MaintenanceException: + errors["base"] = "server_in_maintenance" except Exception as exception: # pylint: disable=broad-except errors["base"] = "unknown" _LOGGER.exception(exception) @@ -81,6 +87,9 @@ async def async_step_import(self, import_config: dict): except (TimeoutError, ClientError): _LOGGER.error("cannot_connect") return self.async_abort(reason="cannot_connect") + except MaintenanceException: + _LOGGER.error("server_in_maintenance") + return self.async_abort(reason="server_in_maintenance") except Exception as exception: # pylint: disable=broad-except _LOGGER.exception(exception) return self.async_abort(reason="unknown") diff --git a/custom_components/tahoma/coordinator.py b/custom_components/tahoma/coordinator.py index b49a7154d..decb4956d 100644 --- a/custom_components/tahoma/coordinator.py +++ b/custom_components/tahoma/coordinator.py @@ -11,6 +11,7 @@ from pyhoma.enums import EventName, ExecutionState from pyhoma.exceptions import ( BadCredentialsException, + MaintenanceException, NotAuthenticatedException, TooManyRequestsException, ) @@ -67,6 +68,8 @@ async def _async_update_data(self) -> Dict[str, Device]: raise UpdateFailed("invalid_auth") from exception except TooManyRequestsException as exception: raise UpdateFailed("too_many_requests") from exception + except MaintenanceException as exception: + raise UpdateFailed("server_in_maintenance") from exception except (ServerDisconnectedError, NotAuthenticatedException) as exception: _LOGGER.debug(exception) self.executions = {} diff --git a/custom_components/tahoma/manifest.json b/custom_components/tahoma/manifest.json index 5c282ba71..4a7b662a3 100644 --- a/custom_components/tahoma/manifest.json +++ b/custom_components/tahoma/manifest.json @@ -4,8 +4,13 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/tahoma", "requirements": [ - "pyhoma==0.5.0" + "pyhoma==0.5.1" + ], + "codeowners": [ + "@philklei", + "@imicknl", + "@vlebourl", + "@tetienne" ], - "codeowners": ["@philklei", "@imicknl", "@vlebourl", "@tetienne"], "issue_tracker": "https://github.com/imicknl/ha-tahoma/issues" -} +} \ No newline at end of file diff --git a/custom_components/tahoma/strings.json b/custom_components/tahoma/strings.json index 66cd52871..c51e129f7 100644 --- a/custom_components/tahoma/strings.json +++ b/custom_components/tahoma/strings.json @@ -13,6 +13,7 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "too_many_requests": "Too many requests, try again later.", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "server_in_maintenance": "Server is down for maintenance", "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { diff --git a/custom_components/tahoma/translations/en.json b/custom_components/tahoma/translations/en.json index 6012b64ad..93d0b3cf6 100644 --- a/custom_components/tahoma/translations/en.json +++ b/custom_components/tahoma/translations/en.json @@ -13,6 +13,7 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "too_many_requests": "Too many requests, try again later.", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "server_in_maintenance": "Server is down for maintenance", "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { diff --git a/custom_components/tahoma/translations/fr.json b/custom_components/tahoma/translations/fr.json index a48409582..6c62b8711 100644 --- a/custom_components/tahoma/translations/fr.json +++ b/custom_components/tahoma/translations/fr.json @@ -13,6 +13,7 @@ "cannot_connect": "Connexion impossible", "too_many_requests": "Trop de reqûtees, veuillez réessayer plus tard.", "invalid_auth": "Mot de passe ou nom d'utilisateur incorrect", + "server_in_maintenance": "Le serveur est en cours de maintenance", "unknown": "Une erreur inconnue est survenue." }, "abort": { diff --git a/custom_components/tahoma/translations/nl.json b/custom_components/tahoma/translations/nl.json index c9921ecbf..57e678d79 100644 --- a/custom_components/tahoma/translations/nl.json +++ b/custom_components/tahoma/translations/nl.json @@ -13,6 +13,7 @@ "too_many_requests": "Te veel verzoeken, probeer het later opnieuw.", "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "server_in_maintenance": "De server is offline voor onderhoud", "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { diff --git a/requirements.test.txt b/requirements.test.txt index 2c93f89bc..98546f949 100644 --- a/requirements.test.txt +++ b/requirements.test.txt @@ -7,4 +7,4 @@ pytest-cov<3.0.0 pytest-homeassistant # from our manifest.json for our Custom Component -pyhoma==0.5.0 \ No newline at end of file +pyhoma==0.5.1 \ No newline at end of file