-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
/
config_flow.py
197 lines (154 loc) · 6.09 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Config flow for Minut Point."""
import asyncio
from collections import OrderedDict
import logging
import async_timeout
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.http import HomeAssistantView
from homeassistant.core import callback
DOMAIN = 'point'
CLIENT_ID = 'client_id'
CLIENT_SECRET = 'client_secret'
KEY_IMPORT_GROUPS = 'import_groups'
AUTH_CALLBACK_PATH = '/api/minut'
AUTH_CALLBACK_NAME = 'api:minut'
DATA_FLOW_IMPL = 'point_flow_implementation'
_LOGGER = logging.getLogger(__name__)
@callback
def register_flow_implementation(hass, domain, name, client_id, client_secret):
"""Register a flow implementation.
domain: Domain of the component responsible for the implementation.
name: Name of the component.
client_id: Client id.
client_secret: Client secret.
"""
if DATA_FLOW_IMPL not in hass.data:
hass.data[DATA_FLOW_IMPL] = OrderedDict()
hass.data[DATA_FLOW_IMPL][domain] = {
'domain': domain,
'name': name,
CLIENT_ID: client_id,
CLIENT_SECRET: client_secret,
}
@config_entries.HANDLERS.register('point')
class PointFlowHandler(config_entries.ConfigFlow):
"""Handle a config flow."""
VERSION = 1
CONNETION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
def __init__(self):
"""Initialize flow."""
self.flow_impl = None
async def async_step_import(self, user_input=None):
"""Handle external yaml configuration."""
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='already_setup')
self.flow_impl = DOMAIN
return await self.async_step_auth()
async def async_step_user(self, user_input=None):
"""Handle a flow start."""
flows = self.hass.data.get(DATA_FLOW_IMPL, {})
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='already_setup')
if not flows:
_LOGGER.debug("no flows")
return self.async_abort(reason='no_flows')
if len(flows) == 1:
self.flow_impl = list(flows)[0]
return await self.async_step_auth()
if user_input is not None:
self.flow_impl = user_input['flow_impl']
return await self.async_step_auth()
return self.async_show_form(
step_id='user',
data_schema=vol.Schema({
vol.Required('flow_impl'):
vol.In(list(flows))
}))
async def async_step_auth(self, user_input=None):
"""Create an entry for auth."""
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='external_setup')
errors = {}
if user_input is not None:
errors['base'] = 'follow_link'
try:
with async_timeout.timeout(10):
url = await self._get_authorization_url()
except asyncio.TimeoutError:
return self.async_abort(reason='authorize_url_timeout')
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected error generating auth url")
return self.async_abort(reason='authorize_url_fail')
return self.async_show_form(
step_id='auth',
description_placeholders={'authorization_url': url},
errors=errors,
)
async def _get_authorization_url(self):
"""Create Minut Point session and get authorization url."""
from pypoint import PointSession
flow = self.hass.data[DATA_FLOW_IMPL][self.flow_impl]
client_id = flow[CLIENT_ID]
client_secret = flow[CLIENT_SECRET]
point_session = PointSession(
client_id, client_secret=client_secret)
self.hass.http.register_view(MinutAuthCallbackView())
return point_session.get_authorization_url
async def async_step_code(self, code=None):
"""Received code for authentication."""
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason='already_setup')
if code is None:
return self.async_abort(reason='no_code')
_LOGGER.debug("Should close all flows below %s",
self.hass.config_entries.flow.async_progress())
# Remove notification if no other discovery config entries in progress
return await self._async_create_session(code)
async def _async_create_session(self, code):
"""Create point session and entries."""
from pypoint import PointSession
flow = self.hass.data[DATA_FLOW_IMPL][DOMAIN]
client_id = flow[CLIENT_ID]
client_secret = flow[CLIENT_SECRET]
point_session = PointSession(
client_id,
client_secret=client_secret,
)
token = await self.hass.async_add_executor_job(
point_session.get_access_token, code)
_LOGGER.debug("Got new token")
if not point_session.is_authorized:
_LOGGER.error('Authentication Error')
return self.async_abort(reason='auth_error')
_LOGGER.info('Successfully authenticated Point')
user_email = point_session.user().get('email') or ""
return self.async_create_entry(
title=user_email,
data={
'token': token,
'refresh_args': {
'client_id': client_id,
'client_secret': client_secret
}
},
)
class MinutAuthCallbackView(HomeAssistantView):
"""Minut Authorization Callback View."""
requires_auth = False
url = AUTH_CALLBACK_PATH
name = AUTH_CALLBACK_NAME
def __init__(self):
"""Initialize."""
@staticmethod
async def get(request):
"""Receive authorization code."""
hass = request.app['hass']
if 'code' in request.query:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={'source': 'code'},
data=request.query['code'],
))
return u"OK!"