Skip to content

Commit

Permalink
Fix hue discovery popping up (#14614)
Browse files Browse the repository at this point in the history
* Fix hue discovery popping up

* Fix result

* Fix tests
  • Loading branch information
balloob committed May 24, 2018
1 parent 4fb4838 commit fa9b910
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions homeassistant/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ async def _async_create_login_flow(self, handler, *, source, data):

async def _async_finish_login_flow(self, result):
"""Result of a credential login flow."""
if result['type'] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
return None

auth_provider = self._providers[result['handler']]
return await auth_provider.async_get_or_create_credentials(
result['data'])
Expand Down
15 changes: 9 additions & 6 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ async def async_forward_entry_unload(self, entry, component):

async def _async_finish_flow(self, result):
"""Finish a config flow and add an entry."""
# If no discovery config entries in progress, remove notification.
if not any(ent['source'] in DISCOVERY_SOURCES for ent
in self.hass.config_entries.flow.async_progress()):
self.hass.components.persistent_notification.async_dismiss(
DISCOVERY_NOTIFICATION_ID)

if result['type'] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
return None

entry = ConfigEntry(
version=result['version'],
domain=result['handler'],
Expand All @@ -370,12 +379,6 @@ async def _async_finish_flow(self, result):
if result['source'] not in DISCOVERY_SOURCES:
return entry

# If no discovery config entries in progress, remove notification.
if not any(ent['source'] in DISCOVERY_SOURCES for ent
in self.hass.config_entries.flow.async_progress()):
self.hass.components.persistent_notification.async_dismiss(
DISCOVERY_NOTIFICATION_ID)

return entry

async def _async_create_flow(self, handler, *, source, data):
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ async def _async_handle_step(self, flow, step_id, user_input):
# Abort and Success results both finish the flow
self._progress.pop(flow.flow_id)

if result['type'] == RESULT_TYPE_ABORT:
return result

# We pass a copy of the result because we're mutating our version
result['result'] = await self._async_finish_flow(dict(result))
entry = await self._async_finish_flow(dict(result))

if result['type'] == RESULT_TYPE_CREATE_ENTRY:
result['result'] = entry
return result


Expand Down
20 changes: 20 additions & 0 deletions tests/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,23 @@ async def async_step_discovery(self, user_input=None):
await hass.async_block_till_done()
state = hass.states.get('persistent_notification.config_entry_discovery')
assert state is None


async def test_discovery_notification_not_created(hass):
"""Test that we not create a notification when discovery is aborted."""
loader.set_component(hass, 'test', MockModule('test'))
await async_setup_component(hass, 'persistent_notification', {})

class TestFlow(data_entry_flow.FlowHandler):
VERSION = 5

async def async_step_discovery(self, user_input=None):
return self.async_abort(reason='test')

with patch.dict(config_entries.HANDLERS, {'test': TestFlow}):
await hass.config_entries.flow.async_init(
'test', source=data_entry_flow.SOURCE_DISCOVERY)

await hass.async_block_till_done()
state = hass.states.get('persistent_notification.config_entry_discovery')
assert state is None
3 changes: 2 additions & 1 deletion tests/test_data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ async def async_create_flow(handler_name, *, source, data):
return handler()

async def async_add_entry(result):
entries.append(result)
if (result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY):
entries.append(result)

manager = data_entry_flow.FlowManager(
None, async_create_flow, async_add_entry)
Expand Down

0 comments on commit fa9b910

Please sign in to comment.