Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"init": {
"data": {
"host": "Host",
"port": "Port (default value: '80')"
"port": "Port"
},
"title": "Define deCONZ gateway"
},
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/.translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"init": {
"data": {
"host": "V\u00e4rd",
"port": "Port (standardv\u00e4rde: '80')"
"port": "Port"
},
"title": "Definiera deCONZ-gatewaye"
},
Expand Down
11 changes: 3 additions & 8 deletions homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
CONF_API_KEY, CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.util.json import load_json

# Loading the config flow file will register the flow
from .config_flow import configured_hosts
from .const import CONFIG_FILE, DOMAIN, _LOGGER
from .const import DEFAULT_PORT, DOMAIN, _LOGGER
from .gateway import DeconzGateway

REQUIREMENTS = ['pydeconz==47']
Expand All @@ -27,7 +26,7 @@
DOMAIN: vol.Schema({
vol.Optional(CONF_API_KEY): cv.string,
vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=80): cv.port,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
})
}, extra=vol.ALLOW_EXTRA)

Expand All @@ -53,11 +52,7 @@ async def async_setup(hass, config):
"""
if DOMAIN in config:
deconz_config = None
config_file = await hass.async_add_job(
load_json, hass.config.path(CONFIG_FILE))
if config_file:
deconz_config = config_file
elif CONF_HOST in config[DOMAIN]:
if CONF_HOST in config[DOMAIN]:
deconz_config = config[DOMAIN]
if deconz_config and not configured_hosts(hass):
hass.async_add_job(hass.config_entries.flow.async_init(
Expand Down
28 changes: 15 additions & 13 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from homeassistant.core import callback
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
from homeassistant.helpers import aiohttp_client
from homeassistant.util.json import load_json

from .const import (
CONF_ALLOW_DECONZ_GROUPS, CONF_ALLOW_CLIP_SENSOR, CONFIG_FILE, DOMAIN)

CONF_ALLOW_DECONZ_GROUPS, CONF_ALLOW_CLIP_SENSOR, DEFAULT_PORT, DOMAIN)

CONF_BRIDGEID = 'bridgeid'

Expand All @@ -35,6 +33,10 @@ def __init__(self):
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.

Only allows one instance to be set up.
Expand All @@ -51,26 +53,33 @@ async def async_step_user(self, user_input=None):
if bridge[CONF_HOST] == user_input[CONF_HOST]:
self.deconz_config = bridge
return await self.async_step_link()
self.deconz_config = user_input
return await self.async_step_link()

session = aiohttp_client.async_get_clientsession(self.hass)
self.bridges = await async_discovery(session)

if len(self.bridges) == 1:
self.deconz_config = self.bridges[0]
return await self.async_step_link()

if len(self.bridges) > 1:
hosts = []
for bridge in self.bridges:
hosts.append(bridge[CONF_HOST])
return self.async_show_form(
step_id='user',
step_id='init',
data_schema=vol.Schema({
vol.Required(CONF_HOST): vol.In(hosts)
})
)

return self.async_abort(
reason='no_bridges'
return self.async_show_form(
step_id='user',
data_schema=vol.Schema({
vol.Required(CONF_HOST): str,
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
}),
)

async def async_step_link(self, user_input=None):
Expand Down Expand Up @@ -135,13 +144,6 @@ async def async_step_discovery(self, discovery_info):
deconz_config[CONF_PORT] = discovery_info.get(CONF_PORT)
deconz_config[CONF_BRIDGEID] = discovery_info.get('serial')

config_file = await self.hass.async_add_job(
load_json, self.hass.config.path(CONFIG_FILE))
if config_file and \
config_file[CONF_HOST] == deconz_config[CONF_HOST] and \
CONF_API_KEY in config_file:
deconz_config[CONF_API_KEY] = config_file[CONF_API_KEY]

return await self.async_step_import(deconz_config)

async def async_step_import(self, import_config):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/deconz/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
_LOGGER = logging.getLogger('homeassistant.components.deconz')

DOMAIN = 'deconz'
CONFIG_FILE = 'deconz.conf'
DECONZ_DOMAIN = 'deconz'

DEFAULT_PORT = 80

CONF_ALLOW_CLIP_SENSOR = 'allow_clip_sensor'
CONF_ALLOW_DECONZ_GROUPS = 'allow_deconz_groups'
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/deconz/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"title": "Define deCONZ gateway",
"data": {
"host": "Host",
"port": "Port (default value: '80')"
"port": "Port"
}
},
"link": {
Expand Down
81 changes: 26 additions & 55 deletions tests/components/deconz/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Tests for deCONZ config flow."""
from unittest.mock import patch
import pytest

