Skip to content

Commit

Permalink
source -> context
Browse files Browse the repository at this point in the history
  • Loading branch information
awarecan committed Aug 10, 2018
1 parent 5778867 commit 64c820d
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion homeassistant/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async def _async_create_login_flow(self, handler, *, context, data):

return await auth_provider.async_credential_flow(context)

async def _async_finish_login_flow(self, result):
async def _async_finish_login_flow(self, context, result):
"""Result of a credential login flow."""
if result['type'] != data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
return None
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/config/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get(self, request):

return self.json([
flw for flw in hass.config_entries.flow.async_progress()
if flw['source'] != config_entries.SOURCE_USER])
if flw['context']['source'] != config_entries.SOURCE_USER])


class ConfigManagerFlowResourceView(FlowManagerResourceView):
Expand Down
17 changes: 5 additions & 12 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,26 +372,23 @@ async def async_forward_entry_unload(self, entry, component):
return await entry.async_unload(
self.hass, component=getattr(self.hass.components, component))

async def _async_finish_flow(self, result):
async def _async_finish_flow(self, context, 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
if not any(ent['context']['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

source = result['source']
if source is None:
source = SOURCE_USER
entry = ConfigEntry(
version=result['version'],
domain=result['handler'],
title=result['title'],
data=result['data'],
source=source,
source=context['source'],
)
self._entries.append(entry)
await self._async_schedule_save()
Expand All @@ -406,7 +403,7 @@ async def _async_finish_flow(self, result):
self.hass, entry.domain, self._hass_config)

# Return Entry if they not from a discovery request
if result['source'] not in DISCOVERY_SOURCES:
if context['source'] not in DISCOVERY_SOURCES:
return entry

return entry
Expand All @@ -422,10 +419,7 @@ async def _async_create_flow(self, handler_key, *, context, data):
if handler is None:
raise data_entry_flow.UnknownHandler

if context is not None:
source = context.get('source', SOURCE_USER)
else:
source = SOURCE_USER
source = context['source']

# Make sure requirements and dependencies of component are resolved
await async_process_deps_reqs(
Expand All @@ -442,7 +436,6 @@ async def _async_create_flow(self, handler_key, *, context, data):
)

flow = handler()
flow.source = source
flow.init_step = source
return flow

Expand Down
5 changes: 1 addition & 4 deletions homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def async_progress(self) -> List[Dict]:
return [{
'flow_id': flow.flow_id,
'handler': flow.handler,
'source': flow.source,
'context': flow.context,
} for flow in self._progress.values()]

Expand Down Expand Up @@ -110,7 +109,7 @@ async def _async_handle_step(self, flow: Any, step_id: str,
self._progress.pop(flow.flow_id)

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

if result['type'] == RESULT_TYPE_CREATE_ENTRY:
result['result'] = entry
Expand All @@ -129,7 +128,6 @@ class FlowHandler:

# Set by _async_create_flow callback
init_step = 'init'
source = None

# Set by developer
VERSION = 1
Expand Down Expand Up @@ -159,7 +157,6 @@ def async_create_entry(self, *, title: str, data: Dict) -> Dict:
'handler': self.handler,
'title': title,
'data': data,
'source': self.source,
}

@callback
Expand Down
3 changes: 0 additions & 3 deletions tests/components/config/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def async_step_user(self, user_input=None):
'handler': 'test',
'title': 'Test Entry',
'type': 'create_entry',
'source': 'user',
'version': 1,
}

Expand Down Expand Up @@ -264,7 +263,6 @@ def async_step_account(self, user_input=None):
'type': 'create_entry',
'title': 'user-title',
'version': 1,
'source': 'user',
}


Expand Down Expand Up @@ -295,7 +293,6 @@ def async_step_account(self, user_input=None):
{
'flow_id': form['flow_id'],
'handler': 'test',
'source': 'hassio',
'context': {'source': 'hassio'}
}
]
Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/test_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ async def test_user_init_trumps_discovery(hass, flow_conf):
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM

# User starts flow
result = await hass.config_entries.flow.async_init('test', data={})
result = await hass.config_entries.flow.async_init(
'test', context={'source': config_entries.SOURCE_USER}, data={})
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY

# Discovery flow has been aborted
Expand Down
9 changes: 6 additions & 3 deletions tests/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def async_step_user(self, user_input=None):
})

with patch.dict(config_entries.HANDLERS, {'comp': TestFlow, 'beer': 5}):
yield from manager.flow.async_init('comp')
yield from manager.flow.async_init(
'comp', context={'source': config_entries.SOURCE_USER})
yield from hass.async_block_till_done()

assert len(mock_setup_entry.mock_calls) == 1
Expand Down Expand Up @@ -171,7 +172,8 @@ def async_step_user(self, user_input=None):
)

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

class Test2Flow(data_entry_flow.FlowHandler):
VERSION = 3
Expand All @@ -187,7 +189,8 @@ def async_step_user(self, user_input=None):

with patch('homeassistant.config_entries.HANDLERS.get',
return_value=Test2Flow):
await hass.config_entries.flow.async_init('test')
await hass.config_entries.flow.async_init(
'test', context={'source': config_entries.SOURCE_USER})

# To trigger the call_later
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1))
Expand Down
6 changes: 4 additions & 2 deletions tests/test_data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ async def async_create_flow(handler_name, *, context, data):
if context is not None else 'user_input'
return flow

async def async_add_entry(result):
async def async_add_entry(context, result):
if (result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY):
result['source'] = context.get('source') \
if context is not None else 'user'
entries.append(result)

manager = data_entry_flow.FlowManager(
Expand Down Expand Up @@ -168,7 +170,7 @@ async def async_step_init(self, user_input=None):
assert entry['handler'] == 'test'
assert entry['title'] == 'Test Title'
assert entry['data'] == 'Test Data'
assert entry['source'] == 'user_input'
assert entry['source'] == 'user'


async def test_discovery_init_flow(manager):
Expand Down

0 comments on commit 64c820d

Please sign in to comment.