-
Notifications
You must be signed in to change notification settings - Fork 90
/
config_flow.py
179 lines (160 loc) · 6.13 KB
/
config_flow.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import logging
import voluptuous as vol
import uuid
import requests
from urllib.parse import parse_qs, urlparse
import traceback
from homeassistant import config_entries
from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
CONF_UNIT_OF_MEASUREMENT,
CONF_UNIT_SYSTEM,
CONF_REGION,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from .utils import get_default_distance_unit
from .const import (
DISTANCE_UNITS,
CONF_SCAN_INTERVAL,
DEFAULT_SCAN_INTERVAL,
CONF_PIN,
DEFAULT_PIN,
CONF_FORCE_SCAN_INTERVAL,
DEFAULT_FORCE_SCAN_INTERVAL,
CONF_NO_FORCE_SCAN_HOUR_START,
DEFAULT_NO_FORCE_SCAN_HOUR_START,
CONF_NO_FORCE_SCAN_HOUR_FINISH,
DEFAULT_NO_FORCE_SCAN_HOUR_FINISH,
CONF_ENABLE_GEOLOCATION_ENTITY,
DEFAULT_ENABLE_GEOLOCATION_ENTITY,
CONF_USE_EMAIL_WITH_GEOCODE_API,
DEFAULT_USE_EMAIL_WITH_GEOCODE_API,
CONF_BRAND,
CONF_STORED_CREDENTIALS,
DOMAIN,
CONFIG_FLOW_VERSION,
REGIONS,
DEFAULT_REGION,
BRANDS,
DEFAULT_BRAND,
)
from .KiaUvoApiImpl import KiaUvoApiImpl
from .Token import Token
from .utils import get_implementation_by_region_brand
_LOGGER = logging.getLogger(__name__)
class KiaUvoOptionFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry):
self.config_entry = config_entry
self.schema = vol.Schema(
{
vol.Optional(
CONF_UNIT_OF_MEASUREMENT,
default=self.config_entry.options.get(
CONF_UNIT_OF_MEASUREMENT, get_default_distance_unit()
),
): vol.In(DISTANCE_UNITS),
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): vol.All(vol.Coerce(int), vol.Range(min=1, max=999)),
vol.Optional(
CONF_FORCE_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_FORCE_SCAN_INTERVAL, DEFAULT_FORCE_SCAN_INTERVAL
),
): vol.All(vol.Coerce(int), vol.Range(min=1, max=999)),
vol.Optional(
CONF_NO_FORCE_SCAN_HOUR_START,
default=self.config_entry.options.get(
CONF_NO_FORCE_SCAN_HOUR_START, DEFAULT_NO_FORCE_SCAN_HOUR_START
),
): vol.All(vol.Coerce(int), vol.Range(min=1, max=23)),
vol.Optional(
CONF_NO_FORCE_SCAN_HOUR_FINISH,
default=self.config_entry.options.get(
CONF_NO_FORCE_SCAN_HOUR_FINISH,
DEFAULT_NO_FORCE_SCAN_HOUR_FINISH,
),
): vol.All(vol.Coerce(int), vol.Range(min=1, max=23)),
vol.Optional(
CONF_ENABLE_GEOLOCATION_ENTITY,
default=self.config_entry.options.get(
CONF_ENABLE_GEOLOCATION_ENTITY,
DEFAULT_ENABLE_GEOLOCATION_ENTITY,
),
): bool,
vol.Optional(
CONF_USE_EMAIL_WITH_GEOCODE_API,
default=self.config_entry.options.get(
CONF_USE_EMAIL_WITH_GEOCODE_API,
DEFAULT_USE_EMAIL_WITH_GEOCODE_API,
),
): bool,
}
)
async def async_step_init(self, user_input=None):
if user_input is not None:
_LOGGER.debug(f"{DOMAIN} user input in option flow : %s", user_input)
return self.async_create_entry(title="", data=user_input)
return self.async_show_form(step_id="init", data_schema=self.schema)
class KiaUvoConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = CONFIG_FLOW_VERSION
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
@staticmethod
@callback
def async_get_options_flow(config_entry):
return KiaUvoOptionFlowHandler(config_entry)
def __init__(self):
self.schema = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Optional(CONF_PIN): str,
vol.Optional(CONF_REGION, default=DEFAULT_REGION): vol.In(REGIONS),
vol.Optional(CONF_BRAND, default=DEFAULT_BRAND): vol.In(BRANDS),
}
)
self.kia_uvo_api = None
self.token = None
async def async_step_user(self, user_input=None):
await self.async_set_unique_id(DOMAIN)
self._abort_if_unique_id_configured()
errors = None
if user_input is not None:
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
region = user_input[CONF_REGION]
pin = user_input.get(CONF_PIN, "")
brand = user_input[CONF_BRAND]
self.kia_uvo_api: KiaUvoApiImpl = get_implementation_by_region_brand(
region, brand, username, password
)
try:
self.token = await self.hass.async_add_executor_job(
self.kia_uvo_api.login
)
return self.async_create_entry(
title=username,
data={
CONF_USERNAME: username,
CONF_PASSWORD: password,
CONF_REGION: region,
CONF_PIN: pin,
CONF_BRAND: brand,
CONF_STORED_CREDENTIALS: vars(self.token),
},
)
except Exception as ex:
_LOGGER.error(
f"{DOMAIN} Exception in kia_uvo login : %s - traceback: %s",
ex,
traceback.format_exc(),
)
errors = {"base": "auth"}
return self.async_show_form(
step_id="user", data_schema=self.schema, errors=errors
)