2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ switches."""
|
2018-08-01 09:03:08 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .const import NEW_LIGHT, POWER_PLUGS, SIRENS
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2019-04-05 00:48:24 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-16 07:33:04 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-27 04:56:00 +00:00
|
|
|
"""Old way of setting up deCONZ platforms."""
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Set up switches for deCONZ component.
|
|
|
|
|
|
|
|
Switches are based same device class as lights in deCONZ.
|
|
|
|
"""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
@callback
|
|
|
|
def async_add_switch(lights):
|
|
|
|
"""Add switch from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
for light in lights:
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-10 17:22:12 +00:00
|
|
|
if light.type in POWER_PLUGS:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzPowerPlug(light, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-10 17:22:12 +00:00
|
|
|
elif light.type in SIRENS:
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzSiren(light, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_switch
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_switch(gateway.api.lights.values())
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzPowerPlug(DeconzDevice, SwitchDevice):
|
|
|
|
"""Representation of a deCONZ power plug."""
|
2018-08-10 17:22:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.state
|
2018-08-10 17:22:12 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn on switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": True}
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn off switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": False}
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-08-10 17:22:12 +00:00
|
|
|
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
class DeconzSiren(DeconzDevice, SwitchDevice):
|
|
|
|
"""Representation of a deCONZ siren."""
|
2018-08-10 17:22:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._device.alert == "lselect"
|
2018-08-10 17:22:12 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn on switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"alert": "lselect"}
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-08-10 17:22:12 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn off switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"alert": "none"}
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|