Deconz make groups configurable (#14704)
* Make groups configurable * Config flow and tests in place * Fix too long line
This commit is contained in:
parent
940577e105
commit
ac13a2736b
7 changed files with 46 additions and 12 deletions
|
@ -23,7 +23,8 @@
|
||||||
"options": {
|
"options": {
|
||||||
"title": "Extra configuration options for deCONZ",
|
"title": "Extra configuration options for deCONZ",
|
||||||
"data": {
|
"data": {
|
||||||
"allow_clip_sensor": "Allow importing virtual sensors"
|
"allow_clip_sensor": "Allow importing virtual sensors",
|
||||||
|
"allow_deconz_groups": "Allow importing deCONZ groups"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,7 +8,9 @@ from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
from homeassistant.util.json import load_json
|
from homeassistant.util.json import load_json
|
||||||
|
|
||||||
from .const import CONF_ALLOW_CLIP_SENSOR, CONFIG_FILE, DOMAIN
|
from .const import (
|
||||||
|
CONF_ALLOW_DECONZ_GROUPS, CONF_ALLOW_CLIP_SENSOR, CONFIG_FILE, DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
CONF_BRIDGEID = 'bridgeid'
|
CONF_BRIDGEID = 'bridgeid'
|
||||||
|
|
||||||
|
@ -94,12 +96,15 @@ class DeconzFlowHandler(data_entry_flow.FlowHandler):
|
||||||
"""Extra options for deCONZ.
|
"""Extra options for deCONZ.
|
||||||
|
|
||||||
CONF_CLIP_SENSOR -- Allow user to choose if they want clip sensors.
|
CONF_CLIP_SENSOR -- Allow user to choose if they want clip sensors.
|
||||||
|
CONF_DECONZ_GROUPS -- Allow user to choose if they want deCONZ groups.
|
||||||
"""
|
"""
|
||||||
from pydeconz.utils import async_get_bridgeid
|
from pydeconz.utils import async_get_bridgeid
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.deconz_config[CONF_ALLOW_CLIP_SENSOR] = \
|
self.deconz_config[CONF_ALLOW_CLIP_SENSOR] = \
|
||||||
user_input[CONF_ALLOW_CLIP_SENSOR]
|
user_input[CONF_ALLOW_CLIP_SENSOR]
|
||||||
|
self.deconz_config[CONF_ALLOW_DECONZ_GROUPS] = \
|
||||||
|
user_input[CONF_ALLOW_DECONZ_GROUPS]
|
||||||
|
|
||||||
if CONF_BRIDGEID not in self.deconz_config:
|
if CONF_BRIDGEID not in self.deconz_config:
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
|
@ -115,6 +120,7 @@ class DeconzFlowHandler(data_entry_flow.FlowHandler):
|
||||||
step_id='options',
|
step_id='options',
|
||||||
data_schema=vol.Schema({
|
data_schema=vol.Schema({
|
||||||
vol.Optional(CONF_ALLOW_CLIP_SENSOR): bool,
|
vol.Optional(CONF_ALLOW_CLIP_SENSOR): bool,
|
||||||
|
vol.Optional(CONF_ALLOW_DECONZ_GROUPS): bool,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -158,6 +164,7 @@ class DeconzFlowHandler(data_entry_flow.FlowHandler):
|
||||||
return await self.async_step_link()
|
return await self.async_step_link()
|
||||||
|
|
||||||
self.deconz_config[CONF_ALLOW_CLIP_SENSOR] = True
|
self.deconz_config[CONF_ALLOW_CLIP_SENSOR] = True
|
||||||
|
self.deconz_config[CONF_ALLOW_DECONZ_GROUPS] = True
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title='deCONZ-' + self.deconz_config[CONF_BRIDGEID],
|
title='deCONZ-' + self.deconz_config[CONF_BRIDGEID],
|
||||||
data=self.deconz_config
|
data=self.deconz_config
|
||||||
|
|
|
@ -10,3 +10,4 @@ DATA_DECONZ_ID = 'deconz_entities'
|
||||||
DATA_DECONZ_UNSUB = 'deconz_dispatchers'
|
DATA_DECONZ_UNSUB = 'deconz_dispatchers'
|
||||||
|
|
||||||
CONF_ALLOW_CLIP_SENSOR = 'allow_clip_sensor'
|
CONF_ALLOW_CLIP_SENSOR = 'allow_clip_sensor'
|
||||||
|
CONF_ALLOW_DECONZ_GROUPS = 'allow_deconz_groups'
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
"options": {
|
"options": {
|
||||||
"title": "Extra configuration options for deCONZ",
|
"title": "Extra configuration options for deCONZ",
|
||||||
"data":{
|
"data":{
|
||||||
"allow_clip_sensor": "Allow importing virtual sensors"
|
"allow_clip_sensor": "Allow importing virtual sensors",
|
||||||
|
"allow_deconz_groups": "Allow importing deCONZ groups"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,6 +6,7 @@ https://home-assistant.io/components/light.deconz/
|
||||||
"""
|
"""
|
||||||
from homeassistant.components.deconz import (
|
from homeassistant.components.deconz import (
|
||||||
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB)
|
DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB)
|
||||||
|
from homeassistant.components.deconz.const import CONF_ALLOW_DECONZ_GROUPS
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR,
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR,
|
||||||
ATTR_TRANSITION, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT,
|
ATTR_TRANSITION, EFFECT_COLORLOOP, FLASH_LONG, FLASH_SHORT,
|
||||||
|
@ -33,6 +34,7 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
|
||||||
for light in lights:
|
for light in lights:
|
||||||
entities.append(DeconzLight(light))
|
entities.append(DeconzLight(light))
|
||||||
async_add_devices(entities, True)
|
async_add_devices(entities, True)
|
||||||
|
|
||||||
hass.data[DATA_DECONZ_UNSUB].append(
|
hass.data[DATA_DECONZ_UNSUB].append(
|
||||||
async_dispatcher_connect(hass, 'deconz_new_light', async_add_light))
|
async_dispatcher_connect(hass, 'deconz_new_light', async_add_light))
|
||||||
|
|
||||||
|
@ -40,10 +42,12 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
|
||||||
def async_add_group(groups):
|
def async_add_group(groups):
|
||||||
"""Add group from deCONZ."""
|
"""Add group from deCONZ."""
|
||||||
entities = []
|
entities = []
|
||||||
|
allow_group = config_entry.data.get(CONF_ALLOW_DECONZ_GROUPS, True)
|
||||||
for group in groups:
|
for group in groups:
|
||||||
if group.lights:
|
if group.lights and allow_group:
|
||||||
entities.append(DeconzLight(group))
|
entities.append(DeconzLight(group))
|
||||||
async_add_devices(entities, True)
|
async_add_devices(entities, True)
|
||||||
|
|
||||||
hass.data[DATA_DECONZ_UNSUB].append(
|
hass.data[DATA_DECONZ_UNSUB].append(
|
||||||
async_dispatcher_connect(hass, 'deconz_new_group', async_add_group))
|
async_dispatcher_connect(hass, 'deconz_new_group', async_add_group))
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ async def test_flow_works(hass, aioclient_mock):
|
||||||
await flow.async_step_init()
|
await flow.async_step_init()
|
||||||
await flow.async_step_link(user_input={})
|
await flow.async_step_link(user_input={})
|
||||||
result = await flow.async_step_options(
|
result = await flow.async_step_options(
|
||||||
user_input={'allow_clip_sensor': True})
|
user_input={'allow_clip_sensor': True, 'allow_deconz_groups': True})
|
||||||
|
|
||||||
assert result['type'] == 'create_entry'
|
assert result['type'] == 'create_entry'
|
||||||
assert result['title'] == 'deCONZ-id'
|
assert result['title'] == 'deCONZ-id'
|
||||||
|
@ -32,7 +32,8 @@ async def test_flow_works(hass, aioclient_mock):
|
||||||
'host': '1.2.3.4',
|
'host': '1.2.3.4',
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF',
|
'api_key': '1234567890ABCDEF',
|
||||||
'allow_clip_sensor': True
|
'allow_clip_sensor': True,
|
||||||
|
'allow_deconz_groups': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -149,6 +150,7 @@ async def test_bridge_discovery_config_file(hass):
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'serial': 'id'
|
'serial': 'id'
|
||||||
})
|
})
|
||||||
|
|
||||||
assert result['type'] == 'create_entry'
|
assert result['type'] == 'create_entry'
|
||||||
assert result['title'] == 'deCONZ-id'
|
assert result['title'] == 'deCONZ-id'
|
||||||
assert result['data'] == {
|
assert result['data'] == {
|
||||||
|
@ -156,7 +158,8 @@ async def test_bridge_discovery_config_file(hass):
|
||||||
'host': '1.2.3.4',
|
'host': '1.2.3.4',
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF',
|
'api_key': '1234567890ABCDEF',
|
||||||
'allow_clip_sensor': True
|
'allow_clip_sensor': True,
|
||||||
|
'allow_deconz_groups': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -217,6 +220,7 @@ async def test_import_with_api_key(hass):
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF'
|
'api_key': '1234567890ABCDEF'
|
||||||
})
|
})
|
||||||
|
|
||||||
assert result['type'] == 'create_entry'
|
assert result['type'] == 'create_entry'
|
||||||
assert result['title'] == 'deCONZ-id'
|
assert result['title'] == 'deCONZ-id'
|
||||||
assert result['data'] == {
|
assert result['data'] == {
|
||||||
|
@ -224,7 +228,8 @@ async def test_import_with_api_key(hass):
|
||||||
'host': '1.2.3.4',
|
'host': '1.2.3.4',
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF',
|
'api_key': '1234567890ABCDEF',
|
||||||
'allow_clip_sensor': True
|
'allow_clip_sensor': True,
|
||||||
|
'allow_deconz_groups': True
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -238,7 +243,7 @@ async def test_options(hass, aioclient_mock):
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF'}
|
'api_key': '1234567890ABCDEF'}
|
||||||
result = await flow.async_step_options(
|
result = await flow.async_step_options(
|
||||||
user_input={'allow_clip_sensor': False})
|
user_input={'allow_clip_sensor': False, 'allow_deconz_groups': False})
|
||||||
assert result['type'] == 'create_entry'
|
assert result['type'] == 'create_entry'
|
||||||
assert result['title'] == 'deCONZ-id'
|
assert result['title'] == 'deCONZ-id'
|
||||||
assert result['data'] == {
|
assert result['data'] == {
|
||||||
|
@ -246,5 +251,6 @@ async def test_options(hass, aioclient_mock):
|
||||||
'host': '1.2.3.4',
|
'host': '1.2.3.4',
|
||||||
'port': 80,
|
'port': 80,
|
||||||
'api_key': '1234567890ABCDEF',
|
'api_key': '1234567890ABCDEF',
|
||||||
'allow_clip_sensor': False
|
'allow_clip_sensor': False,
|
||||||
|
'allow_deconz_groups': False
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ GROUP = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def setup_bridge(hass, data):
|
async def setup_bridge(hass, data, allow_deconz_groups=True):
|
||||||
"""Load the deCONZ light platform."""
|
"""Load the deCONZ light platform."""
|
||||||
from pydeconz import DeconzSession
|
from pydeconz import DeconzSession
|
||||||
loop = Mock()
|
loop = Mock()
|
||||||
|
@ -53,7 +53,9 @@ async def setup_bridge(hass, data):
|
||||||
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
hass.data[deconz.DATA_DECONZ_UNSUB] = []
|
||||||
hass.data[deconz.DATA_DECONZ_ID] = {}
|
hass.data[deconz.DATA_DECONZ_ID] = {}
|
||||||
config_entry = config_entries.ConfigEntry(
|
config_entry = config_entries.ConfigEntry(
|
||||||
1, deconz.DOMAIN, 'Mock Title', {'host': 'mock-host'}, 'test')
|
1, deconz.DOMAIN, 'Mock Title',
|
||||||
|
{'host': 'mock-host', 'allow_deconz_groups': allow_deconz_groups},
|
||||||
|
'test')
|
||||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
|
await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
|
||||||
# To flush out the service call to update the group
|
# To flush out the service call to update the group
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -98,3 +100,15 @@ async def test_add_new_group(hass):
|
||||||
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "light.name" in hass.data[deconz.DATA_DECONZ_ID]
|
assert "light.name" in hass.data[deconz.DATA_DECONZ_ID]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_do_not_add_deconz_groups(hass):
|
||||||
|
"""Test that clip sensors can be ignored."""
|
||||||
|
data = {}
|
||||||
|
await setup_bridge(hass, data, allow_deconz_groups=False)
|
||||||
|
group = Mock()
|
||||||
|
group.name = 'name'
|
||||||
|
group.register_async_callback = Mock()
|
||||||
|
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(hass.data[deconz.DATA_DECONZ_ID]) == 0
|
||||||
|
|
Loading…
Add table
Reference in a new issue