core/homeassistant/components/light/zwave.py

117 lines
3.5 KiB
Python
Raw Normal View History

2015-11-07 14:56:28 +00:00
"""
homeassistant.components.light.zwave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-11-13 07:29:54 +00:00
Support for Z-Wave lights.
2015-11-07 14:56:28 +00:00
2015-11-13 07:29:54 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.zwave/
2015-11-07 14:56:28 +00:00
"""
2015-11-29 21:49:05 +00:00
# Because we do not compile openzwave on CI
2015-11-07 14:56:28 +00:00
# pylint: disable=import-error
2015-11-29 21:49:05 +00:00
from threading import Timer
2015-11-07 14:56:28 +00:00
2016-02-19 05:27:50 +00:00
from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN, Light
from homeassistant.components.zwave import (
2016-02-19 05:27:50 +00:00
ATTR_NODE_ID, ATTR_VALUE_ID, COMMAND_CLASS_SWITCH_MULTILEVEL, GENRE_USER,
NETWORK, TYPE_BYTE, ZWaveDeviceEntity)
from homeassistant.const import STATE_OFF, STATE_ON
2015-11-07 14:56:28 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2015-11-13 07:29:54 +00:00
""" Find and add Z-Wave lights. """
if discovery_info is None or NETWORK is None:
2015-11-07 14:56:28 +00:00
return
node = NETWORK.nodes[discovery_info[ATTR_NODE_ID]]
value = node.values[discovery_info[ATTR_VALUE_ID]]
2015-11-07 14:56:28 +00:00
if value.command_class != COMMAND_CLASS_SWITCH_MULTILEVEL:
2015-11-07 14:56:28 +00:00
return
if value.type != TYPE_BYTE:
2015-11-07 14:56:28 +00:00
return
if value.genre != GENRE_USER:
2015-11-07 14:56:28 +00:00
return
value.set_change_verified(False)
add_devices([ZwaveDimmer(value)])
def brightness_state(value):
2015-11-13 07:29:54 +00:00
"""
Returns the brightness and state according to the current data of given
value.
"""
2015-11-07 14:56:28 +00:00
if value.data > 0:
return (value.data / 99) * 255, STATE_ON
else:
return 255, STATE_OFF
class ZwaveDimmer(ZWaveDeviceEntity, Light):
2015-11-13 07:29:54 +00:00
""" Provides a Z-Wave dimmer. """
2015-11-07 14:56:28 +00:00
# pylint: disable=too-many-arguments
def __init__(self, value):
2015-11-17 08:18:42 +00:00
from openzwave.network import ZWaveNetwork
from pydispatch import dispatcher
ZWaveDeviceEntity.__init__(self, value, DOMAIN)
2015-11-07 14:56:28 +00:00
self._brightness, self._state = brightness_state(value)
# Used for value change event handling
self._refreshing = False
self._timer = None
2015-11-07 14:56:28 +00:00
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:
return
if self._refreshing:
self._refreshing = False
self._brightness, self._state = brightness_state(value)
else:
def _refresh_value():
"""Used timer callback for delayed value refresh."""
2015-11-07 14:56:28 +00:00
self._refreshing = True
self._value.refresh()
if self._timer is not None and self._timer.isAlive():
self._timer.cancel()
self._timer = Timer(2, _refresh_value)
self._timer.start()
self.update_ha_state()
2015-11-07 14:56:28 +00:00
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
@property
def is_on(self):
""" True if device is on. """
return self._state == STATE_ON
def turn_on(self, **kwargs):
""" Turn the device on. """
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
# Zwave multilevel switches use a range of [0, 99] to control
# brightness.
2015-11-07 14:56:28 +00:00
brightness = (self._brightness / 255) * 99
if self._value.node.set_dimmer(self._value.value_id, brightness):
2015-11-07 14:56:28 +00:00
self._state = STATE_ON
def turn_off(self, **kwargs):
""" Turn the device off. """
if self._value.node.set_dimmer(self._value.value_id, 0):
2015-11-07 14:56:28 +00:00
self._state = STATE_OFF