-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
deCONZ config entry #13402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deCONZ config entry #13402
Changes from 11 commits
17c685f
cecd17b
929450c
872e268
d9d9824
444ad17
078f1e6
5b20e5e
ed1aad5
f4e08c5
b6f88d8
b152fb0
f6e9804
010ebe9
6ee7037
030e78e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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==33'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -57,6 +58,11 @@ | |
|
|
||
| async def async_setup(hass, config): | ||
| """Set up services and configuration for deCONZ component.""" | ||
| if DOMAIN in hass.data: | ||
| _LOGGER.error( | ||
| "Set up failed since one deCONZ instance already exists") | ||
| return False | ||
|
|
||
| result = False | ||
| config_file = await hass.async_add_job( | ||
| load_json, hass.config.path(CONFIG_FILE)) | ||
|
|
@@ -160,7 +166,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) | ||
|
|
@@ -186,3 +193,85 @@ async def async_configuration_callback(data): | |
| entity_picture="/static/images/logo_deconz.jpeg", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove all the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if a user has a host configured and has a config entry for the same host?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, I will make sure to check in both setups as well. |
||
| if result: | ||
| return True | ||
| return False | ||
| 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" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """Tests for deCONZ config flow.""" | ||
| import asyncio | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'asyncio' imported but unused |
||
| from unittest.mock import Mock, patch | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'unittest.mock.Mock' imported but unused |
||
|
|
||
| import pydeconz | ||
| import pytest | ||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.components.deconz as deconz | ||
|
|
||
| from tests.common import MockConfigEntry, mock_coro | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'tests.common.MockConfigEntry' imported but unused |
||
|
|
||
|
|
||
| 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 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'}) | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line at end of file |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will never happen. Home Assistant already takes care of this.