mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add confirmation to Cast/Sonos/iOS config entries (#16769)
* Add confirmation to Cast/Sonos/iOS config entries * Remove redundant code
This commit is contained in:
parent
213171769d
commit
8b42d0c471
@ -31,40 +31,39 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow):
|
|||||||
reason='single_instance_allowed'
|
reason='single_instance_allowed'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get current discovered entries.
|
return await self.async_step_confirm()
|
||||||
in_progress = self._async_in_progress()
|
|
||||||
|
|
||||||
has_devices = in_progress
|
async def async_step_confirm(self, user_input=None):
|
||||||
if not has_devices:
|
"""Confirm setup."""
|
||||||
has_devices = await self.hass.async_add_job(
|
if user_input is None:
|
||||||
self._discovery_function, self.hass)
|
return self.async_show_form(
|
||||||
|
step_id='confirm',
|
||||||
if not has_devices:
|
|
||||||
return self.async_abort(
|
|
||||||
reason='no_devices_found'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Cancel the discovered one.
|
if self.context and self.context.get('source') != \
|
||||||
for flow in in_progress:
|
config_entries.SOURCE_DISCOVERY:
|
||||||
self.hass.config_entries.flow.async_abort(flow['flow_id'])
|
# Get current discovered entries.
|
||||||
|
in_progress = self._async_in_progress()
|
||||||
|
|
||||||
|
has_devices = in_progress
|
||||||
|
if not has_devices:
|
||||||
|
has_devices = await self.hass.async_add_job(
|
||||||
|
self._discovery_function, self.hass)
|
||||||
|
|
||||||
|
if not has_devices:
|
||||||
|
return self.async_abort(
|
||||||
|
reason='no_devices_found'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Cancel the discovered one.
|
||||||
|
for flow in in_progress:
|
||||||
|
self.hass.config_entries.flow.async_abort(flow['flow_id'])
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=self._title,
|
title=self._title,
|
||||||
data={},
|
data={},
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_confirm(self, user_input=None):
|
|
||||||
"""Confirm setup."""
|
|
||||||
if user_input is not None:
|
|
||||||
return self.async_create_entry(
|
|
||||||
title=self._title,
|
|
||||||
data={},
|
|
||||||
)
|
|
||||||
|
|
||||||
return self.async_show_form(
|
|
||||||
step_id='confirm',
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_step_discovery(self, discovery_info):
|
async def async_step_discovery(self, discovery_info):
|
||||||
"""Handle a flow initialized by discovery."""
|
"""Handle a flow initialized by discovery."""
|
||||||
if self._async_in_progress() or self._async_current_entries():
|
if self._async_in_progress() or self._async_current_entries():
|
||||||
|
@ -17,6 +17,12 @@ async def test_creating_entry_sets_up_media_player(hass):
|
|||||||
return_value=True):
|
return_value=True):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
cast.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
cast.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
||||||
|
|
||||||
|
# Confirmation form
|
||||||
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result['flow_id'], {})
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -30,6 +30,12 @@ async def test_creating_entry_sets_up_sensor(hass):
|
|||||||
return_value=mock_coro(True)) as mock_setup:
|
return_value=mock_coro(True)) as mock_setup:
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
ios.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
ios.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
||||||
|
|
||||||
|
# Confirmation form
|
||||||
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result['flow_id'], {})
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -15,6 +15,12 @@ async def test_creating_entry_sets_up_media_player(hass):
|
|||||||
patch('pysonos.discover', return_value=True):
|
patch('pysonos.discover', return_value=True):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
sonos.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
sonos.DOMAIN, context={'source': config_entries.SOURCE_USER})
|
||||||
|
|
||||||
|
# Confirmation form
|
||||||
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result['flow_id'], {})
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -42,22 +42,24 @@ async def test_user_no_devices_found(hass, flow_conf):
|
|||||||
"""Test if no devices found."""
|
"""Test if no devices found."""
|
||||||
flow = config_entries.HANDLERS['test']()
|
flow = config_entries.HANDLERS['test']()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {
|
||||||
result = await flow.async_step_user()
|
'source': config_entries.SOURCE_USER
|
||||||
|
}
|
||||||
|
result = await flow.async_step_confirm(user_input={})
|
||||||
|
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result['reason'] == 'no_devices_found'
|
assert result['reason'] == 'no_devices_found'
|
||||||
|
|
||||||
|
|
||||||
async def test_user_no_confirmation(hass, flow_conf):
|
async def test_user_has_confirmation(hass, flow_conf):
|
||||||
"""Test user requires no confirmation to set up."""
|
"""Test user requires no confirmation to setup."""
|
||||||
flow = config_entries.HANDLERS['test']()
|
flow = config_entries.HANDLERS['test']()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
flow_conf['discovered'] = True
|
flow_conf['discovered'] = True
|
||||||
|
|
||||||
result = await flow.async_step_user()
|
result = await flow.async_step_user()
|
||||||
|
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
|
||||||
async def test_discovery_single_instance(hass, flow_conf):
|
async def test_discovery_single_instance(hass, flow_conf):
|
||||||
@ -100,7 +102,7 @@ async def test_multiple_discoveries(hass, flow_conf):
|
|||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
assert result['type'] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
|
||||||
|
|
||||||
async def test_user_init_trumps_discovery(hass, flow_conf):
|
async def test_only_one_in_progress(hass, flow_conf):
|
||||||
"""Test a user initialized one will finish and cancel discovered one."""
|
"""Test a user initialized one will finish and cancel discovered one."""
|
||||||
loader.set_component(hass, 'test', MockModule('test'))
|
loader.set_component(hass, 'test', MockModule('test'))
|
||||||
|
|
||||||
@ -112,9 +114,16 @@ async def test_user_init_trumps_discovery(hass, flow_conf):
|
|||||||
# User starts flow
|
# User starts flow
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
'test', context={'source': config_entries.SOURCE_USER}, data={})
|
'test', context={'source': config_entries.SOURCE_USER}, data={})
|
||||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
||||||
|
|
||||||
# Discovery flow has been aborted
|
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
|
# Discovery flow has not been aborted
|
||||||
|
assert len(hass.config_entries.flow.async_progress()) == 2
|
||||||
|
|
||||||
|
# Discovery should be aborted once user confirms
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result['flow_id'], {})
|
||||||
|
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert len(hass.config_entries.flow.async_progress()) == 0
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user