diff --git a/homeassistant/components/binary_sensor/__init__.py b/homeassistant/components/binary_sensor/__init__.py index c51b30d8b7c955ce59d67c1ac8c9d71119990cb7..1be1c4423eed0a441053b93e72a82860b96da29c 100644 --- a/homeassistant/components/binary_sensor/__init__.py +++ b/homeassistant/components/binary_sensor/__init__.py @@ -12,7 +12,7 @@ import logging from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity import Entity from homeassistant.const import (STATE_ON, STATE_OFF) -from homeassistant.components import (bloomsky, mysensors) +from homeassistant.components import (bloomsky, mysensors, zwave) DOMAIN = 'binary_sensor' SCAN_INTERVAL = 30 @@ -34,6 +34,7 @@ SENSOR_CLASSES = [ DISCOVERY_PLATFORMS = { bloomsky.DISCOVER_BINARY_SENSORS: 'bloomsky', mysensors.DISCOVER_BINARY_SENSORS: 'mysensors', + zwave.DISCOVER_BINARY_SENSORS: 'zwave', } diff --git a/homeassistant/components/binary_sensor/zwave.py b/homeassistant/components/binary_sensor/zwave.py new file mode 100644 index 0000000000000000000000000000000000000000..eb9f446febef385deab3ce6c86870aedece3edea --- /dev/null +++ b/homeassistant/components/binary_sensor/zwave.py @@ -0,0 +1,70 @@ +""" +homeassistant.components.binary_sensor.zwave +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Interfaces with Z-Wave sensors. + +For more details about this platform, please refer to the documentation +at https://home-assistant.io/components/zwave/ +""" + + +import logging + +from homeassistant.components.zwave import ( + ATTR_NODE_ID, ATTR_VALUE_ID, + COMMAND_CLASS_SENSOR_BINARY, NETWORK, + ZWaveDeviceEntity) +from homeassistant.components.binary_sensor import ( + DOMAIN, + BinarySensorDevice) + +_LOGGER = logging.getLogger(__name__) +DEPENDENCIES = [] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the mysensors platform for sensors.""" + + if discovery_info is None or NETWORK is None: + return + + node = NETWORK.nodes[discovery_info[ATTR_NODE_ID]] + value = node.values[discovery_info[ATTR_VALUE_ID]] + + value.set_change_verified(False) + if value.command_class == COMMAND_CLASS_SENSOR_BINARY: + add_devices([ZWaveBinarySensor(value, "opening")]) + + +class ZWaveBinarySensor(BinarySensorDevice, ZWaveDeviceEntity): + """ Represents a binary sensor within Z-Wave. """ + + def __init__(self, value, sensor_class): + self._sensor_type = sensor_class + # pylint: disable=import-error + from openzwave.network import ZWaveNetwork + from pydispatch import dispatcher + + ZWaveDeviceEntity.__init__(self, value, DOMAIN) + + dispatcher.connect( + self.value_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED) + + @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): + return False + + def value_changed(self, value): + """ Called when a value has changed on the network. """ + if self._value.value_id == value.value_id: + self.update_ha_state() diff --git a/homeassistant/components/sensor/zwave.py b/homeassistant/components/sensor/zwave.py index f011b82b9f3e4a3b7432c03755b9a40fa0993b08..4159b915f26c52e17bc51fede3a42c21fcc051bf 100644 --- a/homeassistant/components/sensor/zwave.py +++ b/homeassistant/components/sensor/zwave.py @@ -14,7 +14,7 @@ import homeassistant.util.dt as dt_util from homeassistant.components.sensor import DOMAIN from homeassistant.components.zwave import ( ATTR_NODE_ID, ATTR_VALUE_ID, COMMAND_CLASS_ALARM, COMMAND_CLASS_METER, - COMMAND_CLASS_SENSOR_BINARY, COMMAND_CLASS_SENSOR_MULTILEVEL, NETWORK, + COMMAND_CLASS_SENSOR_MULTILEVEL, NETWORK, TYPE_DECIMAL, ZWaveDeviceEntity, get_config_value) from homeassistant.const import ( STATE_OFF, STATE_ON, TEMP_CELCIUS, TEMP_FAHRENHEIT) @@ -79,9 +79,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return # generic Device mappings - elif value.command_class == COMMAND_CLASS_SENSOR_BINARY: - add_devices([ZWaveBinarySensor(value)]) - elif value.command_class == COMMAND_CLASS_SENSOR_MULTILEVEL: add_devices([ZWaveMultilevelSensor(value)]) @@ -120,16 +117,6 @@ class ZWaveSensor(ZWaveDeviceEntity, Entity): self.update_ha_state() -# pylint: disable=too-few-public-methods -class ZWaveBinarySensor(ZWaveSensor): - """ Represents a binary sensor within Z-Wave. """ - - @property - def state(self): - """ Returns the state of the sensor. """ - return STATE_ON if self._value.data else STATE_OFF - - class ZWaveTriggerSensor(ZWaveSensor): """ Represents a stateless sensor which diff --git a/homeassistant/components/zwave.py b/homeassistant/components/zwave.py index 49ab2bcaf40e5331cd24fa77c896e357d6ea54dd..80b56b1dac60351e3cb1b71753a9c6ea2a510363 100644 --- a/homeassistant/components/zwave.py +++ b/homeassistant/components/zwave.py @@ -34,6 +34,7 @@ SERVICE_REMOVE_NODE = "remove_node" DISCOVER_SENSORS = "zwave.sensors" DISCOVER_SWITCHES = "zwave.switch" DISCOVER_LIGHTS = "zwave.light" +DISCOVER_BINARY_SENSORS = 'zwave.binary_sensor' EVENT_SCENE_ACTIVATED = "zwave.scene_activated" @@ -54,13 +55,13 @@ TYPE_BYTE = "Byte" TYPE_BOOL = "Bool" TYPE_DECIMAL = "Decimal" + # list of tuple (DOMAIN, discovered service, supported command # classes, value type) DISCOVERY_COMPONENTS = [ ('sensor', DISCOVER_SENSORS, - [COMMAND_CLASS_SENSOR_BINARY, - COMMAND_CLASS_SENSOR_MULTILEVEL, + [COMMAND_CLASS_SENSOR_MULTILEVEL, COMMAND_CLASS_METER, COMMAND_CLASS_ALARM], TYPE_WHATEVER, @@ -75,8 +76,14 @@ DISCOVERY_COMPONENTS = [ [COMMAND_CLASS_SWITCH_BINARY], TYPE_BOOL, GENRE_USER), + ('binary_sensor', + DISCOVER_BINARY_SENSORS, + [COMMAND_CLASS_SENSOR_BINARY], + TYPE_BOOL, + GENRE_USER) ] + ATTR_NODE_ID = "node_id" ATTR_VALUE_ID = "value_id"