mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Add significant change support to binary_sensor (#45677)
* Add significant change support to binary_sensor
This commit is contained in:
parent
85e6bc581f
commit
6bf59dbeab
20
homeassistant/components/binary_sensor/significant_change.py
Normal file
20
homeassistant/components/binary_sensor/significant_change.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
"""Helper to test significant Binary Sensor state changes."""
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_check_significant_change(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
old_state: str,
|
||||||
|
old_attrs: dict,
|
||||||
|
new_state: str,
|
||||||
|
new_attrs: dict,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> Optional[bool]:
|
||||||
|
"""Test if state significantly changed."""
|
||||||
|
if old_state != new_state:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
@ -159,8 +159,8 @@
|
|||||||
"on": "Plugged in"
|
"on": "Plugged in"
|
||||||
},
|
},
|
||||||
"presence": {
|
"presence": {
|
||||||
"off": "Away",
|
"off": "[%key:common::state::not_home%]",
|
||||||
"on": "Home"
|
"on": "[%key:common::state::home%]"
|
||||||
},
|
},
|
||||||
"problem": {
|
"problem": {
|
||||||
"off": "OK",
|
"off": "OK",
|
||||||
|
20
tests/components/binary_sensor/test_significant_change.py
Normal file
20
tests/components/binary_sensor/test_significant_change.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
"""Test the Binary Sensor significant change platform."""
|
||||||
|
from homeassistant.components.binary_sensor.significant_change import (
|
||||||
|
async_check_significant_change,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_significant_change():
|
||||||
|
"""Detect Binary Sensor significant changes."""
|
||||||
|
old_attrs = {"attr_1": "value_1"}
|
||||||
|
new_attrs = {"attr_1": "value_2"}
|
||||||
|
|
||||||
|
assert (
|
||||||
|
async_check_significant_change(None, "on", old_attrs, "on", old_attrs) is False
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
async_check_significant_change(None, "on", old_attrs, "on", new_attrs) is False
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
async_check_significant_change(None, "on", old_attrs, "off", old_attrs) is True
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user