Repository navigation
Expand file tree
/
Copy pathconfig_flow.py
More file actions
145 lines (123 loc) · 4.54 KB
/
Copy pathconfig_flow.py
File metadata and controls
145 lines (123 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Config flow for Tuya."""
from __future__ import annotations
from typing import Any
from tuya_iot import AuthType, TuyaOpenAPI
import voluptuous as vol
from homeassistant import config_entries
from .const import (
CONF_ACCESS_ID,
CONF_ACCESS_SECRET,
CONF_APP_TYPE,
CONF_AUTH_TYPE,
CONF_COUNTRY_CODE,
CONF_ENDPOINT,
CONF_PASSWORD,
CONF_USERNAME,
DOMAIN,
LOGGER,
SMARTLIFE_APP,
TUYA_COUNTRIES,
TUYA_RESPONSE_CODE,
TUYA_RESPONSE_MSG,
TUYA_RESPONSE_PLATFORM_URL,
TUYA_RESPONSE_RESULT,
TUYA_RESPONSE_SUCCESS,
TUYA_SMART_APP,
)
class TuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Tuya Config Flow."""
@staticmethod
def _try_login(user_input: dict[str, Any]) -> tuple[dict[Any, Any], dict[str, Any]]:
"""Try login."""
response = {}
country = [
country
for country in TUYA_COUNTRIES
if country.name == user_input[CONF_COUNTRY_CODE]
][0]
data = {
CONF_ENDPOINT: country.endpoint,
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],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_COUNTRY_CODE: country.country_code,
}
for app_type in ("", TUYA_SMART_APP, SMARTLIFE_APP):
data[CONF_APP_TYPE] = app_type
if data[CONF_APP_TYPE] == "":
data[CONF_AUTH_TYPE] = AuthType.CUSTOM
else:
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],
auth_type=data[CONF_AUTH_TYPE],
)
api.set_dev_channel("hass")
response = api.connect(
username=data[CONF_USERNAME],
password=data[CONF_PASSWORD],
country_code=data[CONF_COUNTRY_CODE],
schema=data[CONF_APP_TYPE],
)
LOGGER.debug("Response %s", response)
if response.get(TUYA_RESPONSE_SUCCESS, False):
break
return response, data
async def async_step_user(self, user_input=None):
"""Step user."""
errors = {}
placeholders = {}
if user_input is not None:
response, data = await self.hass.async_add_executor_job(
self._try_login, user_input
)
if response.get(TUYA_RESPONSE_SUCCESS, False):
if endpoint := response.get(TUYA_RESPONSE_RESULT, {}).get(
TUYA_RESPONSE_PLATFORM_URL
):
data[CONF_ENDPOINT] = endpoint
data[CONF_AUTH_TYPE] = data[CONF_AUTH_TYPE].value
return self.async_create_entry(
title=user_input[CONF_USERNAME],
data=data,
)
errors["base"] = "login_error"
placeholders = {
TUYA_RESPONSE_CODE: response.get(TUYA_RESPONSE_CODE),
TUYA_RESPONSE_MSG: response.get(TUYA_RESPONSE_MSG),
}
if user_input is None:
user_input = {}
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
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]
),
vol.Required(
CONF_ACCESS_ID, default=user_input.get(CONF_ACCESS_ID, "")
): str,
vol.Required(
CONF_ACCESS_SECRET,
default=user_input.get(CONF_ACCESS_SECRET, ""),
): str,
vol.Required(
CONF_USERNAME, default=user_input.get(CONF_USERNAME, "")
): str,
vol.Required(
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "")
): str,
}
),
errors=errors,
description_placeholders=placeholders,
)