From 50619bd3602074b9ee44337c07e0a99801024901 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 8 Aug 2018 23:15:32 +0200 Subject: [PATCH 1/4] Add support for sirenes --- homeassistant/components/deconz/const.py | 4 +- homeassistant/components/switch/deconz.py | 53 +++++++++++++++++++---- tests/components/switch/test_deconz.py | 6 +++ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py index 7e16a9d7f107e0..c21c9ee7925b51 100644 --- a/homeassistant/components/deconz/const.py +++ b/homeassistant/components/deconz/const.py @@ -15,4 +15,6 @@ ATTR_DARK = 'dark' ATTR_ON = 'on' -SWITCH_TYPES = ["On/Off plug-in unit", "Smart plug"] +POWER_PLUGS = ["On/Off plug-in unit", "Smart plug"] +SIRENES = ["Warning device"] +SWITCH_TYPES = POWER_PLUGS + SIRENES diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 95e7d7367392db..fceeb08d0c09f2 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -5,7 +5,8 @@ https://home-assistant.io/components/switch.deconz/ """ from homeassistant.components.deconz.const import ( - DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB, SWITCH_TYPES) + DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB, + POWER_PLUGS, SIRENES) from homeassistant.components.switch import SwitchDevice from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -29,8 +30,10 @@ def async_add_switch(lights): """Add switch from deCONZ.""" entities = [] for light in lights: - if light.type in SWITCH_TYPES: - entities.append(DeconzSwitch(light)) + if light.type in POWER_PLUGS: + entities.append(DeconzPowerPlug(light)) + elif light.type in SIRENES: + entities.append(DeconzSirene(light)) async_add_devices(entities, True) hass.data[DATA_DECONZ_UNSUB].append( @@ -56,11 +59,6 @@ def async_update_callback(self, reason): """Update the switch's state.""" self.async_schedule_update_ha_state() - @property - def is_on(self): - """Return true if switch is on.""" - return self._switch.state - @property def name(self): """Return the name of the switch.""" @@ -71,6 +69,25 @@ def unique_id(self): """Return a unique identifier for this switch.""" return self._switch.uniqueid + @property + def available(self): + """Return True if light is available.""" + return self._switch.reachable + + @property + def should_poll(self): + """No polling needed.""" + return False + + +class DeconzPowerPlug(DeconzSwitch): + """Representation of power plugs from deCONZ.""" + + @property + def is_on(self): + """Return true if switch is on.""" + return self._switch.state + async def async_turn_on(self, **kwargs): """Turn on switch.""" data = {'on': True} @@ -80,3 +97,23 @@ async def async_turn_off(self, **kwargs): """Turn off switch.""" data = {'on': False} await self._switch.async_set_state(data) + + +class DeconzSirene(DeconzSwitch): + """Representation of sirene from deCONZ.""" + + @property + def is_on(self): + """Return true if switch is on.""" + return self._switch.state + + async def async_turn_on(self, **kwargs): + """Turn on switch.""" + data['alert'] = 'lselect' + data = {'alert': 'lselect'} + await self._switch.async_set_state(data) + + async def async_turn_off(self, **kwargs): + """Turn off switch.""" + data = {'on': False} + await self._switch.async_set_state(data) diff --git a/tests/components/switch/test_deconz.py b/tests/components/switch/test_deconz.py index 490a0e67c9dd59..0dcf0435c91df9 100644 --- a/tests/components/switch/test_deconz.py +++ b/tests/components/switch/test_deconz.py @@ -20,6 +20,12 @@ "name": "Switch 2 name", "type": "Smart plug", "state": {} + }, + "3": { + "id": "Switch 3 id", + "name": "Switch 3 name", + "type": "Warning device", + "state": {} } } From 5f9c3eb54376f46b745a4cac6ba826b74fe20649 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 8 Aug 2018 23:20:35 +0200 Subject: [PATCH 2/4] Too quick... --- homeassistant/components/switch/deconz.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index fceeb08d0c09f2..f63e7979200e46 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -109,11 +109,10 @@ def is_on(self): async def async_turn_on(self, **kwargs): """Turn on switch.""" - data['alert'] = 'lselect' data = {'alert': 'lselect'} await self._switch.async_set_state(data) async def async_turn_off(self, **kwargs): """Turn off switch.""" - data = {'on': False} + data = {'alert': 'none'} await self._switch.async_set_state(data) From 335d7267b197e460d943bcd0c3a9a704a08a7ae5 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 9 Aug 2018 09:23:27 +0200 Subject: [PATCH 3/4] Fix test --- homeassistant/components/switch/deconz.py | 2 +- tests/components/switch/test_deconz.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index f63e7979200e46..341fe5ae64fadc 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -100,7 +100,7 @@ async def async_turn_off(self, **kwargs): class DeconzSirene(DeconzSwitch): - """Representation of sirene from deCONZ.""" + """Representation of sirenes from deCONZ.""" @property def is_on(self): diff --git a/tests/components/switch/test_deconz.py b/tests/components/switch/test_deconz.py index 0dcf0435c91df9..57fc8b3bcd9c23 100644 --- a/tests/components/switch/test_deconz.py +++ b/tests/components/switch/test_deconz.py @@ -73,8 +73,9 @@ async def test_switch(hass): await setup_bridge(hass, {"lights": SUPPORTED_SWITCHES}) assert "switch.switch_1_name" in hass.data[deconz.DATA_DECONZ_ID] assert "switch.switch_2_name" in hass.data[deconz.DATA_DECONZ_ID] + assert "switch.switch_3_name" in hass.data[deconz.DATA_DECONZ_ID] assert len(SUPPORTED_SWITCHES) == len(SWITCH_TYPES) - assert len(hass.states.async_all()) == 3 + assert len(hass.states.async_all()) == 4 async def test_add_new_switch(hass): From add5d909d5dc1d9910153354870d8b79cd53a2fd Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 9 Aug 2018 13:41:15 +0200 Subject: [PATCH 4/4] Use siren instead of sirene --- homeassistant/components/deconz/const.py | 4 ++-- homeassistant/components/switch/deconz.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/deconz/const.py b/homeassistant/components/deconz/const.py index c21c9ee7925b51..e7bc5605aee400 100644 --- a/homeassistant/components/deconz/const.py +++ b/homeassistant/components/deconz/const.py @@ -16,5 +16,5 @@ ATTR_ON = 'on' POWER_PLUGS = ["On/Off plug-in unit", "Smart plug"] -SIRENES = ["Warning device"] -SWITCH_TYPES = POWER_PLUGS + SIRENES +SIRENS = ["Warning device"] +SWITCH_TYPES = POWER_PLUGS + SIRENS diff --git a/homeassistant/components/switch/deconz.py b/homeassistant/components/switch/deconz.py index 341fe5ae64fadc..d5fb22e97c467f 100644 --- a/homeassistant/components/switch/deconz.py +++ b/homeassistant/components/switch/deconz.py @@ -6,7 +6,7 @@ """ from homeassistant.components.deconz.const import ( DOMAIN as DATA_DECONZ, DATA_DECONZ_ID, DATA_DECONZ_UNSUB, - POWER_PLUGS, SIRENES) + POWER_PLUGS, SIRENS) from homeassistant.components.switch import SwitchDevice from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -32,8 +32,8 @@ def async_add_switch(lights): for light in lights: if light.type in POWER_PLUGS: entities.append(DeconzPowerPlug(light)) - elif light.type in SIRENES: - entities.append(DeconzSirene(light)) + elif light.type in SIRENS: + entities.append(DeconzSiren(light)) async_add_devices(entities, True) hass.data[DATA_DECONZ_UNSUB].append( @@ -99,13 +99,13 @@ async def async_turn_off(self, **kwargs): await self._switch.async_set_state(data) -class DeconzSirene(DeconzSwitch): - """Representation of sirenes from deCONZ.""" +class DeconzSiren(DeconzSwitch): + """Representation of sirens from deCONZ.""" @property def is_on(self): """Return true if switch is on.""" - return self._switch.state + return self._switch.alert == 'lselect' async def async_turn_on(self, **kwargs): """Turn on switch."""