2020-05-11 03:01:00 +00:00
|
|
|
"""Config flow for Tuya."""
|
2021-10-04 15:27:24 +00:00
|
|
|
from __future__ import annotations
|
2021-08-23 19:30:04 +00:00
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
from typing import Any
|
2020-05-11 03:01:00 +00:00
|
|
|
|
2021-10-05 17:21:55 +00:00
|
|
|
from tuya_iot import AuthType, TuyaOpenAPI
|
2020-05-11 03:01:00 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
|
2020-10-28 01:26:18 +00:00
|
|
|
from .const import (
|
2021-09-30 10:02:56 +00:00
|
|
|
CONF_ACCESS_ID,
|
|
|
|
CONF_ACCESS_SECRET,
|
|
|
|
CONF_APP_TYPE,
|
2021-10-05 17:21:55 +00:00
|
|
|
CONF_AUTH_TYPE,
|
2021-09-30 10:02:56 +00:00
|
|
|
CONF_COUNTRY_CODE,
|
|
|
|
CONF_ENDPOINT,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
2021-03-02 08:02:04 +00:00
|
|
|
DOMAIN,
|
2021-12-29 14:12:27 +00:00
|
|
|
LOGGER,
|
2021-10-04 15:27:24 +00:00
|
|
|
SMARTLIFE_APP,
|
2021-10-05 20:32:48 +00:00
|
|
|
TUYA_COUNTRIES,
|
2021-10-04 15:27:24 +00:00
|
|
|
TUYA_RESPONSE_CODE,
|
|
|
|
TUYA_RESPONSE_MSG,
|
2021-10-14 22:46:06 +00:00
|
|
|
TUYA_RESPONSE_PLATFORM_URL,
|
2021-10-04 15:27:24 +00:00
|
|
|
TUYA_RESPONSE_RESULT,
|
|
|
|
TUYA_RESPONSE_SUCCESS,
|
|
|
|
TUYA_SMART_APP,
|
2020-10-28 01:26:18 +00:00
|
|
|
)
|
2020-05-11 03:01:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2021-09-30 10:02:56 +00:00
|
|
|
"""Tuya Config Flow."""
|
2020-05-11 03:01:00 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
@staticmethod
|
2021-10-04 15:27:24 +00:00
|
|
|
def _try_login(user_input: dict[str, Any]) -> tuple[dict[Any, Any], dict[str, Any]]:
|
|
|
|
"""Try login."""
|
|
|
|
response = {}
|
|
|
|
|
2021-10-05 20:32:48 +00:00
|
|
|
country = [
|
|
|
|
country
|
|
|
|
for country in TUYA_COUNTRIES
|
|
|
|
if country.name == user_input[CONF_COUNTRY_CODE]
|
|
|
|
][0]
|
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
data = {
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_ENDPOINT: country.endpoint,
|
2021-10-05 17:21:55 +00:00
|
|
|
CONF_AUTH_TYPE: AuthType.CUSTOM,
|
2021-10-04 15:27:24 +00:00
|
|
|
CONF_ACCESS_ID: user_input[CONF_ACCESS_ID],
|
|
|
|
CONF_ACCESS_SECRET: user_input[CONF_ACCESS_SECRET],
|
|
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
|
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_COUNTRY_CODE: country.country_code,
|
2021-10-04 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for app_type in ("", TUYA_SMART_APP, SMARTLIFE_APP):
|
|
|
|
data[CONF_APP_TYPE] = app_type
|
|
|
|
if data[CONF_APP_TYPE] == "":
|
2021-10-05 17:21:55 +00:00
|
|
|
data[CONF_AUTH_TYPE] = AuthType.CUSTOM
|
2021-10-04 08:45:37 +00:00
|
|
|
else:
|
2021-10-05 17:21:55 +00:00
|
|
|
data[CONF_AUTH_TYPE] = AuthType.SMART_HOME
|
2021-10-04 08:45:37 +00:00
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
api = TuyaOpenAPI(
|
|
|
|
endpoint=data[CONF_ENDPOINT],
|
|
|
|
access_id=data[CONF_ACCESS_ID],
|
|
|
|
access_secret=data[CONF_ACCESS_SECRET],
|
2021-10-05 17:21:55 +00:00
|
|
|
auth_type=data[CONF_AUTH_TYPE],
|
2020-05-11 03:01:00 +00:00
|
|
|
)
|
2021-10-04 15:27:24 +00:00
|
|
|
api.set_dev_channel("hass")
|
2020-05-11 03:01:00 +00:00
|
|
|
|
2021-10-05 17:21:55 +00:00
|
|
|
response = api.connect(
|
2021-10-04 15:27:24 +00:00
|
|
|
username=data[CONF_USERNAME],
|
|
|
|
password=data[CONF_PASSWORD],
|
|
|
|
country_code=data[CONF_COUNTRY_CODE],
|
|
|
|
schema=data[CONF_APP_TYPE],
|
2020-10-28 01:26:18 +00:00
|
|
|
)
|
|
|
|
|
2021-12-29 14:12:27 +00:00
|
|
|
LOGGER.debug("Response %s", response)
|
2020-10-28 01:26:18 +00:00
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
if response.get(TUYA_RESPONSE_SUCCESS, False):
|
|
|
|
break
|
2020-12-08 10:32:48 +00:00
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
return response, data
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Step user."""
|
2021-09-30 10:02:56 +00:00
|
|
|
errors = {}
|
2021-10-04 15:27:24 +00:00
|
|
|
placeholders = {}
|
2020-10-28 01:26:18 +00:00
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
if user_input is not None:
|
|
|
|
response, data = await self.hass.async_add_executor_job(
|
2021-09-30 10:02:56 +00:00
|
|
|
self._try_login, user_input
|
2020-10-28 01:26:18 +00:00
|
|
|
)
|
|
|
|
|
2021-10-04 15:27:24 +00:00
|
|
|
if response.get(TUYA_RESPONSE_SUCCESS, False):
|
|
|
|
if endpoint := response.get(TUYA_RESPONSE_RESULT, {}).get(
|
2021-10-14 22:46:06 +00:00
|
|
|
TUYA_RESPONSE_PLATFORM_URL
|
2021-10-04 15:27:24 +00:00
|
|
|
):
|
|
|
|
data[CONF_ENDPOINT] = endpoint
|
|
|
|
|
2021-10-05 17:21:55 +00:00
|
|
|
data[CONF_AUTH_TYPE] = data[CONF_AUTH_TYPE].value
|
2021-10-04 15:27:24 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_USERNAME],
|
2021-10-04 15:27:24 +00:00
|
|
|
data=data,
|
2021-09-30 10:02:56 +00:00
|
|
|
)
|
2021-10-04 15:27:24 +00:00
|
|
|
errors["base"] = "login_error"
|
|
|
|
placeholders = {
|
|
|
|
TUYA_RESPONSE_CODE: response.get(TUYA_RESPONSE_CODE),
|
|
|
|
TUYA_RESPONSE_MSG: response.get(TUYA_RESPONSE_MSG),
|
|
|
|
}
|
2021-09-30 10:02:56 +00:00
|
|
|
|
2021-10-05 20:32:48 +00:00
|
|
|
if user_input is None:
|
|
|
|
user_input = {}
|
2020-10-28 01:26:18 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2021-10-04 15:27:24 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_COUNTRY_CODE,
|
|
|
|
default=user_input.get(CONF_COUNTRY_CODE, "United States"),
|
|
|
|
): vol.In(
|
|
|
|
# We don't pass a dict {code:name} because country codes can be duplicate.
|
|
|
|
[country.name for country in TUYA_COUNTRIES]
|
|
|
|
),
|
2021-10-04 15:27:24 +00:00
|
|
|
vol.Required(
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_ACCESS_ID, default=user_input.get(CONF_ACCESS_ID, "")
|
2021-10-04 15:27:24 +00:00
|
|
|
): str,
|
|
|
|
vol.Required(
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_ACCESS_SECRET,
|
|
|
|
default=user_input.get(CONF_ACCESS_SECRET, ""),
|
2021-10-04 15:27:24 +00:00
|
|
|
): str,
|
|
|
|
vol.Required(
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_USERNAME, default=user_input.get(CONF_USERNAME, "")
|
2021-10-04 15:27:24 +00:00
|
|
|
): str,
|
|
|
|
vol.Required(
|
2021-10-05 20:32:48 +00:00
|
|
|
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "")
|
2021-10-04 15:27:24 +00:00
|
|
|
): str,
|
|
|
|
}
|
|
|
|
),
|
2021-09-30 10:02:56 +00:00
|
|
|
errors=errors,
|
2021-10-04 15:27:24 +00:00
|
|
|
description_placeholders=placeholders,
|
2020-10-28 01:26:18 +00:00
|
|
|
)
|