Skip to content
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 - Add support for sirens #15896

Merged
merged 4 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion homeassistant/components/deconz/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
SIRENS = ["Warning device"]
SWITCH_TYPES = POWER_PLUGS + SIRENS
52 changes: 44 additions & 8 deletions homeassistant/components/switch/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, SIRENS)
from homeassistant.components.switch import SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand All @@ -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 SIRENS:
entities.append(DeconzSiren(light))
async_add_devices(entities, True)

hass.data[DATA_DECONZ_UNSUB].append(
Expand All @@ -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."""
Expand All @@ -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}
Expand All @@ -80,3 +97,22 @@ async def async_turn_off(self, **kwargs):
"""Turn off switch."""
data = {'on': False}
await self._switch.async_set_state(data)


class DeconzSiren(DeconzSwitch):
"""Representation of sirens from deCONZ."""

@property
def is_on(self):
"""Return true if switch is on."""
return self._switch.alert == 'lselect'

async def async_turn_on(self, **kwargs):
"""Turn on switch."""
data = {'alert': 'lselect'}
await self._switch.async_set_state(data)

async def async_turn_off(self, **kwargs):
"""Turn off switch."""
data = {'alert': 'none'}
await self._switch.async_set_state(data)
9 changes: 8 additions & 1 deletion tests/components/switch/test_deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
}

Expand Down Expand Up @@ -67,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):
Expand Down