Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homeassistant/components/cast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Component to embed Google Cast."""
from homeassistant import data_entry_flow
from homeassistant import config_entries
from homeassistant.helpers import config_entry_flow


Expand All @@ -15,7 +15,7 @@ async def async_setup(hass, config):

if conf is not None:
hass.async_create_task(hass.config_entries.flow.async_init(
DOMAIN, source=data_entry_flow.SOURCE_IMPORT))
DOMAIN, source=config_entries.SOURCE_IMPORT))

return True

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'] != data_entry_flow.SOURCE_USER])
if flw['source'] != config_entries.SOURCE_USER])


class ConfigManagerFlowResourceView(FlowManagerResourceView):
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import (
CONF_API_KEY, CONF_EVENT, CONF_HOST,
CONF_ID, CONF_PORT, EVENT_HOMEASSISTANT_STOP)
Expand Down Expand Up @@ -60,7 +61,9 @@ async def async_setup(hass, config):
deconz_config = config[DOMAIN]
if deconz_config and not configured_hosts(hass):
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data=deconz_config
DOMAIN,
source=config_entries.SOURCE_IMPORT,
data=deconz_config
))
return True

Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def __init__(self):
self.bridges = []
self.deconz_config = {}

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a deCONZ config flow start.

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import voluptuous as vol

from homeassistant import data_entry_flow
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.const import EVENT_HOMEASSISTANT_START
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -138,7 +138,7 @@ async def new_service_found(service, info):
if service in CONFIG_ENTRY_HANDLERS:
await hass.config_entries.flow.async_init(
CONFIG_ENTRY_HANDLERS[service],
source=data_entry_flow.SOURCE_DISCOVERY,
source=config_entries.SOURCE_DISCOVERY,
data=info
)
return
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/homematicip_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries

from .const import (
DOMAIN, HMIPC_HAPID, HMIPC_AUTHTOKEN, HMIPC_NAME,
Expand Down Expand Up @@ -41,7 +42,7 @@ async def async_setup(hass, config):
for conf in accesspoints:
if conf[CONF_ACCESSPOINT] not in configured_haps(hass):
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
DOMAIN, source=config_entries.SOURCE_IMPORT, data={
HMIPC_HAPID: conf[CONF_ACCESSPOINT],
HMIPC_AUTHTOKEN: conf[CONF_AUTHTOKEN],
HMIPC_NAME: conf[CONF_NAME],
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/homematicip_cloud/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def __init__(self):
"""Initialize HomematicIP Cloud config flow."""
self.auth = None

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a flow start."""
errors = {}
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/hue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import voluptuous as vol

from homeassistant import data_entry_flow
from homeassistant import config_entries
from homeassistant.const import CONF_FILENAME, CONF_HOST
from homeassistant.helpers import aiohttp_client, config_validation as cv

Expand Down Expand Up @@ -108,7 +108,7 @@ async def async_setup(hass, config):
# deadlock: creating a config entry will set up the component but the
# setup would block till the entry is created!
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source=data_entry_flow.SOURCE_IMPORT, data={
DOMAIN, source=config_entries.SOURCE_IMPORT, data={
'host': bridge_conf[CONF_HOST],
'path': bridge_conf[CONF_FILENAME],
}
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/hue/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def async_setup(self, tries=0):
# linking procedure. When linking succeeds, it will remove the
# old config entry.
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
DOMAIN, source=config_entries.SOURCE_IMPORT, data={
'host': host,
}
))
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/hue/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def __init__(self):
"""Initialize the Hue flow."""
self.host = None

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a flow start."""
from aiohue.discovery import discover_nupnp
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/nest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import (
CONF_STRUCTURE, CONF_FILENAME, CONF_BINARY_SENSORS, CONF_SENSORS,
CONF_MONITORED_CONDITIONS,
Expand Down Expand Up @@ -103,7 +104,7 @@ async def async_setup(hass, config):
access_token_cache_file = hass.config.path(filename)

hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, source='import', data={
DOMAIN, source=config_entries.SOURCE_IMPORT, data={
'nest_conf_path': access_token_cache_file,
}
))
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/nest/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def __init__(self):
"""Initialize the Nest config flow."""
self.flow_impl = None

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a flow start."""
flows = self.hass.data.get(DATA_FLOW_IMPL, {})
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/sonos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Component to embed Sonos."""
from homeassistant import data_entry_flow
from homeassistant import config_entries
from homeassistant.helpers import config_entry_flow


Expand All @@ -15,7 +15,7 @@ async def async_setup(hass, config):

if conf is not None:
hass.async_create_task(hass.config_entries.flow.async_init(
DOMAIN, source=data_entry_flow.SOURCE_IMPORT))
DOMAIN, source=config_entries.SOURCE_IMPORT))

return True

Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/zone/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self):
"""Initialize zone configuration flow."""
pass

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a flow start."""
errors = {}
Expand Down
29 changes: 21 additions & 8 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'init' step.

@config_entries.HANDLERS.register(DOMAIN)
class ExampleConfigFlow(config_entries.FlowHandler):
class ExampleConfigFlow(data_entry_flow.FlowHandler):

VERSION = 1

