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
25 changes: 25 additions & 0 deletions homeassistant/components/deconz/.translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"config": {
"title": "deCONZ",
"step": {
"init": {
"title": "Define deCONZ gateway",
"data": {
"host": "Host",
"port": "Port (default value: '80')"
}
},
"link": {
"title": "Link with deCONZ",
"description": "Unlock your deCONZ gateway to register with Home Assistant.\n\n1. Go to deCONZ system settings\n2. Press \"Unlock Gateway\" button"
}
},
"error": {
"no_key": "Couldn't get an API key"
},
"abort": {
"no_bridges": "No deCONZ bridges discovered",
"one_instance_only": "Component only supports one deCONZ instance"
}
}
}
90 changes: 87 additions & 3 deletions homeassistant/components/deconz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.discovery import SERVICE_DECONZ
from homeassistant.const import (
CONF_API_KEY, CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP)
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.helpers import discovery, aiohttp_client
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util.json import load_json, save_json

REQUIREMENTS = ['pydeconz==32']
REQUIREMENTS = ['pydeconz==35']

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -160,7 +161,8 @@ async def async_request_configuration(hass, config, deconz_config):
async def async_configuration_callback(data):
"""Set up actions to do when our configuration callback is called."""
from pydeconz.utils import async_get_api_key
api_key = await async_get_api_key(hass.loop, **deconz_config)
websession = async_get_clientsession(hass)
api_key = await async_get_api_key(websession, **deconz_config)
if api_key:
deconz_config[CONF_API_KEY] = api_key
result = await async_setup_deconz(hass, config, deconz_config)
Expand All @@ -186,3 +188,85 @@ async def async_configuration_callback(data):
entity_picture="/static/images/logo_deconz.jpeg",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all the configurator code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok that I do this when I've migrated the discovery part?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

submit_caption="I have unlocked the gateway",
)


@config_entries.HANDLERS.register(DOMAIN)
class DeconzFlowHandler(config_entries.ConfigFlowHandler):
"""Handle a deCONZ config flow."""

VERSION = 1

def __init__(self):
"""Initialize the deCONZ flow."""
self.bridges = []
self.deconz_config = {}

async def async_step_init(self, user_input=None):
"""Handle a flow start."""
from pydeconz.utils import async_discovery

if DOMAIN in self.hass.data:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you cannot create a config entry if you have one configured, but you will be able to configure a host after you have created a config entry?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an instance of deconz running, the config flow won't run. What do you mean by "but you will be able to configure a host after you have created a config entry"?

return self.async_abort(
reason='one_instance_only'
)

if user_input is not None:
for bridge in self.bridges:
if bridge[CONF_HOST] == user_input[CONF_HOST]:
self.deconz_config = bridge
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()
elif len(self.bridges) > 1:
hosts = []
for bridge in self.bridges:
hosts.append(bridge[CONF_HOST])
return self.async_show_form(
step_id='init',
data_schema=vol.Schema({
vol.Required(CONF_HOST): vol.In(hosts)
})
)

return self.async_abort(
reason='no_bridges'
)

async def async_step_link(self, user_input=None):
"""Attempt to link with the deCONZ bridge."""
from pydeconz.utils import async_get_api_key
errors = {}

if user_input is not None:
session = aiohttp_client.async_get_clientsession(self.hass)
api_key = await async_get_api_key(session, **self.deconz_config)
if api_key:
self.deconz_config[CONF_API_KEY] = api_key
return self.async_create_entry(
title='deCONZ',
data=self.deconz_config
)
else:
errors['base'] = 'no_key'

return self.async_show_form(
step_id='link',
errors=errors,
)


