From ac13a2736b26abcafc80f348edba53ff7dc8749d Mon Sep 17 00:00:00 2001
From: Robert Svensson <Kane610@users.noreply.github.com>
Date: Fri, 15 Jun 2018 20:31:22 +0200
Subject: [PATCH] Deconz make groups configurable (#14704)

* Make groups configurable

* Config flow and tests in place

* Fix too long line
---
 .../components/deconz/.translations/en.json    |  3 ++-
 homeassistant/components/deconz/config_flow.py |  9 ++++++++-
 homeassistant/components/deconz/const.py       |  1 +
 homeassistant/components/deconz/strings.json   |  3 ++-
 homeassistant/components/light/deconz.py       |  6 +++++-
 tests/components/deconz/test_config_flow.py    | 18 ++++++++++++------
 tests/components/light/test_deconz.py          | 18 ++++++++++++++++--
 7 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/homeassistant/components/deconz/.translations/en.json b/homeassistant/components/deconz/.translations/en.json
index a2f90e49e3a..465c6c1e0e8 100644
--- a/homeassistant/components/deconz/.translations/en.json
+++ b/homeassistant/components/deconz/.translations/en.json
@@ -23,7 +23,8 @@
             "options": {
                 "title": "Extra configuration options for deCONZ",
                 "data": {
-                    "allow_clip_sensor": "Allow importing virtual sensors"
+                    "allow_clip_sensor": "Allow importing virtual sensors",
+                    "allow_deconz_groups": "Allow importing deCONZ groups"
                 }
             }
         },
diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py
index cb7c3aad7fd..27fb6987f8c 100644
--- a/homeassistant/components/deconz/config_flow.py
+++ b/homeassistant/components/deconz/config_flow.py
@@ -8,7 +8,9 @@ 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_CLIP_SENSOR, CONFIG_FILE, DOMAIN
+from .const import (
+    CONF_ALLOW_DECONZ_GROUPS, CONF_ALLOW_CLIP_SENSOR, CONFIG_FILE, DOMAIN)
+
 
 CONF_BRIDGEID = 'bridgeid'
 
@@ -94,12 +96,15 @@ class DeconzFlowHandler(data_entry_flow.FlowHandler):
         """Extra options for deCONZ.
 
         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
 
         if user_input is not None:
             self.deconz_config[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:
                 session = aiohttp_client.async_get_clientsession(self.hass)
@@ -115,6 +120,7 @@ class DeconzFlowHandler(data_entry_flow.FlowHandler):
             step_id='options',
             data_schema=vol.Schema({
                 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()
 
         self.deconz_config[CONF_ALLOW_CLIP_SENSOR] = True
+        self.deconz_config[CONF_ALLOW_DECONZ_GROUPS] = True
         return self.async_create_entry(
             title='deCONZ-' + self.deconz_config[CONF_BRIDGEID],
             data=self.deconz_config
diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py
index 43f3c6441da..f7aa4c7a430 100644
--- a/homeassistant/components/deconz/const.py
+++ b/homeassistant/components/deconz/const.py
@@ -10,3 +10,4 @@ DATA_DECONZ_ID = 'deconz_entities'
 DATA_DECONZ_UNSUB = 'deconz_dispatchers'
 
 CONF_ALLOW_CLIP_SENSOR = 'allow_clip_sensor'
+CONF_ALLOW_DECONZ_GROUPS = 'allow_deconz_groups'
diff --git a/homeassistant/components/deconz/strings.json b/homeassistant/components/deconz/strings.json
index cabe58694d2..09549a300a0 100644
--- a/homeassistant/components/deconz/strings.json
+++ b/homeassistant/components/deconz/strings.json
@@ -16,7 +16,8 @@
             "options": {
                 "title": "Extra configuration options for deCONZ",
                 "data":{
-                    "allow_clip_sensor": "Allow importing virtual sensors"
+                    "allow_clip_sensor": "Allow importing virtual sensors",
+                    "allow_deconz_groups": "Allow importing deCONZ groups"
                 }
             }
         },
diff --git a/homeassistant/components/light/deconz.py b/homeassistant/components/light/deconz.py
index 916e60c00b1..a4593a72617 100644
--- a/homeassistant/components/light/deconz.py
+++ b/homeassistant/components/light/deconz.py
@@ -6,6 +6,7 @@ https://home-assistant.io/components/light.deconz/
 """
 from homeassistant.components.deconz import (
     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 (
     ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR,
     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:
             entities.append(DeconzLight(light))
         async_add_devices(entities, True)
+
     hass.data[DATA_DECONZ_UNSUB].append(
         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):
         """Add group from deCONZ."""
         entities = []
+        allow_group = config_entry.data.get(CONF_ALLOW_DECONZ_GROUPS, True)
         for group in groups:
-            if group.lights:
+            if group.lights and allow_group:
                 entities.append(DeconzLight(group))
         async_add_devices(entities, True)
+
     hass.data[DATA_DECONZ_UNSUB].append(
         async_dispatcher_connect(hass, 'deconz_new_group', async_add_group))
 
diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py
index df3310f3d6f..111cfbe9697 100644
--- a/tests/components/deconz/test_config_flow.py
+++ b/tests/components/deconz/test_config_flow.py
@@ -23,7 +23,7 @@ async def test_flow_works(hass, aioclient_mock):
     await flow.async_step_init()
     await flow.async_step_link(user_input={})
     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['title'] == 'deCONZ-id'
@@ -32,7 +32,8 @@ async def test_flow_works(hass, aioclient_mock):
         'host': '1.2.3.4',
         'port': 80,
         '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,
             'serial': 'id'
         })
+
     assert result['type'] == 'create_entry'
     assert result['title'] == 'deCONZ-id'
     assert result['data'] == {
@@ -156,7 +158,8 @@ async def test_bridge_discovery_config_file(hass):
         'host': '1.2.3.4',
         'port': 80,
         '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,
         'api_key': '1234567890ABCDEF'
     })
+
     assert result['type'] == 'create_entry'
     assert result['title'] == 'deCONZ-id'
     assert result['data'] == {
@@ -224,7 +228,8 @@ async def test_import_with_api_key(hass):
         'host': '1.2.3.4',
         'port': 80,
         '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,
                           'api_key': '1234567890ABCDEF'}
     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['title'] == 'deCONZ-id'
     assert result['data'] == {
@@ -246,5 +251,6 @@ async def test_options(hass, aioclient_mock):
         'host': '1.2.3.4',
         'port': 80,
         'api_key': '1234567890ABCDEF',
-        'allow_clip_sensor': False
+        'allow_clip_sensor': False,
+        'allow_deconz_groups': False
     }
diff --git a/tests/components/light/test_deconz.py b/tests/components/light/test_deconz.py
index 2608d77ce2a..d7d609f820e 100644
--- a/tests/components/light/test_deconz.py
+++ b/tests/components/light/test_deconz.py
@@ -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."""
     from pydeconz import DeconzSession
     loop = Mock()
@@ -53,7 +53,9 @@ async def setup_bridge(hass, data):
     hass.data[deconz.DATA_DECONZ_UNSUB] = []
     hass.data[deconz.DATA_DECONZ_ID] = {}
     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')
     # To flush out the service call to update the group
     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])
     await hass.async_block_till_done()
     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
-- 
GitLab