-
-
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 7 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__) | ||
|
|
||
|
|
@@ -186,3 +187,91 @@ async def async_configuration_callback(data): | |
| entity_picture="/static/images/logo_deconz.jpeg", | ||
| 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) | ||
| }) | ||
| ) | ||
| else: | ||
| 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: | ||
| api_key = await async_get_api_key(self.hass.loop, **self.deconz_config) | ||
|
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. line too long (83 > 79 characters) |
||
| 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.""" | ||
| 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 | ||
|
|
||
|
|
||
| async def async_unload_entry(hass, entry): | ||
|
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. Remove this. We don't support unloading platforms yet and so you are not removing all the entities that this config entry has created.
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. Will do |
||
| """Unload an entry.""" | ||
| deconz = hass.data[DOMAIN] | ||
| entity_ids = hass.data[DATA_DECONZ_ID] | ||
| for entity_id in entity_ids.keys(): | ||
| hass.states.async_remove(entity_id) | ||
| deconz.close() | ||
| del hass.data[DOMAIN] | ||
| return True | ||
| 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" | ||
| } | ||
| } | ||
| } |
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.
Please remove all the
configuratorcode.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.
Is it ok that I do this when I've migrated the discovery part?
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.
Sure