2015-02-26 07:27:17 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.zwave
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Interfaces with Z-Wave sensors.
|
|
|
|
"""
|
2015-02-23 01:36:28 +00:00
|
|
|
import homeassistant.components.zwave as zwave
|
|
|
|
from homeassistant.helpers import Device
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_FRIENDLY_NAME, ATTR_BATTERY_LEVEL, ATTR_UNIT_OF_MEASUREMENT,
|
2015-02-26 07:27:17 +00:00
|
|
|
TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_LOCATION, STATE_ON, STATE_OFF)
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZWaveSensor(Device):
|
2015-02-26 07:27:17 +00:00
|
|
|
""" Represents a Z-Wave sensor. """
|
|
|
|
def __init__(self, sensor_value):
|
|
|
|
self._value = sensor_value
|
|
|
|
self._node = sensor_value.node
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
""" Returns a unique id. """
|
2015-02-26 07:27:17 +00:00
|
|
|
return "ZWAVE-{}-{}".format(self._node.node_id, self._value.object_id)
|
2015-02-23 01:36:28 +00: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 state(self):
|
|
|
|
""" Returns the state of the sensor. """
|
|
|
|
return self._value.data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
""" Returns the state attributes. """
|
|
|
|
attrs = {
|
2015-02-26 07:27:17 +00:00
|
|
|
ATTR_FRIENDLY_NAME: self.name,
|
|
|
|
zwave.ATTR_NODE_ID: self._node.node_id,
|
2015-02-23 01:36:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
battery_level = self._node.get_battery_level()
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
if battery_level is not None:
|
|
|
|
attrs[ATTR_BATTERY_LEVEL] = battery_level
|
|
|
|
|
|
|
|
unit = self.unit
|
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
if unit:
|
2015-02-23 01:36:28 +00:00
|
|
|
attrs[ATTR_UNIT_OF_MEASUREMENT] = unit
|
|
|
|
|
|
|
|
location = self._node.location
|
|
|
|
|
|
|
|
if location:
|
|
|
|
attrs[ATTR_LOCATION] = location
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit(self):
|
|
|
|
""" Unit if sensor has one. """
|
2015-02-26 07:27:17 +00:00
|
|
|
return self._value.units
|
|
|
|
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class ZWaveBinarySensor(ZWaveSensor):
|
|
|
|
""" Represents a binary sensor within Z-Wave. """
|
2015-02-23 01:36:28 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the sensor. """
|
|
|
|
return STATE_ON if self._value.data else STATE_OFF
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
class ZWaveMultilevelSensor(ZWaveSensor):
|
|
|
|
""" Represents a multi level sensor Z-Wave sensor. """
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the sensor. """
|
2015-02-26 07:27:17 +00:00
|
|
|
value = self._value.data
|
|
|
|
|
|
|
|
if self._value.units in ('C', 'F'):
|
|
|
|
return round(value, 1)
|
|
|
|
|
|
|
|
return value
|
2015-02-23 01:36:28 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit(self):
|
|
|
|
""" Unit of this sensor. """
|
|
|
|
unit = self._value.units
|
|
|
|
|
|
|
|
if unit == 'C':
|
|
|
|
return TEMP_CELCIUS
|
|
|
|
elif unit == 'F':
|
|
|
|
return TEMP_FAHRENHEIT
|
|
|
|
else:
|
2015-02-26 07:27:17 +00:00
|
|
|
return unit
|
2015-02-23 08:01:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def devices_discovered(hass, config, info):
|
2015-02-26 07:27:17 +00:00
|
|
|
""" Called when a device is discovered. """
|
|
|
|
node = zwave.NETWORK.nodes[info[zwave.ATTR_NODE_ID]]
|
|
|
|
value = node.values[info[zwave.ATTR_VALUE_ID]]
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
value.set_change_verified(False)
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
if zwave.NETWORK.controller.node_id not in node.groups[1].associations:
|
|
|
|
node.groups[1].add_association(zwave.NETWORK.controller.node_id)
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
if value.command_class == zwave.COMMAND_CLASS_SENSOR_BINARY:
|
|
|
|
return [ZWaveBinarySensor(value)]
|
2015-02-23 08:01:04 +00:00
|
|
|
|
2015-02-26 07:27:17 +00:00
|
|
|
elif value.command_class == zwave.COMMAND_CLASS_SENSOR_MULTILEVEL:
|
|
|
|
return [ZWaveMultilevelSensor(value)]
|