Use check_valid_float helper in significant change support of sensor and weather (#106013)
parent
a1614d6b7e
commit
c87f2027d4
|
@ -12,6 +12,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.significant_change import (
|
||||
check_absolute_change,
|
||||
check_percentage_change,
|
||||
check_valid_float,
|
||||
)
|
||||
|
||||
from . import SensorDeviceClass
|
||||
|
@ -63,23 +64,20 @@ def async_check_significant_change(
|
|||
absolute_change = 1.0
|
||||
percentage_change = 2.0
|
||||
|
||||
try:
|
||||
if not check_valid_float(new_state):
|
||||
# New state is invalid, don't report it
|
||||
new_state_f = float(new_state)
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
try:
|
||||
if not check_valid_float(old_state):
|
||||
# Old state was invalid, we should report again
|
||||
old_state_f = float(old_state)
|
||||
except ValueError:
|
||||
return True
|
||||
|
||||
if absolute_change is not None and percentage_change is not None:
|
||||
return _absolute_and_relative_change(
|
||||
old_state_f, new_state_f, absolute_change, percentage_change
|
||||
float(old_state), float(new_state), absolute_change, percentage_change
|
||||
)
|
||||
if absolute_change is not None:
|
||||
return check_absolute_change(old_state_f, new_state_f, absolute_change)
|
||||
|
||||
return check_absolute_change(
|
||||
float(old_state), float(new_state), absolute_change
|
||||
)
|
||||
return None
|
||||
|
|
|
@ -5,7 +5,10 @@ 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 homeassistant.helpers.significant_change import (
|
||||
check_absolute_change,
|
||||
check_valid_float,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
ATTR_WEATHER_APPARENT_TEMPERATURE,
|
||||
|
@ -60,15 +63,6 @@ VALID_CARDINAL_DIRECTIONS: list[str] = [
|
|||
]
|
||||
|
||||
|
||||
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):
|
||||
|
@ -109,11 +103,11 @@ def async_check_significant_change(
|
|||
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):
|
||||
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):
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue