Add significant change support to binary_sensor (#45677)

* Add significant change support to binary_sensor
pull/46219/head
Aaron Bach 2021-01-30 01:04:35 -07:00 committed by GitHub
parent 85e6bc581f
commit 6bf59dbeab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 2 deletions

View 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

View File

@ -159,8 +159,8 @@
"on": "Plugged in"
},
"presence": {
"off": "Away",
"on": "Home"
"off": "[%key:common::state::not_home%]",
"on": "[%key:common::state::home%]"
},
"problem": {
"off": "OK",

View 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
)