core/homeassistant/components/sensor/zwave.py

114 lines
3.3 KiB
Python
Raw Normal View History

2015-02-26 07:27:17 +00:00
"""
Interfaces with Z-Wave sensors.
2015-10-20 19:27:40 +00:00
2015-11-09 12:12:18 +00:00
For more details about this platform, please refer to the documentation
2016-02-23 05:21:49 +00:00
at https://home-assistant.io/components/sensor.zwave/
2015-02-26 07:27:17 +00:00
"""
import logging
2015-11-29 21:49:05 +00:00
# Because we do not compile openzwave on CI
2015-03-01 07:02:26 +00:00
# pylint: disable=import-error
from homeassistant.components.sensor import DOMAIN
from homeassistant.components import zwave
2016-04-20 03:30:44 +00:00
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
_LOGGER = logging.getLogger(__name__)
2015-03-01 09:35:58 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup Z-Wave sensors."""
# Return on empty `discovery_info`. Given you configure HA with:
#
# sensor:
# platform: zwave
#
# `setup_platform` will be called without `discovery_info`.
if discovery_info is None or zwave.NETWORK is None:
return
node = zwave.NETWORK.nodes[discovery_info[zwave.const.ATTR_NODE_ID]]
value = node.values[discovery_info[zwave.const.ATTR_VALUE_ID]]
2015-03-01 09:35:58 +00:00
value.set_change_verified(False)
# if 1 in groups and (NETWORK.controller.node_id not in
2015-10-30 14:28:06 +00:00
# groups[1].associations):
# node.groups[1].add_association(NETWORK.controller.node_id)
2015-10-30 14:28:06 +00:00
2016-02-23 05:21:49 +00:00
# Generic Device mappings
if node.has_command_class(zwave.const.COMMAND_CLASS_SENSOR_MULTILEVEL):
2015-03-15 07:01:18 +00:00
add_devices([ZWaveMultilevelSensor(value)])
2015-03-01 09:35:58 +00:00
elif node.has_command_class(zwave.const.COMMAND_CLASS_METER) and \
value.type == zwave.const.TYPE_DECIMAL:
2015-11-15 16:50:11 +00:00
add_devices([ZWaveMultilevelSensor(value)])
elif node.has_command_class(zwave.const.COMMAND_CLASS_ALARM) or \
node.has_command_class(zwave.const.COMMAND_CLASS_SENSOR_ALARM):
add_devices([ZWaveAlarmSensor(value)])
2015-11-15 16:50:11 +00:00
2015-03-01 09:35:58 +00:00
class ZWaveSensor(zwave.ZWaveDeviceEntity):
2016-03-08 15:46:34 +00:00
"""Representation of a Z-Wave sensor."""
def __init__(self, value):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
zwave.ZWaveDeviceEntity.__init__(self, value, DOMAIN)
2015-03-01 06:49:55 +00:00
@property
def force_update(self):
"""Return force_update."""
return True
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
return self._value.data
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement the value is expressed in."""
2015-02-26 07:27:17 +00:00
return self._value.units
2015-02-26 07:27:17 +00:00
class ZWaveMultilevelSensor(ZWaveSensor):
2016-03-08 15:46:34 +00:00
"""Representation of a multi level sensor Z-Wave sensor."""
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return 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)
2015-03-19 02:15:48 +00:00
elif isinstance(value, float):
return round(value, 2)
2015-02-26 07:27:17 +00:00
return value
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit the value is expressed in."""
unit = self._value.units
if unit == 'C':
2016-04-20 03:30:44 +00:00
return TEMP_CELSIUS
elif unit == 'F':
return TEMP_FAHRENHEIT
else:
2015-02-26 07:27:17 +00:00
return unit
class ZWaveAlarmSensor(ZWaveSensor):
2016-03-08 15:46:34 +00:00
"""Representation of a Z-Wave sensor that sends Alarm alerts.
2016-02-23 05:21:49 +00:00
Examples include certain Multisensors that have motion and vibration
capabilities. Z-Wave defines various alarm types such as Smoke, Flood,
Burglar, CarbonMonoxide, etc.
2016-02-23 05:21:49 +00:00
This wraps these alarms and allows you to use them to trigger things, etc.
COMMAND_CLASS_ALARM is what we get here.
"""
2016-03-08 15:46:34 +00:00
pass