mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 10:08:23 +00:00
Improve threshold sensor
This commit is contained in:
parent
ee78864b05
commit
336c17112b
@ -212,25 +212,37 @@ class ThresholdSensor(BinarySensorEntity):
|
||||
|
||||
if self.sensor_value is None:
|
||||
self._state_position = POSITION_UNKNOWN
|
||||
self._state = False
|
||||
self._state = None
|
||||
return
|
||||
|
||||
elif self.threshold_type == TYPE_LOWER:
|
||||
if self.threshold_type == TYPE_LOWER:
|
||||
if self._state is None:
|
||||
self._state = False
|
||||
self._state_position = POSITION_ABOVE
|
||||
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
|
||||
return
|
||||
|
||||
elif self.threshold_type == TYPE_UPPER:
|
||||
if self.threshold_type == TYPE_UPPER:
|
||||
if self._state is None:
|
||||
self._state = False
|
||||
self._state_position = POSITION_BELOW
|
||||
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
|
||||
return
|
||||
|
||||
elif self.threshold_type == TYPE_RANGE:
|
||||
if self.threshold_type == TYPE_RANGE:
|
||||
if self._state is None:
|
||||
self._state = True
|
||||
self._state_position = POSITION_IN_RANGE
|
||||
if below(self._threshold_lower):
|
||||
self._state_position = POSITION_BELOW
|
||||
self._state = False
|
||||
@ -240,3 +252,4 @@ class ThresholdSensor(BinarySensorEntity):
|
||||
elif above(self._threshold_lower) and below(self._threshold_upper):
|
||||
self._state_position = POSITION_IN_RANGE
|
||||
self._state = True
|
||||
return
|
||||
|
@ -28,8 +28,8 @@ async def test_sensor_upper(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 15)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "below"
|
||||
assert state.state == "off"
|
||||
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
@ -62,12 +62,12 @@ async def test_sensor_upper(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 15)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.attributes["position"] == "below"
|
||||
assert state.state == "off"
|
||||
|
||||
|
||||
@ -88,8 +88,8 @@ async def test_sensor_lower(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 15)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "above"
|
||||
assert state.state == "off"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 16)
|
||||
await hass.async_block_till_done()
|
||||
@ -116,12 +116,12 @@ async def test_sensor_lower(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 15)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.attributes["position"] == "above"
|
||||
assert state.state == "off"
|
||||
|
||||
|
||||
@ -143,15 +143,15 @@ async def test_sensor_upper_hysteresis(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 17.5)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "below"
|
||||
assert state.state == "off"
|
||||
|
||||
# Set the monitored sensor's state to the threshold - hysteresis
|
||||
hass.states.async_set("sensor.test_monitored", 12.5)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "below"
|
||||
assert state.state == "off"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 20)
|
||||
await hass.async_block_till_done()
|
||||
@ -191,7 +191,7 @@ async def test_sensor_upper_hysteresis(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 18)
|
||||
await hass.async_block_till_done()
|
||||
@ -218,15 +218,15 @@ async def test_sensor_lower_hysteresis(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 17.5)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "above"
|
||||
assert state.state == "off"
|
||||
|
||||
# Set the monitored sensor's state to the threshold - hysteresis
|
||||
hass.states.async_set("sensor.test_monitored", 12.5)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "above"
|
||||
assert state.state == "off"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 20)
|
||||
await hass.async_block_till_done()
|
||||
@ -266,7 +266,7 @@ async def test_sensor_lower_hysteresis(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 18)
|
||||
await hass.async_block_till_done()
|
||||
@ -293,15 +293,15 @@ async def test_sensor_in_range_no_hysteresis(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 10)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
# Set the monitored sensor's state to the upper threshold
|
||||
hass.states.async_set("sensor.test_monitored", 20)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
@ -335,7 +335,7 @@ async def test_sensor_in_range_no_hysteresis(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 21)
|
||||
await hass.async_block_till_done()
|
||||
@ -363,29 +363,29 @@ async def test_sensor_in_range_with_hysteresis(hass: HomeAssistant) -> None:
|
||||
hass.states.async_set("sensor.test_monitored", 8)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
# Set the monitored sensor's state to the lower threshold + hysteresis
|
||||
hass.states.async_set("sensor.test_monitored", 12)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
# Set the monitored sensor's state to the upper threshold + hysteresis
|
||||
hass.states.async_set("sensor.test_monitored", 22)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
# Set the monitored sensor's state to the upper threshold - hysteresis
|
||||
hass.states.async_set("sensor.test_monitored", 18)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["position"] == "in_range"
|
||||
assert state.state == "on"
|
||||
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
@ -459,7 +459,7 @@ async def test_sensor_in_range_with_hysteresis(hass: HomeAssistant) -> None:
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 17)
|
||||
await hass.async_block_till_done()
|
||||
@ -506,13 +506,13 @@ async def test_sensor_in_range_unknown_state(
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", STATE_UNAVAILABLE)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes["position"] == "unknown"
|
||||
assert state.state == "off"
|
||||
assert state.state == "unknown"
|
||||
|
||||
assert "State is not numerical" not in caplog.text
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user