hass-core/homeassistant/components/light/zwave.py

150 lines
4.4 KiB
Python
Raw Normal View History

2015-11-07 15:56:28 +01:00
"""
homeassistant.components.light.zwave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-11-13 08:29:54 +01:00
Support for Z-Wave lights.
2015-11-07 15:56:28 +01:00
2015-11-13 08:29:54 +01:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.zwave/
2015-11-07 15:56:28 +01:00
"""
2015-11-29 13:49:05 -08:00
# Because we do not compile openzwave on CI
2015-11-07 15:56:28 +01:00
# pylint: disable=import-error
2015-11-29 13:49:05 -08:00
from threading import Timer
2015-11-07 15:56:28 +01:00
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.light import (Light, ATTR_BRIGHTNESS)
from homeassistant.util import slugify
2015-11-29 13:49:05 -08:00
import homeassistant.components.zwave as zwave
2015-11-07 15:56:28 +01:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2015-11-13 08:29:54 +01:00
""" Find and add Z-Wave lights. """
2015-11-07 15:56:28 +01:00
if discovery_info is None:
return
node = zwave.NETWORK.nodes[discovery_info[zwave.ATTR_NODE_ID]]
value = node.values[discovery_info[zwave.ATTR_VALUE_ID]]
if value.command_class != zwave.COMMAND_CLASS_SWITCH_MULTILEVEL:
return
if value.type != zwave.TYPE_BYTE:
return
if value.genre != zwave.GENRE_USER:
return
value.set_change_verified(False)
add_devices([ZwaveDimmer(value)])
def brightness_state(value):
2015-11-13 08:29:54 +01:00
"""
Returns the brightness and state according to the current data of given
value.
"""
2015-11-07 15:56:28 +01:00
if value.data > 0:
return (value.data / 99) * 255, STATE_ON
else:
return 255, STATE_OFF
class ZwaveDimmer(Light):
2015-11-13 08:29:54 +01:00
""" Provides a Z-Wave dimmer. """
2015-11-07 15:56:28 +01:00
# pylint: disable=too-many-arguments
def __init__(self, value):
2015-11-17 00:18:42 -08:00
from openzwave.network import ZWaveNetwork
from pydispatch import dispatcher
2015-11-07 15:56:28 +01:00
self._value = value
self._node = value.node
self._brightness, self._state = brightness_state(value)
# Used for value change event handling
self._refreshing = False
self._timer = None
2015-11-07 15:56:28 +01: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 15:56:28 +01: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 15:56:28 +01:00
@property
def should_poll(self):
""" No polling needed for a light. """
return False
@property
def unique_id(self):
""" Returns a unique id. """
return "ZWAVE-{}-{}".format(self._node.node_id, self._value.object_id)
2015-11-07 15:56:28 +01:00
@property
def name(self):
""" Returns the name of the device. """
name = self._node.name or "{} {}".format(
self._node.manufacturer_name, self._node.product_name)
return "{} {}".format(name, self._value.label)
@property
def entity_id(self):
""" Returns the entity_id of the device if any.
The entity_id contains node_id and value instance id
to not collide with other entity_ids"""
entity_id = "light.{}_{}".format(slugify(self.name),
self._node.node_id)
# Add the instance id if there is more than one instance for the value
if self._value.instance > 1:
return "{}_{}".format(entity_id, self._value.instance)
2015-11-07 15:56:28 +01:00
return entity_id
2015-11-07 15:56:28 +01: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 15:56:28 +01:00
brightness = (self._brightness / 255) * 99
if self._node.set_dimmer(self._value.value_id, brightness):
self._state = STATE_ON
def turn_off(self, **kwargs):
""" Turn the device off. """
if self._node.set_dimmer(self._value.value_id, 0):
self._state = STATE_OFF