Add significant Change support for humidifier (#106015)
This commit is contained in:
parent
20ba764d92
commit
321dc3984c
2 changed files with 113 additions and 0 deletions
60
homeassistant/components/humidifier/significant_change.py
Normal file
60
homeassistant/components/humidifier/significant_change.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
"""Helper to test significant Humidifier state changes."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.significant_change import (
|
||||||
|
check_absolute_change,
|
||||||
|
check_valid_float,
|
||||||
|
)
|
||||||
|
|
||||||
|
from . import ATTR_ACTION, ATTR_CURRENT_HUMIDITY, ATTR_HUMIDITY, ATTR_MODE
|
||||||
|
|
||||||
|
SIGNIFICANT_ATTRIBUTES: set[str] = {
|
||||||
|
ATTR_ACTION,
|
||||||
|
ATTR_CURRENT_HUMIDITY,
|
||||||
|
ATTR_HUMIDITY,
|
||||||
|
ATTR_MODE,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
old_attrs_s = set(old_attrs.items())
|
||||||
|
new_attrs_s = set(new_attrs.items())
|
||||||
|
changed_attrs: set[str] = {item[0] for item in old_attrs_s ^ new_attrs_s}
|
||||||
|
|
||||||
|
for attr_name in changed_attrs:
|
||||||
|
if attr_name not in SIGNIFICANT_ATTRIBUTES:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if attr_name in [ATTR_ACTION, ATTR_MODE]:
|
||||||
|
return True
|
||||||
|
|
||||||
|
old_attr_value = old_attrs.get(attr_name)
|
||||||
|
new_attr_value = new_attrs.get(attr_name)
|
||||||
|
if new_attr_value is None or not check_valid_float(new_attr_value):
|
||||||
|
# New attribute value is invalid, ignore it
|
||||||
|
continue
|
||||||
|
|
||||||
|
if old_attr_value is None or not check_valid_float(old_attr_value):
|
||||||
|
# Old attribute value was invalid, we should report again
|
||||||
|
return True
|
||||||
|
|
||||||
|
if check_absolute_change(old_attr_value, new_attr_value, 1.0):
|
||||||
|
return True
|
||||||
|
|
||||||
|
# no significant attribute change detected
|
||||||
|
return False
|
53
tests/components/humidifier/test_significant_change.py
Normal file
53
tests/components/humidifier/test_significant_change.py
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
"""Test the Humidifier significant change platform."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.humidifier import (
|
||||||
|
ATTR_ACTION,
|
||||||
|
ATTR_CURRENT_HUMIDITY,
|
||||||
|
ATTR_HUMIDITY,
|
||||||
|
ATTR_MODE,
|
||||||
|
)
|
||||||
|
from homeassistant.components.humidifier.significant_change import (
|
||||||
|
async_check_significant_change,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_significant_state_change() -> None:
|
||||||
|
"""Detect Humidifier significant state changes."""
|
||||||
|
attrs = {}
|
||||||
|
assert not async_check_significant_change(None, "on", attrs, "on", attrs)
|
||||||
|
assert async_check_significant_change(None, "on", attrs, "off", attrs)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("old_attrs", "new_attrs", "expected_result"),
|
||||||
|
[
|
||||||
|
({ATTR_ACTION: "old_value"}, {ATTR_ACTION: "old_value"}, False),
|
||||||
|
({ATTR_ACTION: "old_value"}, {ATTR_ACTION: "new_value"}, True),
|
||||||
|
({ATTR_MODE: "old_value"}, {ATTR_MODE: "new_value"}, True),
|
||||||
|
# multiple attributes
|
||||||
|
(
|
||||||
|
{ATTR_ACTION: "old_value", ATTR_MODE: "old_value"},
|
||||||
|
{ATTR_ACTION: "new_value", ATTR_MODE: "old_value"},
|
||||||
|
True,
|
||||||
|
),
|
||||||
|
# float attributes
|
||||||
|
({ATTR_CURRENT_HUMIDITY: 60.0}, {ATTR_CURRENT_HUMIDITY: 61}, True),
|
||||||
|
({ATTR_CURRENT_HUMIDITY: 60.0}, {ATTR_CURRENT_HUMIDITY: 60.9}, False),
|
||||||
|
({ATTR_CURRENT_HUMIDITY: "invalid"}, {ATTR_CURRENT_HUMIDITY: 60.0}, True),
|
||||||
|
({ATTR_CURRENT_HUMIDITY: 60.0}, {ATTR_CURRENT_HUMIDITY: "invalid"}, False),
|
||||||
|
({ATTR_HUMIDITY: 62.0}, {ATTR_HUMIDITY: 63.0}, True),
|
||||||
|
({ATTR_HUMIDITY: 62.0}, {ATTR_HUMIDITY: 62.9}, False),
|
||||||
|
# insignificant attributes
|
||||||
|
({"unknown_attr": "old_value"}, {"unknown_attr": "old_value"}, False),
|
||||||
|
({"unknown_attr": "old_value"}, {"unknown_attr": "new_value"}, False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_significant_atributes_change(
|
||||||
|
old_attrs: dict, new_attrs: dict, expected_result: bool
|
||||||
|
) -> None:
|
||||||
|
"""Detect Humidifier significant attribute changes."""
|
||||||
|
assert (
|
||||||
|
async_check_significant_change(None, "state", old_attrs, "state", new_attrs)
|
||||||
|
== expected_result
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue