Add significant Change support for weather (#104840)
parent
1edfaed7be
commit
3310f4c130
|
@ -0,0 +1,175 @@
|
|||
"""Helper to test significant Weather state changes."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.significant_change import check_absolute_change
|
||||
|
||||
from .const import (
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE,
|
||||
ATTR_WEATHER_CLOUD_COVERAGE,
|
||||
ATTR_WEATHER_DEW_POINT,
|
||||
ATTR_WEATHER_HUMIDITY,
|
||||
ATTR_WEATHER_OZONE,
|
||||
ATTR_WEATHER_PRESSURE,
|
||||
ATTR_WEATHER_PRESSURE_UNIT,
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT,
|
||||
ATTR_WEATHER_UV_INDEX,
|
||||
ATTR_WEATHER_VISIBILITY,
|
||||
ATTR_WEATHER_WIND_BEARING,
|
||||
ATTR_WEATHER_WIND_GUST_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT,
|
||||
)
|
||||
|
||||
SIGNIFICANT_ATTRIBUTES: set[str] = {
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE,
|
||||
ATTR_WEATHER_CLOUD_COVERAGE,
|
||||
ATTR_WEATHER_DEW_POINT,
|
||||
ATTR_WEATHER_HUMIDITY,
|
||||
ATTR_WEATHER_OZONE,
|
||||
ATTR_WEATHER_PRESSURE,
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
ATTR_WEATHER_UV_INDEX,
|
||||
ATTR_WEATHER_VISIBILITY,
|
||||
ATTR_WEATHER_WIND_BEARING,
|
||||
ATTR_WEATHER_WIND_GUST_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED,
|
||||
}
|
||||
|
||||
VALID_CARDINAL_DIRECTIONS: list[str] = [
|
||||
"n",
|
||||
"nne",
|
||||
"ne",
|
||||
"ene",
|
||||
"e",
|
||||
"ese",
|
||||
"se",
|
||||
"sse",
|
||||
"s",
|
||||
"ssw",
|
||||
"sw",
|
||||
"wsw",
|
||||
"w",
|
||||
"wnw",
|
||||
"nw",
|
||||
"nnw",
|
||||
]
|
||||
|
||||
|
||||
def _check_valid_float(value: str | int | float) -> bool:
|
||||
"""Check if given value is a valid float."""
|
||||
try:
|
||||
float(value)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _cardinal_to_degrees(value: str | int | float | None) -> int | float | None:
|
||||
"""Translate a cardinal direction into azimuth angle (degrees)."""
|
||||
if not isinstance(value, str):
|
||||
return value
|
||||
|
||||
try:
|
||||
return float(360 / 16 * VALID_CARDINAL_DIRECTIONS.index(value.lower()))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
@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."""
|
||||
# state changes are always significant
|
||||
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
|
||||
|
||||
old_attr_value = old_attrs.get(attr_name)
|
||||
new_attr_value = new_attrs.get(attr_name)
|
||||
absolute_change: float | None = None
|
||||
if attr_name == ATTR_WEATHER_WIND_BEARING:
|
||||
old_attr_value = _cardinal_to_degrees(old_attr_value)
|
||||
new_attr_value = _cardinal_to_degrees(new_attr_value)
|
||||
|
||||
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 attr_name in (
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE,
|
||||
ATTR_WEATHER_DEW_POINT,
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
):
|
||||
if (
|
||||
unit := new_attrs.get(ATTR_WEATHER_TEMPERATURE_UNIT)
|
||||
) is not None and unit == UnitOfTemperature.FAHRENHEIT:
|
||||
absolute_change = 1.0
|
||||
else:
|
||||
absolute_change = 0.5
|
||||
|
||||
if attr_name in (
|
||||
ATTR_WEATHER_WIND_GUST_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED,
|
||||
):
|
||||
if (
|
||||
unit := new_attrs.get(ATTR_WEATHER_WIND_SPEED_UNIT)
|
||||
) is None or unit in (
|
||||
UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
UnitOfSpeed.MILES_PER_HOUR, # 1km/h = 0.62mi/s
|
||||
UnitOfSpeed.FEET_PER_SECOND, # 1km/h = 0.91ft/s
|
||||
):
|
||||
absolute_change = 1.0
|
||||
elif unit == UnitOfSpeed.METERS_PER_SECOND: # 1km/h = 0.277m/s
|
||||
absolute_change = 0.5
|
||||
|
||||
if attr_name in (
|
||||
ATTR_WEATHER_CLOUD_COVERAGE, # range 0-100%
|
||||
ATTR_WEATHER_HUMIDITY, # range 0-100%
|
||||
ATTR_WEATHER_OZONE, # range ~20-100ppm
|
||||
ATTR_WEATHER_VISIBILITY, # range 0-240km (150mi)
|
||||
ATTR_WEATHER_WIND_BEARING, # range 0-359°
|
||||
):
|
||||
absolute_change = 1.0
|
||||
|
||||
if attr_name == ATTR_WEATHER_UV_INDEX: # range 1-11
|
||||
absolute_change = 0.1
|
||||
|
||||
if attr_name == ATTR_WEATHER_PRESSURE: # local variation of around 100 hpa
|
||||
if (unit := new_attrs.get(ATTR_WEATHER_PRESSURE_UNIT)) is None or unit in (
|
||||
UnitOfPressure.HPA,
|
||||
UnitOfPressure.MBAR, # 1hPa = 1mbar
|
||||
UnitOfPressure.MMHG, # 1hPa = 0.75mmHg
|
||||
):
|
||||
absolute_change = 1.0
|
||||
elif unit == UnitOfPressure.INHG: # 1hPa = 0.03inHg
|
||||
absolute_change = 0.05
|
||||
|
||||
# check for significant attribute value change
|
||||
if absolute_change is not None:
|
||||
if check_absolute_change(old_attr_value, new_attr_value, absolute_change):
|
||||
return True
|
||||
|
||||
# no significant attribute change detected
|
||||
return False
|
|
@ -0,0 +1,347 @@
|
|||
"""Test the Weather significant change platform."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.weather.const import (
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE,
|
||||
ATTR_WEATHER_CLOUD_COVERAGE,
|
||||
ATTR_WEATHER_DEW_POINT,
|
||||
ATTR_WEATHER_HUMIDITY,
|
||||
ATTR_WEATHER_OZONE,
|
||||
ATTR_WEATHER_PRECIPITATION_UNIT,
|
||||
ATTR_WEATHER_PRESSURE,
|
||||
ATTR_WEATHER_PRESSURE_UNIT,
|
||||
ATTR_WEATHER_TEMPERATURE,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT,
|
||||
ATTR_WEATHER_UV_INDEX,
|
||||
ATTR_WEATHER_VISIBILITY_UNIT,
|
||||
ATTR_WEATHER_WIND_BEARING,
|
||||
ATTR_WEATHER_WIND_GUST_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT,
|
||||
)
|
||||
from homeassistant.components.weather.significant_change import (
|
||||
async_check_significant_change,
|
||||
)
|
||||
from homeassistant.const import UnitOfPressure, UnitOfSpeed, UnitOfTemperature
|
||||
|
||||
|
||||
async def test_significant_state_change() -> None:
|
||||
"""Detect Weather significant state changes."""
|
||||
assert not async_check_significant_change(
|
||||
None, "clear-night", {}, "clear-night", {}
|
||||
)
|
||||
assert async_check_significant_change(None, "clear-night", {}, "cloudy", {})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("old_attrs", "new_attrs", "expected_result"),
|
||||
[
|
||||
# insignificant attributes
|
||||
(
|
||||
{ATTR_WEATHER_PRECIPITATION_UNIT: "a"},
|
||||
{ATTR_WEATHER_PRECIPITATION_UNIT: "b"},
|
||||
False,
|
||||
),
|
||||
({ATTR_WEATHER_PRESSURE_UNIT: "a"}, {ATTR_WEATHER_PRESSURE_UNIT: "b"}, False),
|
||||
(
|
||||
{ATTR_WEATHER_TEMPERATURE_UNIT: "a"},
|
||||
{ATTR_WEATHER_TEMPERATURE_UNIT: "b"},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_VISIBILITY_UNIT: "a"},
|
||||
{ATTR_WEATHER_VISIBILITY_UNIT: "b"},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_SPEED_UNIT: "a"},
|
||||
{ATTR_WEATHER_WIND_SPEED_UNIT: "b"},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRECIPITATION_UNIT: "a", ATTR_WEATHER_WIND_SPEED_UNIT: "a"},
|
||||
{ATTR_WEATHER_PRECIPITATION_UNIT: "b", ATTR_WEATHER_WIND_SPEED_UNIT: "a"},
|
||||
False,
|
||||
),
|
||||
# significant attributes, close to but not significant change
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 20},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 20.4},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 68},
|
||||
{
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE: 68.9,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_DEW_POINT: 20},
|
||||
{ATTR_WEATHER_DEW_POINT: 20.4},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_TEMPERATURE: 20},
|
||||
{ATTR_WEATHER_TEMPERATURE: 20.4},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_CLOUD_COVERAGE: 80},
|
||||
{ATTR_WEATHER_CLOUD_COVERAGE: 80.9},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_HUMIDITY: 90},
|
||||
{ATTR_WEATHER_HUMIDITY: 89.1},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "W"}, # W = 270°
|
||||
{ATTR_WEATHER_WIND_BEARING: 269.1},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "W"},
|
||||
{ATTR_WEATHER_WIND_BEARING: "W"},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: 270},
|
||||
{ATTR_WEATHER_WIND_BEARING: 269.1},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_GUST_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_GUST_SPEED: 5.9,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_GUST_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_GUST_SPEED: 5.4,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.METERS_PER_SECOND,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_SPEED: 5.9,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_SPEED: 5.4,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.METERS_PER_SECOND,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_UV_INDEX: 1},
|
||||
{ATTR_WEATHER_UV_INDEX: 1.09},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_OZONE: 20},
|
||||
{ATTR_WEATHER_OZONE: 20.9},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 1000},
|
||||
{ATTR_WEATHER_PRESSURE: 1000.9},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 750.06},
|
||||
{
|
||||
ATTR_WEATHER_PRESSURE: 750.74,
|
||||
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.MMHG,
|
||||
},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 29.5},
|
||||
{
|
||||
ATTR_WEATHER_PRESSURE: 29.54,
|
||||
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.INHG,
|
||||
},
|
||||
False,
|
||||
),
|
||||
# significant attributes with significant change
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 20},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 20.5},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 68},
|
||||
{
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE: 69,
|
||||
ATTR_WEATHER_TEMPERATURE_UNIT: UnitOfTemperature.FAHRENHEIT,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_DEW_POINT: 20},
|
||||
{ATTR_WEATHER_DEW_POINT: 20.5},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_TEMPERATURE: 20},
|
||||
{ATTR_WEATHER_TEMPERATURE: 20.5},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_CLOUD_COVERAGE: 80},
|
||||
{ATTR_WEATHER_CLOUD_COVERAGE: 81},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_HUMIDITY: 90},
|
||||
{ATTR_WEATHER_HUMIDITY: 89},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "W"}, # W = 270°
|
||||
{ATTR_WEATHER_WIND_BEARING: 269},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "W"},
|
||||
{ATTR_WEATHER_WIND_BEARING: "NW"}, # NW = 315°
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: 270},
|
||||
{ATTR_WEATHER_WIND_BEARING: 269},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_GUST_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_GUST_SPEED: 6,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_GUST_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_GUST_SPEED: 5.5,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.METERS_PER_SECOND,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_SPEED: 6,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.KILOMETERS_PER_HOUR,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_SPEED: 5},
|
||||
{
|
||||
ATTR_WEATHER_WIND_SPEED: 5.5,
|
||||
ATTR_WEATHER_WIND_SPEED_UNIT: UnitOfSpeed.METERS_PER_SECOND,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_UV_INDEX: 1},
|
||||
{ATTR_WEATHER_UV_INDEX: 1.1},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_OZONE: 20},
|
||||
{ATTR_WEATHER_OZONE: 21},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 1000},
|
||||
{ATTR_WEATHER_PRESSURE: 1001},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 750},
|
||||
{
|
||||
ATTR_WEATHER_PRESSURE: 749,
|
||||
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.MMHG,
|
||||
},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_PRESSURE: 29.5},
|
||||
{
|
||||
ATTR_WEATHER_PRESSURE: 29.55,
|
||||
ATTR_WEATHER_PRESSURE_UNIT: UnitOfPressure.INHG,
|
||||
},
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_significant_atributes_change(
|
||||
old_attrs: dict, new_attrs: dict, expected_result: bool
|
||||
) -> None:
|
||||
"""Detect Weather significant attribute changes."""
|
||||
assert (
|
||||
async_check_significant_change(None, "state", old_attrs, "state", new_attrs)
|
||||
== expected_result
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("old_attrs", "new_attrs", "expected_result"),
|
||||
[
|
||||
# invalid new values
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 30},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: "invalid"},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 30},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: None},
|
||||
False,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "NNW"},
|
||||
{ATTR_WEATHER_WIND_BEARING: "invalid"},
|
||||
False,
|
||||
),
|
||||
# invalid old values
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: "invalid"},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 30},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: None},
|
||||
{ATTR_WEATHER_APPARENT_TEMPERATURE: 30},
|
||||
True,
|
||||
),
|
||||
(
|
||||
{ATTR_WEATHER_WIND_BEARING: "invalid"},
|
||||
{ATTR_WEATHER_WIND_BEARING: "NNW"},
|
||||
True,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_invalid_atributes_change(
|
||||
old_attrs: dict, new_attrs: dict, expected_result: bool
|
||||
) -> None:
|
||||
"""Detect Weather invalid attribute changes."""
|
||||
assert (
|
||||
async_check_significant_change(None, "state", old_attrs, "state", new_attrs)
|
||||
== expected_result
|
||||
)
|
Loading…
Reference in New Issue