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