Expand Down Expand Up @@ -123,6 +123,11 @@ async def async_step_discovery(info):


_LOGGER = logging.getLogger(__name__)

SOURCE_USER = 'user'
SOURCE_DISCOVERY = 'discovery'
SOURCE_IMPORT = 'import'

HANDLERS = Registry()
# Components that have config flows. In future we will auto-generate this list.
FLOWS = [
Expand Down Expand Up @@ -151,8 +156,8 @@ async def async_step_discovery(info):

DISCOVERY_NOTIFICATION_ID = 'config_entry_discovery'
DISCOVERY_SOURCES = (
data_entry_flow.SOURCE_DISCOVERY,
data_entry_flow.SOURCE_IMPORT,
SOURCE_DISCOVERY,
SOURCE_IMPORT,
)

EVENT_FLOW_DISCOVERED = 'config_entry_discovered'
Expand Down Expand Up @@ -374,12 +379,15 @@ async def _async_finish_flow(self, result):
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=result['source'],
source=source,
)
self._entries.append(entry)
await self._async_schedule_save()
Expand All @@ -399,13 +407,13 @@ async def _async_finish_flow(self, result):

return entry

async def _async_create_flow(self, handler, *, source, data):
async def _async_create_flow(self, handler_key, *, source, data):
"""Create a flow for specified handler.

Handler key is the domain of the component that we want to setup.
"""
component = getattr(self.hass.components, handler)
handler = HANDLERS.get(handler)
component = getattr(self.hass.components, handler_key)
handler = HANDLERS.get(handler_key)

if handler is None:
raise data_entry_flow.UnknownHandler
Expand All @@ -424,7 +432,12 @@ async def _async_create_flow(self, handler, *, source, data):
notification_id=DISCOVERY_NOTIFICATION_ID
)

return handler()
flow = handler()
if source is not None:
flow.init_step = source
else:
flow.init_step = 'init'
return flow

async def _async_schedule_save(self):
"""Save the entity registry to a file."""
Expand Down
18 changes: 5 additions & 13 deletions homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

_LOGGER = logging.getLogger(__name__)

SOURCE_USER = 'user'
SOURCE_DISCOVERY = 'discovery'
SOURCE_IMPORT = 'import'

RESULT_TYPE_FORM = 'form'
RESULT_TYPE_CREATE_ENTRY = 'create_entry'
RESULT_TYPE_ABORT = 'abort'
Expand Down Expand Up @@ -53,8 +49,8 @@ def async_progress(self) -> List[Dict]:
'source': flow.source,
} for flow in self._progress.values()]

async def async_init(self, handler: Callable, *, source: str = SOURCE_USER,
data: str = None) -> Any:
async def async_init(self, handler: Callable, *, source: str = None,
data: Any = None) -> Any:
"""Start a configuration flow."""
flow = await self._async_create_flow(handler, source=source, data=data)
flow.hass = self.hass
Expand All @@ -63,12 +59,7 @@ async def async_init(self, handler: Callable, *, source: str = SOURCE_USER,
flow.source = source
self._progress[flow.flow_id] = flow

if source == SOURCE_USER:
step = 'init'
else:
step = source

return await self._async_handle_step(flow, step, data)
return await self._async_handle_step(flow, flow.init_step, data)

async def async_configure(
self, flow_id: str, user_input: str = None) -> Any:
Expand Down Expand Up @@ -131,8 +122,9 @@ class FlowHandler:
flow_id = None
hass = None
handler = None
source = SOURCE_USER
source = None
cur_step = None
init_step = 'init'

# Set by developer
VERSION = 1
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/helpers/config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def __init__(self, domain, title, discovery_function):
self._title = title
self._discovery_function = discovery_function

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
return await self.async_step_init(user_input)

async def async_step_init(self, user_input=None):
"""Handle a flow initialized by the user."""
if self._async_current_entries():
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/helpers/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import voluptuous as vol

from homeassistant import data_entry_flow
from homeassistant import data_entry_flow, config_entries
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.data_validator import RequestDataValidator

Expand Down Expand Up @@ -53,7 +53,8 @@ async def post(self, request, data):
handler = data['handler']

try:
result = await self._flow_mgr.async_init(handler)
result = await self._flow_mgr.async_init(
handler, source=config_entries.SOURCE_USER)
except data_entry_flow.UnknownHandler:
return self.json_message('Invalid handler specified', 404)
except data_entry_flow.UnknownStep:
Expand Down
4 changes: 2 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import threading
from contextlib import contextmanager

from homeassistant import auth, core as ha, data_entry_flow, config_entries
from homeassistant import auth, core as ha, config_entries
from homeassistant.auth import (
models as auth_models, auth_store, providers as auth_providers)
from homeassistant.setup import setup_component, async_setup_component
Expand Down Expand Up @@ -509,7 +509,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
"""Helper for creating config entries that adds some defaults."""

def __init__(self, *, domain='test', data=None, version=0, entry_id=None,
source=data_entry_flow.SOURCE_USER, title='Mock Title',
source=config_entries.SOURCE_USER, title='Mock Title',
state=None):
"""Initialize a mock config entry."""
kwargs = {
Expand Down
Loading