async def async_setup_entry(hass, entry):
"""Set up a bridge for a config entry."""
if DOMAIN in hass.data:
_LOGGER.error(
"Config entry failed since one deCONZ instance already exists")
return False
result = await async_setup_deconz(hass, None, entry.data)
if result:
return True
return False
25 changes: 25 additions & 0 deletions homeassistant/components/deconz/strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"config": {
"title": "deCONZ",
"step": {
"init": {
"title": "Define deCONZ gateway",
"data": {
"host": "Host",
"port": "Port (default value: '80')"
}
},
"link": {
"title": "Link with deCONZ",
"description": "Unlock your deCONZ gateway to register with Home Assistant.\n\n1. Go to deCONZ system settings\n2. Press \"Unlock Gateway\" button"
}
},
"error": {
"no_key": "Couldn't get an API key"
},
"abort": {
"no_bridges": "No deCONZ bridges discovered",
"one_instance_only": "Component only supports one deCONZ instance"
}
}
}
1 change: 1 addition & 0 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ async def async_step_discovery(info):
FLOWS = [
'config_entry_example',
'hue',
'deconz',
]

SOURCE_USER = 'user'
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ pycsspeechtts==1.0.2
pydaikin==0.4

# homeassistant.components.deconz
pydeconz==32
pydeconz==35

# homeassistant.components.zwave
pydispatcher==2.0.5
Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ pushbullet.py==0.11.0
# homeassistant.components.canary
py-canary==0.4.1

# homeassistant.components.deconz
pydeconz==35

# homeassistant.components.zwave
pydispatcher==2.0.5

Expand Down
1 change: 1 addition & 0 deletions script/gen_requirements_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'prometheus_client',
'pushbullet.py',
'py-canary',
'pydeconz',
'pydispatcher',
'PyJWT',
'pylitejet',
Expand Down
97 changes: 97 additions & 0 deletions tests/components/test_deconz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Tests for deCONZ config flow."""
import pytest

import voluptuous as vol

import homeassistant.components.deconz as deconz
import pydeconz


async def test_flow_works(hass, aioclient_mock):
"""Test config flow."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id', 'internalipaddress': '1.2.3.4', 'internalport': '80'}
])
aioclient_mock.post('http://1.2.3.4:80/api', json=[
{"success": {"username": "1234567890ABCDEF"}}
])

flow = deconz.DeconzFlowHandler()
flow.hass = hass
await flow.async_step_init()
result = await flow.async_step_link(user_input={})

assert result['type'] == 'create_entry'
assert result['title'] == 'deCONZ'
assert result['data'] == {
'bridgeid': 'id',
'host': '1.2.3.4',
'port': '80',
'api_key': '1234567890ABCDEF'
}


async def test_flow_already_registered_bridge(hass, aioclient_mock):
"""Test config flow don't allow more than one bridge to be registered."""
flow = deconz.DeconzFlowHandler()
flow.hass = hass
flow.hass.data[deconz.DOMAIN] = True

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


async def test_flow_no_discovered_bridges(hass, aioclient_mock):
"""Test config flow discovers no bridges."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[])
flow = deconz.DeconzFlowHandler()
flow.hass = hass

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


async def test_flow_one_bridge_discovered(hass, aioclient_mock):
"""Test config flow discovers one bridge."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id', 'internalipaddress': '1.2.3.4', 'internalport': '80'}
])
flow = deconz.DeconzFlowHandler()
flow.hass = hass

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


async def test_flow_two_bridges_discovered(hass, aioclient_mock):
"""Test config flow discovers two bridges."""
aioclient_mock.get(pydeconz.utils.URL_DISCOVER, json=[
{'id': 'id1', 'internalipaddress': '1.2.3.4', 'internalport': '80'},
{'id': 'id2', 'internalipaddress': '5.6.7.8', 'internalport': '80'}
])
flow = deconz.DeconzFlowHandler()
flow.hass = hass

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

with pytest.raises(vol.Invalid):
assert result['data_schema']({'host': '0.0.0.0'})

result['data_schema']({'host': '1.2.3.4'})
result['data_schema']({'host': '5.6.7.8'})


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line at end of file

async def test_flow_no_api_key(hass, aioclient_mock):
"""Test config flow discovers no bridges."""
aioclient_mock.post('http://1.2.3.4:80/api', json=[])
flow = deconz.DeconzFlowHandler()
flow.hass = hass
flow.deconz_config = {'host': '1.2.3.4', 'port': 80}

result = await flow.async_step_link(user_input={})
assert result['type'] == 'form'
assert result['step_id'] == 'link'
assert result['errors'] == {'base': 'no_key'}