From fca8841e34808d698d2667072a076ca1f58f8811 Mon Sep 17 00:00:00 2001 From: Alex Szlavik Date: Tue, 8 Dec 2020 05:32:48 -0500 Subject: [PATCH] Retry tuya setup on auth rate limiting (#44001) Co-authored-by: Martin Hjelmare --- homeassistant/components/tuya/__init__.py | 5 +++++ homeassistant/components/tuya/config_flow.py | 14 ++++++++++++-- homeassistant/components/tuya/manifest.json | 2 +- homeassistant/components/tuya/strings.json | 3 +++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/tuya/__init__.py b/homeassistant/components/tuya/__init__.py index bc665baeb86..5876331ea97 100644 --- a/homeassistant/components/tuya/__init__.py +++ b/homeassistant/components/tuya/__init__.py @@ -6,6 +6,7 @@ import logging from tuyaha import TuyaApi from tuyaha.tuyaapi import ( TuyaAPIException, + TuyaAPIRateLimitException, TuyaFrequentlyInvokeException, TuyaNetException, TuyaServerException, @@ -137,6 +138,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): ) as exc: raise ConfigEntryNotReady() from exc + except TuyaAPIRateLimitException as exc: + _LOGGER.error("Tuya login rate limited") + raise ConfigEntryNotReady() from exc + except TuyaAPIException as exc: _LOGGER.error( "Connection error during integration setup. Error: %s", diff --git a/homeassistant/components/tuya/config_flow.py b/homeassistant/components/tuya/config_flow.py index e2048aaf7bf..5d22a83e03e 100644 --- a/homeassistant/components/tuya/config_flow.py +++ b/homeassistant/components/tuya/config_flow.py @@ -2,7 +2,12 @@ import logging from tuyaha import TuyaApi -from tuyaha.tuyaapi import TuyaAPIException, TuyaNetException, TuyaServerException +from tuyaha.tuyaapi import ( + TuyaAPIException, + TuyaAPIRateLimitException, + TuyaNetException, + TuyaServerException, +) import voluptuous as vol from homeassistant import config_entries @@ -103,7 +108,7 @@ class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): tuya.init( self._username, self._password, self._country_code, self._platform ) - except (TuyaNetException, TuyaServerException): + except (TuyaAPIRateLimitException, TuyaNetException, TuyaServerException): return RESULT_CONN_ERROR except TuyaAPIException: return RESULT_AUTH_FAILED @@ -249,6 +254,11 @@ class OptionsFlowHandler(config_entries.OptionsFlow): async def async_step_init(self, user_input=None): """Handle options flow.""" + + if self.config_entry.state != config_entries.ENTRY_STATE_LOADED: + _LOGGER.error("Tuya integration not yet loaded") + return self.async_abort(reason="cannot_connect") + if user_input is not None: dev_ids = user_input.get(CONF_LIST_DEVICES) if dev_ids: diff --git a/homeassistant/components/tuya/manifest.json b/homeassistant/components/tuya/manifest.json index 642a4dbe5d1..7481e56f00a 100644 --- a/homeassistant/components/tuya/manifest.json +++ b/homeassistant/components/tuya/manifest.json @@ -2,7 +2,7 @@ "domain": "tuya", "name": "Tuya", "documentation": "https://www.home-assistant.io/integrations/tuya", - "requirements": ["tuyaha==0.0.8"], + "requirements": ["tuyaha==0.0.9"], "codeowners": ["@ollo69"], "config_flow": true } diff --git a/homeassistant/components/tuya/strings.json b/homeassistant/components/tuya/strings.json index 84575906010..444ff0b5c21 100644 --- a/homeassistant/components/tuya/strings.json +++ b/homeassistant/components/tuya/strings.json @@ -23,6 +23,9 @@ } }, "options": { + "abort": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" + }, "step": { "init": { "title": "Configure Tuya Options", diff --git a/requirements_all.txt b/requirements_all.txt index bd7e3474308..e9daee8f38f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2208,7 +2208,7 @@ tp-connected==0.0.4 transmissionrpc==0.11 # homeassistant.components.tuya -tuyaha==0.0.8 +tuyaha==0.0.9 # homeassistant.components.twentemilieu twentemilieu==0.3.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b15a2d7afce..dc4210dd8ff 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1073,7 +1073,7 @@ total_connect_client==0.55 transmissionrpc==0.11 # homeassistant.components.tuya -tuyaha==0.0.8 +tuyaha==0.0.9 # homeassistant.components.twentemilieu twentemilieu==0.3.0