Extend Threshold binary sensor to support ranges (#11110)

* Extend Threshold binary sensor to support ranges

- Adds support for ranges
- Threshold type (lower, upper, range) is defined by supplied
  thresholds (lower, upper)
- Adds verbose status/position relative to threshold as attribute
  (position)

* Minor changes (ordering, names, etc.)

* Update name

* Update name
This commit is contained in:
Dan Nixon authored and Fabian Affolter committed 2017-12-19 01:52:19 +01:00
1 parent ef22a6e18d
commit 200c927087
2 files changed
+302 -66

No files matched your search

+213 -26
View File
@@ -2,7 +2,8 @@
import unittest
from homeassistant.setup import setup_component
from homeassistant.const import (ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS)
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, TEMP_CELSIUS)
from tests.common import get_test_home_assistant
@@ -23,8 +24,7 @@ class TestThresholdSensor(unittest.TestCase):
config = {
'binary_sensor': {
'platform': 'threshold',
'threshold': '15',
'type': 'upper',
'upper': '15',
'entity_id': 'sensor.test_monitored',
}
}
@@ -37,12 +37,14 @@ class TestThresholdSensor(unittest.TestCase):
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('upper', state.attributes.get('type'))
self.assertEqual('sensor.test_monitored',
state.attributes.get('entity_id'))
self.assertEqual(16, state.attributes.get('sensor_value'))
self.assertEqual(float(config['binary_sensor']['threshold']),
state.attributes.get('threshold'))
self.assertEqual('above', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['upper']),
state.attributes.get('upper'))
self.assertEqual(0.0, state.attributes.get('hysteresis'))
self.assertEqual('upper', state.attributes.get('type'))
assert state.state == 'on'
@@ -65,9 +67,7 @@ class TestThresholdSensor(unittest.TestCase):
config = {
'binary_sensor': {
'platform': 'threshold',
'threshold': '15',
'name': 'Test_threshold',
'type': 'lower',
'lower': '15',
'entity_id': 'sensor.test_monitored',
}
}
@@ -77,8 +77,12 @@ class TestThresholdSensor(unittest.TestCase):
self.hass.states.set('sensor.test_monitored', 16)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('above', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['lower']),
state.attributes.get('lower'))
self.assertEqual(0.0, state.attributes.get('hysteresis'))
self.assertEqual('lower', state.attributes.get('type'))
assert state.state == 'off'
@@ -86,26 +90,17 @@ class TestThresholdSensor(unittest.TestCase):
self.hass.states.set('sensor.test_monitored', 14)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 15)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
assert state.state == 'off'
def test_sensor_hysteresis(self):
"""Test if source is above threshold using hysteresis."""
config = {
'binary_sensor': {
'platform': 'threshold',
'threshold': '15',
'upper': '15',
'hysteresis': '2.5',
'name': 'Test_threshold',
'type': 'upper',
'entity_id': 'sensor.test_monitored',
}
}
@@ -115,34 +110,226 @@ class TestThresholdSensor(unittest.TestCase):
self.hass.states.set('sensor.test_monitored', 20)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('above', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['upper']),
state.attributes.get('upper'))
self.assertEqual(2.5, state.attributes.get('hysteresis'))
self.assertEqual('upper', state.attributes.get('type'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 13)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 12)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 17)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 18)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_threshold')
state = self.hass.states.get('binary_sensor.threshold')
assert state.state == 'on'
def test_sensor_in_range_no_hysteresis(self):
"""Test if source is within the range."""
config = {
'binary_sensor': {
'platform': 'threshold',
'lower': '10',
'upper': '20',
'entity_id': 'sensor.test_monitored',
}
}
assert setup_component(self.hass, 'binary_sensor', config)
self.hass.states.set('sensor.test_monitored', 16,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('sensor.test_monitored',
state.attributes.get('entity_id'))
self.assertEqual(16, state.attributes.get('sensor_value'))
self.assertEqual('in_range', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['lower']),
state.attributes.get('lower'))
self.assertEqual(float(config['binary_sensor']['upper']),
state.attributes.get('upper'))
self.assertEqual(0.0, state.attributes.get('hysteresis'))
self.assertEqual('range', state.attributes.get('type'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 9)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('below', state.attributes.get('position'))
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 21)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('above', state.attributes.get('position'))
assert state.state == 'off'
def test_sensor_in_range_with_hysteresis(self):
"""Test if source is within the range."""
config = {
'binary_sensor': {
'platform': 'threshold',
'lower': '10',
'upper': '20',
'hysteresis': '2',
'entity_id': 'sensor.test_monitored',
}
}
assert setup_component(self.hass, 'binary_sensor', config)
self.hass.states.set('sensor.test_monitored', 16,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('sensor.test_monitored',
state.attributes.get('entity_id'))
self.assertEqual(16, state.attributes.get('sensor_value'))
self.assertEqual('in_range', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['lower']),
state.attributes.get('lower'))
self.assertEqual(float(config['binary_sensor']['upper']),
state.attributes.get('upper'))
self.assertEqual(float(config['binary_sensor']['hysteresis']),
state.attributes.get('hysteresis'))
self.assertEqual('range', state.attributes.get('type'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 8)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('in_range', state.attributes.get('position'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 7)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('below', state.attributes.get('position'))
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 12)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('below', state.attributes.get('position'))
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 13)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('in_range', state.attributes.get('position'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 22)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('in_range', state.attributes.get('position'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', 23)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('above', state.attributes.get('position'))
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 18)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('above', state.attributes.get('position'))
assert state.state == 'off'
self.hass.states.set('sensor.test_monitored', 17)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('in_range', state.attributes.get('position'))
assert state.state == 'on'
def test_sensor_in_range_unknown_state(self):
"""Test if source is within the range."""
config = {
'binary_sensor': {
'platform': 'threshold',
'lower': '10',
'upper': '20',
'entity_id': 'sensor.test_monitored',
}
}
assert setup_component(self.hass, 'binary_sensor', config)
self.hass.states.set('sensor.test_monitored', 16,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('sensor.test_monitored',
state.attributes.get('entity_id'))
self.assertEqual(16, state.attributes.get('sensor_value'))
self.assertEqual('in_range', state.attributes.get('position'))
self.assertEqual(float(config['binary_sensor']['lower']),
state.attributes.get('lower'))
self.assertEqual(float(config['binary_sensor']['upper']),
state.attributes.get('upper'))
self.assertEqual(0.0, state.attributes.get('hysteresis'))
self.assertEqual('range', state.attributes.get('type'))
assert state.state == 'on'
self.hass.states.set('sensor.test_monitored', STATE_UNKNOWN)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.threshold')
self.assertEqual('unknown', state.attributes.get('position'))
assert state.state == 'off'