core/homeassistant/components/zwave/sensor.py

112 lines
3.3 KiB
Python
Raw Normal View History

"""Support for Z-Wave sensors."""
from homeassistant.components.sensor import DEVICE_CLASS_BATTERY, DOMAIN
2016-04-20 03:30:44 +00:00
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import ZWaveDeviceEntity, const
2019-07-31 19:25:30 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Z-Wave Sensor from Config Entry."""
2019-07-31 19:25:30 +00:00
@callback
def async_add_sensor(sensor):
"""Add Z-Wave Sensor."""
async_add_entities([sensor])
2019-07-31 19:25:30 +00:00
async_dispatcher_connect(hass, "zwave_new_sensor", async_add_sensor)
def get_device(node, values, **kwargs):
"""Create Z-Wave entity device."""
2016-02-23 05:21:49 +00:00
# Generic Device mappings
if values.primary.command_class == const.COMMAND_CLASS_BATTERY:
return ZWaveBatterySensor(values)
2019-03-06 01:17:58 +00:00
if node.has_command_class(const.COMMAND_CLASS_SENSOR_MULTILEVEL):
return ZWaveMultilevelSensor(values)
2019-07-31 19:25:30 +00:00
if (
node.has_command_class(const.COMMAND_CLASS_METER)
and values.primary.type == const.TYPE_DECIMAL
):
return ZWaveMultilevelSensor(values)
2019-07-31 19:25:30 +00:00
if node.has_command_class(const.COMMAND_CLASS_ALARM) or node.has_command_class(
const.COMMAND_CLASS_SENSOR_ALARM
):
return ZWaveAlarmSensor(values)
return None
2015-11-15 16:50:11 +00:00
2015-03-01 09:35:58 +00:00
2019-03-06 01:17:58 +00:00
class ZWaveSensor(ZWaveDeviceEntity):
2016-03-08 15:46:34 +00:00
"""Representation of a Z-Wave sensor."""
def __init__(self, values):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2019-03-06 01:17:58 +00:00
ZWaveDeviceEntity.__init__(self, values, DOMAIN)
self.update_properties()
def update_properties(self):
"""Handle the data changes for node values."""
self._state = self.values.primary.data
self._units = self.values.primary.units
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._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement the value is expressed in."""
return self._units
2015-02-26 07:27:17 +00:00
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."""
2019-07-31 19:25:30 +00:00
if self._units in ("C", "F"):
return round(self._state, 1)
if isinstance(self._state, float):
return round(self._state, 2)
2015-02-26 07:27:17 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit the value is expressed in."""
2019-07-31 19:25:30 +00:00
if self._units == "C":
2016-04-20 03:30:44 +00:00
return TEMP_CELSIUS
2019-07-31 19:25:30 +00:00
if self._units == "F":
return TEMP_FAHRENHEIT
return self._units
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
class ZWaveBatterySensor(ZWaveSensor):
"""Representation of Z-Wave device battery level."""
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_BATTERY