Add significant Change support for remote (#104627)

This commit is contained in:
Michael 2023-11-30 17:09:06 +01:00 committed by GitHub
parent 419dc8adb1
commit 7ec6510221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 0 deletions

View 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

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