Add significant Change support for remote (#104627)
This commit is contained in:
parent
419dc8adb1
commit
7ec6510221
2 changed files with 89 additions and 0 deletions
27
homeassistant/components/remote/significant_change.py
Normal file
27
homeassistant/components/remote/significant_change.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
"""Helper to test significant Remote state changes."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
|
||||||
|
from . import ATTR_CURRENT_ACTIVITY
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_check_significant_change(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
old_state: str,
|
||||||
|
old_attrs: dict,
|
||||||
|
new_state: str,
|
||||||
|
new_attrs: dict,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> bool | None:
|
||||||
|
"""Test if state significantly changed."""
|
||||||
|
if old_state != new_state:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if old_attrs.get(ATTR_CURRENT_ACTIVITY) != new_attrs.get(ATTR_CURRENT_ACTIVITY):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
62
tests/components/remote/test_significant_change.py
Normal file
62
tests/components/remote/test_significant_change.py
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
"""Test the Remote significant change platform."""
|
||||||
|
from homeassistant.components.remote import ATTR_ACTIVITY_LIST, ATTR_CURRENT_ACTIVITY
|
||||||
|
from homeassistant.components.remote.significant_change import (
|
||||||
|
async_check_significant_change,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_significant_change() -> None:
|
||||||
|
"""Detect Remote significant changes."""
|
||||||
|
# no change at all
|
||||||
|
attrs = {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
}
|
||||||
|
assert not async_check_significant_change(None, "on", attrs, "on", attrs)
|
||||||
|
|
||||||
|
# change of state is significant
|
||||||
|
assert async_check_significant_change(None, "on", attrs, "off", attrs)
|
||||||
|
|
||||||
|
# change of current activity is significant
|
||||||
|
attrs = {
|
||||||
|
"old": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
},
|
||||||
|
"new": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "paused",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert async_check_significant_change(None, "on", attrs["old"], "on", attrs["new"])
|
||||||
|
|
||||||
|
# change of list of possible activities is not significant
|
||||||
|
attrs = {
|
||||||
|
"old": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
},
|
||||||
|
"new": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert not async_check_significant_change(
|
||||||
|
None, "on", attrs["old"], "on", attrs["new"]
|
||||||
|
)
|
||||||
|
|
||||||
|
# change of any not official attribute is not significant
|
||||||
|
attrs = {
|
||||||
|
"old": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
},
|
||||||
|
"new": {
|
||||||
|
ATTR_CURRENT_ACTIVITY: "playing",
|
||||||
|
ATTR_ACTIVITY_LIST: ["playing", "paused"],
|
||||||
|
"not_official": "changed",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert not async_check_significant_change(
|
||||||
|
None, "on", attrs["old"], "on", attrs["new"]
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue