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 commit7fb797de52
. * 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 commit7fb797de52
. * 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
This commit is contained in:
parent
2788de9b10
commit
15569f1e7f
3 changed files with 33 additions and 5 deletions
|
@ -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):
|
||||
|
|
|
@ -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": []
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue