2016-02-20 22:24:45 +02:00
|
|
|
"""
|
|
|
|
Interfaces with Z-Wave sensors.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation
|
2016-02-22 10:11:46 +01:00
|
|
|
https://home-assistant.io/components/binary_sensor.zwave/
|
2016-02-20 22:24:45 +02:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-24 10:28:49 +01:00
|
|
|
import datetime
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from homeassistant.helpers.event import track_point_in_time
|
2016-04-17 23:46:51 +02:00
|
|
|
from homeassistant.components import zwave
|
2017-02-08 06:37:11 +02:00
|
|
|
from homeassistant.components.zwave import workaround
|
2016-02-20 22:24:45 +02:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DOMAIN,
|
|
|
|
BinarySensorDevice)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-07-13 14:47:29 +02:00
|
|
|
"""Setup the Z-Wave platform for binary sensors."""
|
2016-04-17 23:46:51 +02:00
|
|
|
if discovery_info is None or zwave.NETWORK is None:
|
2016-02-20 22:24:45 +02:00
|
|
|
return
|
|
|
|
|
2016-09-30 17:43:18 +02:00
|
|
|
node = zwave.NETWORK.nodes[discovery_info[zwave.const.ATTR_NODE_ID]]
|
|
|
|
value = node.values[discovery_info[zwave.const.ATTR_VALUE_ID]]
|
2016-02-20 22:24:45 +02:00
|
|
|
value.set_change_verified(False)
|
2016-02-29 11:05:11 +01:00
|
|
|
|
2017-02-08 06:37:11 +02:00
|
|
|
device_mapping = workaround.get_device_mapping(value)
|
|
|
|
if device_mapping == workaround.WORKAROUND_NO_OFF_EVENT:
|
|
|
|
# Default the multiplier to 4
|
|
|
|
re_arm_multiplier = (zwave.get_config_value(value.node, 9) or 4)
|
|
|
|
add_devices([
|
|
|
|
ZWaveTriggerSensor(value, "motion",
|
|
|
|
hass, re_arm_multiplier * 8)
|
|
|
|
])
|
|
|
|
return
|
2016-02-29 11:05:11 +01:00
|
|
|
|
2017-02-08 06:37:11 +02:00
|
|
|
if workaround.get_device_component_mapping(value) == DOMAIN:
|
|
|
|
add_devices([ZWaveBinarySensor(value, None)])
|
|
|
|
return
|
2016-02-29 11:05:11 +01:00
|
|
|
|
2016-09-30 17:43:18 +02:00
|
|
|
if value.command_class == zwave.const.COMMAND_CLASS_SENSOR_BINARY:
|
2016-02-24 22:44:49 +01:00
|
|
|
add_devices([ZWaveBinarySensor(value, None)])
|
2016-02-20 22:24:45 +02:00
|
|
|
|
|
|
|
|
2017-01-27 01:21:33 -05:00
|
|
|
class ZWaveBinarySensor(BinarySensorDevice, zwave.ZWaveDeviceEntity):
|
2016-03-09 11:15:04 +01:00
|
|
|
"""Representation of a binary sensor within Z-Wave."""
|
2016-02-20 22:24:45 +02:00
|
|
|
|
|
|
|
def __init__(self, value, sensor_class):
|
2016-03-09 11:15:04 +01:00
|
|
|
"""Initialize the sensor."""
|
2016-02-20 22:24:45 +02:00
|
|
|
self._sensor_type = sensor_class
|
2016-04-17 23:46:51 +02:00
|
|
|
zwave.ZWaveDeviceEntity.__init__(self, value, DOMAIN)
|
2016-02-20 22:24:45 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if the binary sensor is on."""
|
|
|
|
return self._value.data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def sensor_class(self):
|
|
|
|
"""Return the class of this sensor, from SENSOR_CLASSES."""
|
|
|
|
return self._sensor_type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-22 10:11:46 +01:00
|
|
|
"""No polling needed."""
|
2016-02-20 22:24:45 +02:00
|
|
|
return False
|
|
|
|
|
2016-02-24 10:28:49 +01:00
|
|
|
|
2017-01-27 01:21:33 -05:00
|
|
|
class ZWaveTriggerSensor(ZWaveBinarySensor):
|
2016-03-09 11:15:04 +01:00
|
|
|
"""Representation of a stateless sensor within Z-Wave."""
|
2016-02-24 10:28:49 +01:00
|
|
|
|
2017-01-27 01:21:33 -05:00
|
|
|
def __init__(self, value, sensor_class, hass, re_arm_sec=60):
|
2016-03-09 11:15:04 +01:00
|
|
|
"""Initialize the sensor."""
|
2017-01-27 01:21:33 -05:00
|
|
|
super(ZWaveTriggerSensor, self).__init__(value, sensor_class)
|
2016-02-24 10:28:49 +01:00
|
|
|
self._hass = hass
|
|
|
|
self.re_arm_sec = re_arm_sec
|
2016-02-24 22:44:49 +01:00
|
|
|
self.invalidate_after = dt_util.utcnow() + datetime.timedelta(
|
|
|
|
seconds=self.re_arm_sec)
|
|
|
|
# If it's active make sure that we set the timeout tracker
|
2017-01-27 01:21:33 -05:00
|
|
|
if value.data:
|
2016-02-24 22:44:49 +01:00
|
|
|
track_point_in_time(
|
2016-11-17 07:34:46 -08:00
|
|
|
self._hass, self.async_update_ha_state,
|
2016-02-24 22:44:49 +01:00
|
|
|
self.invalidate_after)
|
2016-02-24 10:28:49 +01:00
|
|
|
|
|
|
|
def value_changed(self, value):
|
2017-01-27 01:21:33 -05:00
|
|
|
"""Called when a value for this entity's node has changed."""
|
2016-02-24 10:28:49 +01:00
|
|
|
if self._value.value_id == value.value_id:
|
2016-11-17 07:34:46 -08:00
|
|
|
self.schedule_update_ha_state()
|
2016-02-24 10:28:49 +01:00
|
|
|
if value.data:
|
2016-02-24 22:44:49 +01:00
|
|
|
# only allow this value to be true for re_arm secs
|
2016-02-24 10:28:49 +01:00
|
|
|
self.invalidate_after = dt_util.utcnow() + datetime.timedelta(
|
|
|
|
seconds=self.re_arm_sec)
|
|
|
|
track_point_in_time(
|
2016-11-17 07:34:46 -08:00
|
|
|
self._hass, self.async_update_ha_state,
|
2016-02-24 10:28:49 +01:00
|
|
|
self.invalidate_after)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-02-24 22:44:49 +01:00
|
|
|
"""Return True if movement has happened within the rearm time."""
|
|
|
|
return self._value.data and \
|
|
|
|
(self.invalidate_after is None or
|
|
|
|
self.invalidate_after > dt_util.utcnow())
|