From 86852df2fc38544c8df0f3c42462a18db595ea5c Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 5 Oct 2021 19:21:55 +0200 Subject: [PATCH] Bump tuya-iot-py-sdk to 0.5.0 (#57110) Co-authored-by: Martin Hjelmare --- homeassistant/components/tuya/__init__.py | 26 +++++++++++++------- homeassistant/components/tuya/config_flow.py | 16 ++++++------ homeassistant/components/tuya/const.py | 1 + homeassistant/components/tuya/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/tuya/test_config_flow.py | 8 +++--- 7 files changed, 33 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/tuya/__init__.py b/homeassistant/components/tuya/__init__.py index 3602f9585af..df4689268cf 100644 --- a/homeassistant/components/tuya/__init__.py +++ b/homeassistant/components/tuya/__init__.py @@ -4,7 +4,7 @@ import itertools import logging from tuya_iot import ( - ProjectType, + AuthType, TuyaDevice, TuyaDeviceListener, TuyaDeviceManager, @@ -22,6 +22,7 @@ from .const import ( CONF_ACCESS_ID, CONF_ACCESS_SECRET, CONF_APP_TYPE, + CONF_AUTH_TYPE, CONF_COUNTRY_CODE, CONF_ENDPOINT, CONF_PASSWORD, @@ -45,28 +46,35 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Async setup hass config entry.""" hass.data[DOMAIN] = {entry.entry_id: {TUYA_HA_TUYA_MAP: {}, TUYA_HA_DEVICES: set()}} + # Project type has been renamed to auth type in the upstream Tuya IoT SDK. + # This migrates existing config entries to reflect that name change. + if CONF_PROJECT_TYPE in entry.data: + data = {**entry.data, CONF_AUTH_TYPE: entry.data[CONF_PROJECT_TYPE]} + data.pop(CONF_PROJECT_TYPE) + hass.config_entries.async_update_entry(entry, data=data) + success = await _init_tuya_sdk(hass, entry) return bool(success) async def _init_tuya_sdk(hass: HomeAssistant, entry: ConfigEntry) -> bool: - project_type = ProjectType(entry.data[CONF_PROJECT_TYPE]) + auth_type = AuthType(entry.data[CONF_AUTH_TYPE]) api = TuyaOpenAPI( - entry.data[CONF_ENDPOINT], - entry.data[CONF_ACCESS_ID], - entry.data[CONF_ACCESS_SECRET], - project_type, + endpoint=entry.data[CONF_ENDPOINT], + access_id=entry.data[CONF_ACCESS_ID], + access_secret=entry.data[CONF_ACCESS_SECRET], + auth_type=auth_type, ) api.set_dev_channel("hass") - if project_type == ProjectType.INDUSTY_SOLUTIONS: + if auth_type == AuthType.CUSTOM: response = await hass.async_add_executor_job( - api.login, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD] + api.connect, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD] ) else: response = await hass.async_add_executor_job( - api.login, + api.connect, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], entry.data[CONF_COUNTRY_CODE], diff --git a/homeassistant/components/tuya/config_flow.py b/homeassistant/components/tuya/config_flow.py index 1b439d49007..8fffed3cd9f 100644 --- a/homeassistant/components/tuya/config_flow.py +++ b/homeassistant/components/tuya/config_flow.py @@ -4,7 +4,7 @@ from __future__ import annotations import logging from typing import Any -from tuya_iot import ProjectType, TuyaOpenAPI +from tuya_iot import AuthType, TuyaOpenAPI import voluptuous as vol from voluptuous.schema_builder import UNDEFINED @@ -14,10 +14,10 @@ from .const import ( CONF_ACCESS_ID, CONF_ACCESS_SECRET, CONF_APP_TYPE, + CONF_AUTH_TYPE, CONF_COUNTRY_CODE, CONF_ENDPOINT, CONF_PASSWORD, - CONF_PROJECT_TYPE, CONF_REGION, CONF_USERNAME, DOMAIN, @@ -44,7 +44,7 @@ class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): data = { CONF_ENDPOINT: TUYA_REGIONS[user_input[CONF_REGION]], - CONF_PROJECT_TYPE: ProjectType.INDUSTY_SOLUTIONS, + CONF_AUTH_TYPE: AuthType.CUSTOM, CONF_ACCESS_ID: user_input[CONF_ACCESS_ID], CONF_ACCESS_SECRET: user_input[CONF_ACCESS_SECRET], CONF_USERNAME: user_input[CONF_USERNAME], @@ -55,19 +55,19 @@ class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): for app_type in ("", TUYA_SMART_APP, SMARTLIFE_APP): data[CONF_APP_TYPE] = app_type if data[CONF_APP_TYPE] == "": - data[CONF_PROJECT_TYPE] = ProjectType.INDUSTY_SOLUTIONS + data[CONF_AUTH_TYPE] = AuthType.CUSTOM else: - data[CONF_PROJECT_TYPE] = ProjectType.SMART_HOME + data[CONF_AUTH_TYPE] = AuthType.SMART_HOME api = TuyaOpenAPI( endpoint=data[CONF_ENDPOINT], access_id=data[CONF_ACCESS_ID], access_secret=data[CONF_ACCESS_SECRET], - project_type=data[CONF_PROJECT_TYPE], + auth_type=data[CONF_AUTH_TYPE], ) api.set_dev_channel("hass") - response = api.login( + response = api.connect( username=data[CONF_USERNAME], password=data[CONF_PASSWORD], country_code=data[CONF_COUNTRY_CODE], @@ -97,7 +97,7 @@ class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): ): data[CONF_ENDPOINT] = endpoint - data[CONF_PROJECT_TYPE] = data[CONF_PROJECT_TYPE].value + data[CONF_AUTH_TYPE] = data[CONF_AUTH_TYPE].value return self.async_create_entry( title=user_input[CONF_USERNAME], diff --git a/homeassistant/components/tuya/const.py b/homeassistant/components/tuya/const.py index dd18309d128..7c6440d7e48 100644 --- a/homeassistant/components/tuya/const.py +++ b/homeassistant/components/tuya/const.py @@ -2,6 +2,7 @@ DOMAIN = "tuya" +CONF_AUTH_TYPE = "auth_type" CONF_PROJECT_TYPE = "tuya_project_type" CONF_ENDPOINT = "endpoint" CONF_ACCESS_ID = "access_id" diff --git a/homeassistant/components/tuya/manifest.json b/homeassistant/components/tuya/manifest.json index 0097e7635ec..20df33f4573 100644 --- a/homeassistant/components/tuya/manifest.json +++ b/homeassistant/components/tuya/manifest.json @@ -2,7 +2,7 @@ "domain": "tuya", "name": "Tuya", "documentation": "https://github.com/tuya/tuya-home-assistant", - "requirements": ["tuya-iot-py-sdk==0.4.1"], + "requirements": ["tuya-iot-py-sdk==0.5.0"], "codeowners": ["@Tuya", "@zlinoliver", "@METISU"], "config_flow": true, "iot_class": "cloud_push" diff --git a/requirements_all.txt b/requirements_all.txt index 363618c3e99..d15070ddfbb 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2329,7 +2329,7 @@ tp-connected==0.0.4 transmissionrpc==0.11 # homeassistant.components.tuya -tuya-iot-py-sdk==0.4.1 +tuya-iot-py-sdk==0.5.0 # homeassistant.components.twentemilieu twentemilieu==0.3.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ab801c5f083..5c0fb2fc377 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1318,7 +1318,7 @@ total_connect_client==0.57 transmissionrpc==0.11 # homeassistant.components.tuya -tuya-iot-py-sdk==0.4.1 +tuya-iot-py-sdk==0.5.0 # homeassistant.components.twentemilieu twentemilieu==0.3.0 diff --git a/tests/components/tuya/test_config_flow.py b/tests/components/tuya/test_config_flow.py index 04fb8ebe009..745bcfde661 100644 --- a/tests/components/tuya/test_config_flow.py +++ b/tests/components/tuya/test_config_flow.py @@ -11,9 +11,9 @@ from homeassistant.components.tuya.const import ( CONF_ACCESS_ID, CONF_ACCESS_SECRET, CONF_APP_TYPE, + CONF_AUTH_TYPE, CONF_ENDPOINT, CONF_PASSWORD, - CONF_PROJECT_TYPE, CONF_REGION, CONF_USERNAME, DOMAIN, @@ -86,7 +86,7 @@ async def test_user_flow( assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" - tuya().login = MagicMock(side_effect=side_effects) + tuya().connect = MagicMock(side_effect=side_effects) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=TUYA_INPUT_DATA ) @@ -101,7 +101,7 @@ async def test_user_flow( assert result["data"][CONF_ENDPOINT] == MOCK_ENDPOINT assert result["data"][CONF_ENDPOINT] != TUYA_REGIONS[TUYA_INPUT_DATA[CONF_REGION]] assert result["data"][CONF_APP_TYPE] == app_type - assert result["data"][CONF_PROJECT_TYPE] == project_type + assert result["data"][CONF_AUTH_TYPE] == project_type assert not result["result"].unique_id @@ -115,7 +115,7 @@ async def test_error_on_invalid_credentials(hass, tuya): assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" - tuya().login = MagicMock(return_value=RESPONSE_ERROR) + tuya().connect = MagicMock(return_value=RESPONSE_ERROR) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=TUYA_INPUT_DATA )