ESPHome config flow only connect when needed (#24593)
* ESPHome config flow only connect when needed * Lint
This commit is contained in:
parent
227b8bdf8a
commit
e669e1e2bf
1 changed files with 26 additions and 18 deletions
|
@ -22,7 +22,6 @@ class EsphomeFlowHandler(config_entries.ConfigFlow):
|
||||||
self._host = None # type: Optional[str]
|
self._host = None # type: Optional[str]
|
||||||
self._port = None # type: Optional[int]
|
self._port = None # type: Optional[int]
|
||||||
self._password = None # type: Optional[str]
|
self._password = None # type: Optional[str]
|
||||||
self._name = None # type: Optional[str]
|
|
||||||
|
|
||||||
async def async_step_user(self, user_input: Optional[ConfigType] = None,
|
async def async_step_user(self, user_input: Optional[ConfigType] = None,
|
||||||
error: Optional[str] = None):
|
error: Optional[str] = None):
|
||||||
|
@ -44,34 +43,41 @@ class EsphomeFlowHandler(config_entries.ConfigFlow):
|
||||||
errors=errors
|
errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_authenticate_or_add(self, user_input,
|
@property
|
||||||
from_discovery=False):
|
def _name(self):
|
||||||
|
return self.context.get('name')
|
||||||
|
|
||||||
|
@_name.setter
|
||||||
|
def _name(self, value):
|
||||||
|
# pylint: disable=unsupported-assignment-operation
|
||||||
|
self.context['name'] = value
|
||||||
|
self.context['title_placeholders'] = {
|
||||||
|
'name': self._name
|
||||||
|
}
|
||||||
|
|
||||||
|
def _set_user_input(self, user_input):
|
||||||
|
if user_input is None:
|
||||||
|
return
|
||||||
self._host = user_input['host']
|
self._host = user_input['host']
|
||||||
self._port = user_input['port']
|
self._port = user_input['port']
|
||||||
|
|
||||||
|
async def _async_authenticate_or_add(self, user_input):
|
||||||
|
self._set_user_input(user_input)
|
||||||
error, device_info = await self.fetch_device_info()
|
error, device_info = await self.fetch_device_info()
|
||||||
if error is not None:
|
if error is not None:
|
||||||
return await self.async_step_user(error=error)
|
return await self.async_step_user(error=error)
|
||||||
self._name = device_info.name
|
self._name = device_info.name
|
||||||
# pylint: disable=unsupported-assignment-operation
|
|
||||||
self.context['title_placeholders'] = {
|
|
||||||
'name': self._name
|
|
||||||
}
|
|
||||||
self.context['name'] = self._name
|
|
||||||
|
|
||||||
# Only show authentication step if device uses password
|
# Only show authentication step if device uses password
|
||||||
if device_info.uses_password:
|
if device_info.uses_password:
|
||||||
return await self.async_step_authenticate()
|
return await self.async_step_authenticate()
|
||||||
|
|
||||||
if from_discovery:
|
|
||||||
# If from discovery, do not create entry immediately,
|
|
||||||
# First present user with message
|
|
||||||
return await self.async_step_discovery_confirm()
|
|
||||||
return self._async_get_entry()
|
return self._async_get_entry()
|
||||||
|
|
||||||
async def async_step_discovery_confirm(self, user_input=None):
|
async def async_step_discovery_confirm(self, user_input=None):
|
||||||
"""Handle user-confirmation of discovered node."""
|
"""Handle user-confirmation of discovered node."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
return self._async_get_entry()
|
return await self._async_authenticate_or_add(None)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id='discovery_confirm',
|
step_id='discovery_confirm',
|
||||||
description_placeholders={'name': self._name},
|
description_placeholders={'name': self._name},
|
||||||
|
@ -101,14 +107,16 @@ class EsphomeFlowHandler(config_entries.ConfigFlow):
|
||||||
if already_configured:
|
if already_configured:
|
||||||
return self.async_abort(reason='already_configured')
|
return self.async_abort(reason='already_configured')
|
||||||
|
|
||||||
|
self._host = address
|
||||||
|
self._port = user_input['port']
|
||||||
|
self._name = node_name
|
||||||
|
|
||||||
|
# Check if flow for this device already in progress
|
||||||
for flow in self._async_in_progress():
|
for flow in self._async_in_progress():
|
||||||
if flow['context']['name'] == node_name:
|
if flow['context'].get('name') == node_name:
|
||||||
return self.async_abort(reason='already_configured')
|
return self.async_abort(reason='already_configured')
|
||||||
|
|
||||||
return await self._async_authenticate_or_add(user_input={
|
return await self.async_step_discovery_confirm()
|
||||||
'host': address,
|
|
||||||
'port': user_input['port'],
|
|
||||||
}, from_discovery=True)
|
|
||||||
|
|
||||||
def _async_get_entry(self):
|
def _async_get_entry(self):
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue