From 15569f1e7fc2fa79de88f8c933973e1bd818f517 Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Wed, 29 Apr 2020 13:46:27 +0200 Subject: [PATCH] Fix tuya network failure startup (#34057) * Tuya initialization retry on failure * Rename exception * Changed managed exception * Manage different cases of ConnectionError * Log messages correction * Test always catching exceptions * Update for Lint * Update tuya library to 0.0.6 - Catch new library exception * Tuya initialization retry on failure * Rename exception * Changed managed exception * Manage different cases of ConnectionError * Log messages correction * Test always catching exceptions * Update for Lint * Update tuya library to 0.0.6 - Catch new library exception * Catch wrong credential * Revert "Catch wrong credential" This reverts commit 7fb797de5254bf2fa7811793b066174d9d261428. * Catch wrong credential * Remove trailing whitespace * Black formatting * Import Exception from tuyaapi * Remove str to exception * Force CI checks * Force CI checks * Rebase conflict * Tuya initialization retry on failure * Rename exception * Changed managed exception * Manage different cases of ConnectionError * Log messages correction * Test always catching exceptions * Update for Lint * Update tuya library to 0.0.6 - Catch new library exception * Catch wrong credential * Revert "Catch wrong credential" This reverts commit 7fb797de5254bf2fa7811793b066174d9d261428. * Tuya initialization retry on failure * Rename exception * Changed managed exception * Catch wrong credential * Force CI checks * Force CI checks * Rebase conflict * Rebase * Force Test * Force Test --- homeassistant/components/tuya/__init__.py | 34 +++++++++++++++++++-- homeassistant/components/tuya/manifest.json | 2 +- requirements_all.txt | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/tuya/__init__.py b/homeassistant/components/tuya/__init__.py index 74b1d1439ad..c50e6787d89 100644 --- a/homeassistant/components/tuya/__init__.py +++ b/homeassistant/components/tuya/__init__.py @@ -3,6 +3,7 @@ from datetime import timedelta import logging from tuyaha import TuyaApi +from tuyaha.tuyaapi import TuyaAPIException, TuyaNetException, TuyaServerException import voluptuous as vol from homeassistant.const import CONF_PASSWORD, CONF_PLATFORM, CONF_USERNAME @@ -11,7 +12,7 @@ from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity -from homeassistant.helpers.event import track_time_interval +from homeassistant.helpers.event import call_later, track_time_interval _LOGGER = logging.getLogger(__name__) @@ -22,6 +23,9 @@ PARALLEL_UPDATES = 0 DOMAIN = "tuya" DATA_TUYA = "data_tuya" +FIRST_RETRY_TIME = 60 +MAX_RETRY_TIME = 900 + SIGNAL_DELETE_ENTITY = "tuya_delete" SIGNAL_UPDATE_ENTITY = "tuya_update" @@ -52,17 +56,41 @@ CONFIG_SCHEMA = vol.Schema( ) -def setup(hass, config): +def setup(hass, config, retry_delay=FIRST_RETRY_TIME): """Set up Tuya Component.""" + _LOGGER.debug("Setting up integration") + tuya = TuyaApi() username = config[DOMAIN][CONF_USERNAME] password = config[DOMAIN][CONF_PASSWORD] country_code = config[DOMAIN][CONF_COUNTRYCODE] platform = config[DOMAIN][CONF_PLATFORM] + try: + tuya.init(username, password, country_code, platform) + except (TuyaNetException, TuyaServerException): + + _LOGGER.warning( + "Connection error during integration setup. Will retry in %s seconds", + retry_delay, + ) + + def retry_setup(now): + """Retry setup if a error happens on tuya API.""" + setup(hass, config, retry_delay=min(2 * retry_delay, MAX_RETRY_TIME)) + + call_later(hass, retry_delay, retry_setup) + + return True + + except TuyaAPIException as exc: + _LOGGER.error( + "Connection error during integration setup. Error: %s", exc, + ) + return False + hass.data[DATA_TUYA] = tuya - tuya.init(username, password, country_code, platform) hass.data[DOMAIN] = {"entities": {}} def load_devices(device_list): diff --git a/homeassistant/components/tuya/manifest.json b/homeassistant/components/tuya/manifest.json index cd6cb333020..1279f0a2f66 100644 --- a/homeassistant/components/tuya/manifest.json +++ b/homeassistant/components/tuya/manifest.json @@ -2,6 +2,6 @@ "domain": "tuya", "name": "Tuya", "documentation": "https://www.home-assistant.io/integrations/tuya", - "requirements": ["tuyaha==0.0.5"], + "requirements": ["tuyaha==0.0.6"], "codeowners": [] } diff --git a/requirements_all.txt b/requirements_all.txt index 4bc53309239..c8c136d1021 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2062,7 +2062,7 @@ tp-connected==0.0.4 transmissionrpc==0.11 # homeassistant.components.tuya -tuyaha==0.0.5 +tuyaha==0.0.6 # homeassistant.components.twentemilieu twentemilieu==0.3.0