Add significant Change support for water heater (#106003)

This commit is contained in:
Michael 2023-12-19 10:38:13 +01:00 committed by GitHub
parent 8c3911ffea
commit a4cb64e20e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 172 additions and 0 deletions

View file

@ -0,0 +1,76 @@
"""Helper to test significant Water Heater state changes."""
from __future__ import annotations
from typing import Any
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.significant_change import (
check_absolute_change,
check_valid_float,
)
from . import (
ATTR_AWAY_MODE,
ATTR_CURRENT_TEMPERATURE,
ATTR_OPERATION_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
ATTR_TEMPERATURE,
)
SIGNIFICANT_ATTRIBUTES: set[str] = {
ATTR_CURRENT_TEMPERATURE,
ATTR_TEMPERATURE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
ATTR_OPERATION_MODE,
ATTR_AWAY_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}
ha_unit = hass.config.units.temperature_unit
for attr_name in changed_attrs:
if attr_name not in SIGNIFICANT_ATTRIBUTES:
continue
if attr_name in [ATTR_OPERATION_MODE, ATTR_AWAY_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 ha_unit == UnitOfTemperature.FAHRENHEIT:
absolute_change = 1.0
else:
absolute_change = 0.5
if check_absolute_change(old_attr_value, new_attr_value, absolute_change):
return True
# no significant attribute change detected
return False

View file

@ -0,0 +1,96 @@
"""Test the Water Heater significant change platform."""
import pytest
from homeassistant.components.water_heater import (
ATTR_AWAY_MODE,
ATTR_CURRENT_TEMPERATURE,
ATTR_OPERATION_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
ATTR_TEMPERATURE,
)
from homeassistant.components.water_heater.significant_change import (
async_check_significant_change,
)
from homeassistant.core import HomeAssistant
from homeassistant.util.unit_system import (
METRIC_SYSTEM as METRIC,
US_CUSTOMARY_SYSTEM as IMPERIAL,
UnitSystem,
)
async def test_significant_state_change(hass: HomeAssistant) -> None:
"""Detect Water Heater significant state changes."""
attrs = {}
assert not async_check_significant_change(hass, "on", attrs, "on", attrs)
assert async_check_significant_change(hass, "on", attrs, "off", attrs)
@pytest.mark.parametrize(
("unit_system", "old_attrs", "new_attrs", "expected_result"),
[
(METRIC, {ATTR_AWAY_MODE: "old_value"}, {ATTR_AWAY_MODE: "old_value"}, False),
(METRIC, {ATTR_AWAY_MODE: "old_value"}, {ATTR_AWAY_MODE: "new_value"}, True),
(
METRIC,
{ATTR_OPERATION_MODE: "old_value"},
{ATTR_OPERATION_MODE: "new_value"},
True,
),
# multiple attributes
(
METRIC,
{ATTR_AWAY_MODE: "old_value", ATTR_OPERATION_MODE: "old_value"},
{ATTR_AWAY_MODE: "new_value", ATTR_OPERATION_MODE: "old_value"},
True,
),
# float attributes
(
METRIC,
{ATTR_CURRENT_TEMPERATURE: 50.0},
{ATTR_CURRENT_TEMPERATURE: 50.5},
True,
),
(
METRIC,
{ATTR_CURRENT_TEMPERATURE: 50.0},
{ATTR_CURRENT_TEMPERATURE: 50.4},
False,
),
(
METRIC,
{ATTR_CURRENT_TEMPERATURE: "invalid"},
{ATTR_CURRENT_TEMPERATURE: 10.0},
True,
),
(
METRIC,
{ATTR_CURRENT_TEMPERATURE: 10.0},
{ATTR_CURRENT_TEMPERATURE: "invalid"},
False,
),
(IMPERIAL, {ATTR_TEMPERATURE: 160.0}, {ATTR_TEMPERATURE: 161}, True),
(IMPERIAL, {ATTR_TEMPERATURE: 160.0}, {ATTR_TEMPERATURE: 160.9}, False),
(METRIC, {ATTR_TARGET_TEMP_HIGH: 80.0}, {ATTR_TARGET_TEMP_HIGH: 80.5}, True),
(METRIC, {ATTR_TARGET_TEMP_HIGH: 80.0}, {ATTR_TARGET_TEMP_HIGH: 80.4}, False),
(METRIC, {ATTR_TARGET_TEMP_LOW: 30.0}, {ATTR_TARGET_TEMP_LOW: 30.5}, True),
(METRIC, {ATTR_TARGET_TEMP_LOW: 30.0}, {ATTR_TARGET_TEMP_LOW: 30.4}, False),
# insignificant attributes
(METRIC, {"unknown_attr": "old_value"}, {"unknown_attr": "old_value"}, False),
(METRIC, {"unknown_attr": "old_value"}, {"unknown_attr": "new_value"}, False),
],
)
async def test_significant_atributes_change(
hass: HomeAssistant,
unit_system: UnitSystem,
old_attrs: dict,
new_attrs: dict,
expected_result: bool,
) -> None:
"""Detect Water Heater significant attribute changes."""
hass.config.units = unit_system
assert (
async_check_significant_change(hass, "state", old_attrs, "state", new_attrs)
== expected_result
)