2015-11-07 14:52:36 +00:00
|
|
|
"""
|
2015-11-10 19:04:25 +00:00
|
|
|
homeassistant.components.switch.zwave
|
2015-11-07 14:52:36 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-10 19:04:25 +00:00
|
|
|
Zwave platform that handles simple binary switches.
|
2015-11-07 14:52:36 +00:00
|
|
|
"""
|
2015-11-29 21:49:05 +00:00
|
|
|
# Because we do not compile openzwave on CI
|
2015-11-07 14:52:36 +00:00
|
|
|
# pylint: disable=import-error
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
2016-01-27 06:37:41 +00:00
|
|
|
from homeassistant.components.zwave import (
|
2016-02-19 05:27:50 +00:00
|
|
|
ATTR_NODE_ID, ATTR_VALUE_ID, COMMAND_CLASS_SWITCH_BINARY, GENRE_USER,
|
|
|
|
NETWORK, TYPE_BOOL, ZWaveDeviceEntity)
|
2015-11-07 14:52:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Find and return demo switches. """
|
2016-02-19 00:12:00 +00:00
|
|
|
if discovery_info is None or NETWORK is None:
|
2015-11-07 14:52:36 +00:00
|
|
|
return
|
|
|
|
|
2016-01-27 06:37:41 +00:00
|
|
|
node = NETWORK.nodes[discovery_info[ATTR_NODE_ID]]
|
|
|
|
value = node.values[discovery_info[ATTR_VALUE_ID]]
|
2015-11-07 14:52:36 +00:00
|
|
|
|
2016-01-27 06:37:41 +00:00
|
|
|
if value.command_class != COMMAND_CLASS_SWITCH_BINARY:
|
2015-11-07 14:52:36 +00:00
|
|
|
return
|
2016-01-27 06:37:41 +00:00
|
|
|
if value.type != TYPE_BOOL:
|
2015-11-07 14:52:36 +00:00
|
|
|
return
|
2016-01-27 06:37:41 +00:00
|
|
|
if value.genre != GENRE_USER:
|
2015-11-07 14:52:36 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
value.set_change_verified(False)
|
|
|
|
add_devices([ZwaveSwitch(value)])
|
|
|
|
|
|
|
|
|
2016-01-26 21:11:27 +00:00
|
|
|
class ZwaveSwitch(ZWaveDeviceEntity, SwitchDevice):
|
2015-11-07 14:52:36 +00:00
|
|
|
""" Provides a zwave switch. """
|
|
|
|
def __init__(self, value):
|
2015-11-17 08:18:42 +00:00
|
|
|
from openzwave.network import ZWaveNetwork
|
|
|
|
from pydispatch import dispatcher
|
|
|
|
|
2016-01-26 21:11:27 +00:00
|
|
|
ZWaveDeviceEntity.__init__(self, value, DOMAIN)
|
2015-11-07 14:52:36 +00:00
|
|
|
|
|
|
|
self._state = value.data
|
|
|
|
dispatcher.connect(
|
|
|
|
self._value_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED)
|
|
|
|
|
|
|
|
def _value_changed(self, value):
|
|
|
|
""" Called when a value has changed on the network. """
|
|
|
|
if self._value.value_id == value.value_id:
|
|
|
|
self._state = value.data
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if device is on. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
""" Turn the device on. """
|
2016-01-26 21:11:27 +00:00
|
|
|
self._value.node.set_switch(self._value.value_id, True)
|
2015-11-07 14:52:36 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
""" Turn the device off. """
|
2016-01-26 21:11:27 +00:00
|
|
|
self._value.node.set_switch(self._value.value_id, False)
|