import voluptuous as vol
Expand Down Expand Up @@ -45,7 +44,7 @@ async def test_flow_already_registered_bridge(hass):
flow = config_flow.DeconzFlowHandler()
flow.hass = hass

result = await flow.async_step_user()
result = await flow.async_step_init()
assert result['type'] == 'abort'


Expand All @@ -55,8 +54,9 @@ async def test_flow_no_discovered_bridges(hass, aioclient_mock):
flow = config_flow.DeconzFlowHandler()
flow.hass = hass

result = await flow.async_step_user()
assert result['type'] == 'abort'
result = await flow.async_step_init()
assert result['type'] == 'form'
assert result['step_id'] == 'user'


async def test_flow_one_bridge_discovered(hass, aioclient_mock):
Expand All @@ -67,7 +67,7 @@ async def test_flow_one_bridge_discovered(hass, aioclient_mock):
flow = config_flow.DeconzFlowHandler()
flow.hass = hass

result = await flow.async_step_user()
result = await flow.async_step_init()
assert result['type'] == 'form'
assert result['step_id'] == 'link'

Expand All @@ -81,9 +81,9 @@ async def test_flow_two_bridges_discovered(hass, aioclient_mock):
flow = config_flow.DeconzFlowHandler()
flow.hass = hass

result = await flow.async_step_user()
result = await flow.async_step_init()
assert result['type'] == 'form'
assert result['step_id'] == 'user'
assert result['step_id'] == 'init'

with pytest.raises(vol.Invalid):
assert result['data_schema']({'host': '0.0.0.0'})
Expand All @@ -101,12 +101,26 @@ async def test_flow_two_bridges_selection(hass, aioclient_mock):
{'bridgeid': 'id2', 'host': '5.6.7.8', 'port': 80}
]

result = await flow.async_step_user(user_input={'host': '1.2.3.4'})
result = await flow.async_step_init(user_input={'host': '1.2.3.4'})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert flow.deconz_config['host'] == '1.2.3.4'


async def test_flow_manual_configuration(hass, aioclient_mock):
"""Test config flow with manual input."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[])
flow = config_flow.DeconzFlowHandler()
flow.hass = hass

user_input = {'host': '1.2.3.4', 'port': 80}

result = await flow.async_step_init(user_input)
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert flow.deconz_config == user_input


async def test_link_no_api_key(hass, aioclient_mock):
"""Test config flow should abort if no API key was possible to retrieve."""
aioclient_mock.post('http://1.2.3.4:80/api', json=[])
Expand Down Expand Up @@ -138,57 +152,14 @@ async def test_link_already_registered_bridge(hass):


async def test_bridge_discovery(hass):
"""Test a bridge being discovered with no additional config file."""
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'load_json', return_value={}):
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
'serial': 'id'
})

assert result['type'] == 'form'
assert result['step_id'] == 'link'


async def test_bridge_discovery_config_file(hass):
"""Test a bridge being discovered with a corresponding config file."""
"""Test a bridge being discovered."""
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'load_json',
return_value={'host': '1.2.3.4',
'port': 8080,
'api_key': '1234567890ABCDEF'}):
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
'serial': 'id'
})

assert result['type'] == 'create_entry'
assert result['title'] == 'deCONZ-id'
assert result['data'] == {
'bridgeid': 'id',
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
'api_key': '1234567890ABCDEF',
'allow_clip_sensor': True,
'allow_deconz_groups': True
}


async def test_bridge_discovery_other_config_file(hass):
"""Test a bridge being discovered with another bridges config file."""
flow = config_flow.DeconzFlowHandler()
flow.hass = hass
with patch.object(config_flow, 'load_json',
return_value={'host': '5.6.7.8', 'api_key': '5678'}):
result = await flow.async_step_discovery({
'host': '1.2.3.4',
'port': 80,
'serial': 'id'
})
'serial': 'id'
})

assert result['type'] == 'form'
assert result['step_id'] == 'link'
Expand Down
Loading