2016-11-28 05:55:26 +00:00
|
|
|
"""
|
|
|
|
Support for monitoring if a sensor value is below/above a threshold.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.threshold/
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2017-12-19 00:52:19 +00:00
|
|
|
DEVICE_CLASSES_SCHEMA, PLATFORM_SCHEMA, BinarySensorDevice)
|
2016-11-28 05:55:26 +00:00
|
|
|
from homeassistant.const import (
|
2017-12-19 00:52:19 +00:00
|
|
|
ATTR_ENTITY_ID, CONF_DEVICE_CLASS, CONF_ENTITY_ID, CONF_NAME,
|
|
|
|
STATE_UNKNOWN)
|
2016-11-28 05:55:26 +00:00
|
|
|
from homeassistant.core import callback
|
2017-12-19 00:52:19 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-11-28 05:55:26 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-10-02 15:15:19 +00:00
|
|
|
ATTR_HYSTERESIS = 'hysteresis'
|
2017-12-19 00:52:19 +00:00
|
|
|
ATTR_LOWER = 'lower'
|
|
|
|
ATTR_POSITION = 'position'
|
2016-11-28 05:55:26 +00:00
|
|
|
ATTR_SENSOR_VALUE = 'sensor_value'
|
|
|
|
ATTR_TYPE = 'type'
|
2017-12-19 00:52:19 +00:00
|
|
|
ATTR_UPPER = 'upper'
|
2016-11-28 05:55:26 +00:00
|
|
|
|
2017-10-02 15:15:19 +00:00
|
|
|
CONF_HYSTERESIS = 'hysteresis'
|
2016-11-28 05:55:26 +00:00
|
|
|
CONF_LOWER = 'lower'
|
|
|
|
CONF_UPPER = 'upper'
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'Threshold'
|
2017-10-02 15:15:19 +00:00
|
|
|
DEFAULT_HYSTERESIS = 0.0
|
2016-11-28 05:55:26 +00:00
|
|
|
|
2017-12-19 00:52:19 +00:00
|
|
|
POSITION_ABOVE = 'above'
|
|
|
|
POSITION_BELOW = 'below'
|
|
|
|
POSITION_IN_RANGE = 'in_range'
|
|
|
|
POSITION_UNKNOWN = 'unknown'
|
|
|
|
|
|
|
|
TYPE_LOWER = 'lower'
|
|
|
|
TYPE_RANGE = 'range'
|
|
|
|
TYPE_UPPER = 'upper'
|
2016-11-28 05:55:26 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
2017-02-11 04:46:15 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2017-12-19 00:52:19 +00:00
|
|
|
vol.Optional(CONF_HYSTERESIS, default=DEFAULT_HYSTERESIS):
|
|
|
|
vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_LOWER): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_UPPER): vol.Coerce(float),
|
2016-11-28 05:55:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|
|
|
"""Set up the Threshold sensor."""
|
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
name = config.get(CONF_NAME)
|
2017-12-19 00:52:19 +00:00
|
|
|
lower = config.get(CONF_LOWER)
|
|
|
|
upper = config.get(CONF_UPPER)
|
2017-10-02 15:15:19 +00:00
|
|
|
hysteresis = config.get(CONF_HYSTERESIS)
|
2017-07-29 23:46:27 +00:00
|
|
|
device_class = config.get(CONF_DEVICE_CLASS)
|
2016-11-28 05:55:26 +00:00
|
|
|
|
2017-10-02 15:15:19 +00:00
|
|
|
async_add_devices([ThresholdSensor(
|
2017-12-19 00:52:19 +00:00
|
|
|
hass, entity_id, name, lower, upper, hysteresis, device_class)], True)
|
2016-11-28 05:55:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ThresholdSensor(BinarySensorDevice):
|
|
|
|
"""Representation of a Threshold sensor."""
|
|
|
|
|
2017-12-19 00:52:19 +00:00
|
|
|
def __init__(self, hass, entity_id, name, lower, upper, hysteresis,
|
|
|
|
device_class):
|
2016-11-28 05:55:26 +00:00
|
|
|
"""Initialize the Threshold sensor."""
|
|
|
|
self._hass = hass
|
|
|
|
self._entity_id = entity_id
|
|
|
|
self._name = name
|
2017-12-19 00:52:19 +00:00
|
|
|
self._threshold_lower = lower
|
|
|
|
self._threshold_upper = upper
|
2017-10-02 15:15:19 +00:00
|
|
|
self._hysteresis = hysteresis
|
2017-02-11 04:46:15 +00:00
|
|
|
self._device_class = device_class
|
2017-12-19 00:52:19 +00:00
|
|
|
|
|
|
|
self._state_position = None
|
2017-10-02 15:15:19 +00:00
|
|
|
self._state = False
|
2017-12-19 00:52:19 +00:00
|
|
|
self.sensor_value = None
|
2016-11-28 05:55:26 +00:00
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
2017-12-19 00:52:19 +00:00
|
|
|
@callback
|
2016-11-28 05:55:26 +00:00
|
|
|
def async_threshold_sensor_state_listener(
|
|
|
|
entity, old_state, new_state):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Handle sensor state changes."""
|
2016-11-28 05:55:26 +00:00
|
|
|
try:
|
2017-12-19 00:52:19 +00:00
|
|
|
self.sensor_value = None if new_state.state == STATE_UNKNOWN \
|
|
|
|
else float(new_state.state)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
self.sensor_value = None
|
|
|
|
_LOGGER.warning("State is not numerical")
|
2016-11-28 05:55:26 +00:00
|
|
|
|
|
|
|
hass.async_add_job(self.async_update_ha_state, True)
|
|
|
|
|
|
|
|
async_track_state_change(
|
|
|
|
hass, entity_id, async_threshold_sensor_state_listener)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
2017-10-02 15:15:19 +00:00
|
|
|
return self._state
|
2016-11-28 05:55:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
2016-11-28 05:55:26 +00:00
|
|
|
"""Return the sensor class of the sensor."""
|
2017-02-11 04:46:15 +00:00
|
|
|
return self._device_class
|
2016-11-28 05:55:26 +00:00
|
|
|
|
2017-12-19 00:52:19 +00:00
|
|
|
@property
|
|
|
|
def threshold_type(self):
|
|
|
|
"""Return the type of threshold this sensor represents."""
|
2018-02-06 18:32:56 +00:00
|
|
|
if self._threshold_lower is not None and \
|
|
|
|
self._threshold_upper is not None:
|
2017-12-19 00:52:19 +00:00
|
|
|
return TYPE_RANGE
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._threshold_lower is not None:
|
2017-12-19 00:52:19 +00:00
|
|
|
return TYPE_LOWER
|
2018-07-23 08:16:05 +00:00
|
|
|
if self._threshold_upper is not None:
|
2017-12-19 00:52:19 +00:00
|
|
|
return TYPE_UPPER
|
|
|
|
|
2016-11-28 05:55:26 +00:00
|
|
|
@property
|
2017-01-23 21:21:12 +00:00
|
|
|
def device_state_attributes(self):
|
2016-11-28 05:55:26 +00:00
|
|
|
"""Return the state attributes of the sensor."""
|
|
|
|
return {
|
|
|
|
ATTR_ENTITY_ID: self._entity_id,
|
2017-10-02 15:15:19 +00:00
|
|
|
ATTR_HYSTERESIS: self._hysteresis,
|
2017-12-19 00:52:19 +00:00
|
|
|
ATTR_LOWER: self._threshold_lower,
|
|
|
|
ATTR_POSITION: self._state_position,
|
|
|
|
ATTR_SENSOR_VALUE: self.sensor_value,
|
|
|
|
ATTR_TYPE: self.threshold_type,
|
|
|
|
ATTR_UPPER: self._threshold_upper,
|
2016-11-28 05:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
2017-12-19 00:52:19 +00:00
|
|
|
def below(threshold):
|
|
|
|
"""Determine if the sensor value is below a threshold."""
|
|
|
|
return self.sensor_value < (threshold - self._hysteresis)
|
|
|
|
|
|
|
|
def above(threshold):
|
|
|
|
"""Determine if the sensor value is above a threshold."""
|
|
|
|
return self.sensor_value > (threshold + self._hysteresis)
|
|
|
|
|
|
|
|
if self.sensor_value is None:
|
|
|
|
self._state_position = POSITION_UNKNOWN
|
2017-10-02 15:15:19 +00:00
|
|
|
self._state = False
|
2017-12-19 00:52:19 +00:00
|
|
|
|
|
|
|
elif self.threshold_type == TYPE_LOWER:
|
|
|
|
if below(self._threshold_lower):
|
|
|
|
self._state_position = POSITION_BELOW
|
|
|
|
self._state = True
|
|
|
|
elif above(self._threshold_lower):
|
|
|
|
self._state_position = POSITION_ABOVE
|
|
|
|
self._state = False
|
|
|
|
|
|
|
|
elif self.threshold_type == TYPE_UPPER:
|
|
|
|
if above(self._threshold_upper):
|
|
|
|
self._state_position = POSITION_ABOVE
|
|
|
|
self._state = True
|
|
|
|
elif below(self._threshold_upper):
|
|
|
|
self._state_position = POSITION_BELOW
|
|
|
|
self._state = False
|
|
|
|
|
|
|
|
elif self.threshold_type == TYPE_RANGE:
|
|
|
|
if below(self._threshold_lower):
|
|
|
|
self._state_position = POSITION_BELOW
|
|
|
|
self._state = False
|
|
|
|
if above(self._threshold_upper):
|
|
|
|
self._state_position = POSITION_ABOVE
|
|
|
|
self._state = False
|
|
|
|
elif above(self._threshold_lower) and below(self._threshold_upper):
|
|
|
|
self._state_position = POSITION_IN_RANGE
|
|
|
|
self._state = True
|