2015-11-07 15:52:36 +01:00
|
|
|
"""
|
2016-07-13 14:47:29 +02:00
|
|
|
Z-Wave platform that handles simple binary switches.
|
2016-03-08 13:35:39 +01:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.zwave/
|
2015-11-07 15:52:36 +01:00
|
|
|
"""
|
2017-01-02 18:55:56 +01:00
|
|
|
import logging
|
2017-03-05 22:55:52 +02:00
|
|
|
import time
|
2015-11-29 13:49:05 -08:00
|
|
|
# Because we do not compile openzwave on CI
|
2015-11-07 15:52:36 +01:00
|
|
|
# pylint: disable=import-error
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
2016-04-17 23:46:51 +02:00
|
|
|
from homeassistant.components import zwave
|
2017-03-05 22:55:52 +02:00
|
|
|
from homeassistant.components.zwave import workaround, async_setup_platform # noqa # pylint: disable=unused-import
|
2015-11-07 15:52:36 +01:00
|
|
|
|
2017-01-02 18:55:56 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-11-07 15:52:36 +01:00
|
|
|
|
2017-03-12 23:35:10 -07:00
|
|
|
def get_device(value, **kwargs):
|
2017-02-23 23:06:28 +02:00
|
|
|
"""Create zwave entity device."""
|
2017-03-12 23:35:10 -07:00
|
|
|
return ZwaveSwitch(value)
|
2015-11-07 15:52:36 +01:00
|
|
|
|
|
|
|
|
2016-04-17 23:46:51 +02:00
|
|
|
class ZwaveSwitch(zwave.ZWaveDeviceEntity, SwitchDevice):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of a Z-Wave switch."""
|
|
|
|
|
2017-03-12 23:35:10 -07:00
|
|
|
def __init__(self, value):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the Z-Wave switch device."""
|
2017-03-12 23:35:10 -07:00
|
|
|
zwave.ZWaveDeviceEntity.__init__(self, value, DOMAIN)
|
|
|
|
self.refresh_on_update = (workaround.get_device_mapping(value) ==
|
|
|
|
workaround.WORKAROUND_REFRESH_NODE_ON_UPDATE)
|
2017-03-05 22:55:52 +02:00
|
|
|
self.last_update = time.perf_counter()
|
2017-03-12 23:35:10 -07:00
|
|
|
self._state = self._value.data
|
2017-02-18 08:56:05 +01:00
|
|
|
|
|
|
|
def update_properties(self):
|
|
|
|
"""Callback on data changes for node values."""
|
2017-03-12 23:35:10 -07:00
|
|
|
self._state = self._value.data
|
2017-03-05 22:55:52 +02:00
|
|
|
if self.refresh_on_update and \
|
|
|
|
time.perf_counter() - self.last_update > 30:
|
|
|
|
self.last_update = time.perf_counter()
|
2017-03-12 23:35:10 -07:00
|
|
|
self._value.node.request_state()
|
2015-11-07 15:52:36 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if device is on."""
|
2017-02-18 08:56:05 +01:00
|
|
|
return self._state
|
2015-11-07 15:52:36 +01:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device on."""
|
2017-03-12 23:35:10 -07:00
|
|
|
self._value.node.set_switch(self._value.value_id, True)
|
2015-11-07 15:52:36 +01:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device off."""
|
2017-03-12 23:35:10 -07:00
|
|
|
self._value.node.set_switch(self._value.value_id, False)
|