2021-01-26 13:13:27 +00:00
|
|
|
"""Helper to test significant sensor state changes."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2021-01-26 13:13:27 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2021-01-26 20:45:09 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-01-26 13:13:27 +00:00
|
|
|
|
|
|
|
from . import DEVICE_CLASS_BATTERY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE
|
|
|
|
|
|
|
|
|
2021-01-26 20:45:09 +00:00
|
|
|
@callback
|
|
|
|
def async_check_significant_change(
|
2021-01-26 13:13:27 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
old_state: str,
|
|
|
|
old_attrs: dict,
|
|
|
|
new_state: str,
|
|
|
|
new_attrs: dict,
|
|
|
|
**kwargs: Any,
|
2021-03-18 13:31:38 +00:00
|
|
|
) -> bool | None:
|
2021-01-26 13:13:27 +00:00
|
|
|
"""Test if state significantly changed."""
|
|
|
|
device_class = new_attrs.get(ATTR_DEVICE_CLASS)
|
|
|
|
|
|
|
|
if device_class is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if device_class == DEVICE_CLASS_TEMPERATURE:
|
|
|
|
if new_attrs.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT:
|
2021-03-18 13:31:38 +00:00
|
|
|
change: float | int = 1
|
2021-01-26 13:13:27 +00:00
|
|
|
else:
|
2021-01-26 21:11:06 +00:00
|
|
|
change = 0.5
|
2021-01-26 13:13:27 +00:00
|
|
|
|
|
|
|
old_value = float(old_state)
|
|
|
|
new_value = float(new_state)
|
2021-01-26 21:11:06 +00:00
|
|
|
return abs(old_value - new_value) >= change
|
2021-01-26 13:13:27 +00:00
|
|
|
|
|
|
|
if device_class in (DEVICE_CLASS_BATTERY, DEVICE_CLASS_HUMIDITY):
|
|
|
|
old_value = float(old_state)
|
|
|
|
new_value = float(new_state)
|
|
|
|
|
2021-01-26 21:11:06 +00:00
|
|
|
return abs(old_value - new_value) >= 1
|
2021-01-26 13:13:27 +00:00
|
|
|
|
|
|
|
return None
|