mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
Properly handle thresholds of zero (#12175)
Explicitly test for thresholds to be None rather than truth value testing (which for number types returns False for zero values).
This commit is contained in:
parent
0fd17a7c35
commit
844337ca42
@ -126,11 +126,12 @@ class ThresholdSensor(BinarySensorDevice):
|
||||
@property
|
||||
def threshold_type(self):
|
||||
"""Return the type of threshold this sensor represents."""
|
||||
if self._threshold_lower and self._threshold_upper:
|
||||
if self._threshold_lower is not None and \
|
||||
self._threshold_upper is not None:
|
||||
return TYPE_RANGE
|
||||
elif self._threshold_lower:
|
||||
elif self._threshold_lower is not None:
|
||||
return TYPE_LOWER
|
||||
elif self._threshold_upper:
|
||||
elif self._threshold_upper is not None:
|
||||
return TYPE_UPPER
|
||||
|
||||
@property
|
||||
|
@ -333,3 +333,63 @@ class TestThresholdSensor(unittest.TestCase):
|
||||
|
||||
self.assertEqual('unknown', state.attributes.get('position'))
|
||||
assert state.state == 'off'
|
||||
|
||||
def test_sensor_lower_zero_threshold(self):
|
||||
"""Test if a lower threshold of zero is set."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
'platform': 'threshold',
|
||||
'lower': '0',
|
||||
'entity_id': 'sensor.test_monitored',
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'binary_sensor', config)
|
||||
|
||||
self.hass.states.set('sensor.test_monitored', 16)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('binary_sensor.threshold')
|
||||
|
||||
self.assertEqual('lower', state.attributes.get('type'))
|
||||
self.assertEqual(float(config['binary_sensor']['lower']),
|
||||
state.attributes.get('lower'))
|
||||
|
||||
assert state.state == 'off'
|
||||
|
||||
self.hass.states.set('sensor.test_monitored', -3)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('binary_sensor.threshold')
|
||||
|
||||
assert state.state == 'on'
|
||||
|
||||
def test_sensor_upper_zero_threshold(self):
|
||||
"""Test if an upper threshold of zero is set."""
|
||||
config = {
|
||||
'binary_sensor': {
|
||||
'platform': 'threshold',
|
||||
'upper': '0',
|
||||
'entity_id': 'sensor.test_monitored',
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'binary_sensor', config)
|
||||
|
||||
self.hass.states.set('sensor.test_monitored', -10)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('binary_sensor.threshold')
|
||||
|
||||
self.assertEqual('upper', state.attributes.get('type'))
|
||||
self.assertEqual(float(config['binary_sensor']['upper']),
|
||||
state.attributes.get('upper'))
|
||||
|
||||
assert state.state == 'off'
|
||||
|
||||
self.hass.states.set('sensor.test_monitored', 2)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('binary_sensor.threshold')
|
||||
|
||||
assert state.state == 'on'
|
||||
|
Loading…
x
Reference in New Issue
Block a user