From c87f2027d486e45f87c56b57ef53ef847145c7ab Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:31:10 +0100 Subject: [PATCH] Use check_valid_float helper in significant change support of sensor and weather (#106013) --- .../components/sensor/significant_change.py | 16 +++++++--------- .../components/weather/significant_change.py | 18 ++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/sensor/significant_change.py b/homeassistant/components/sensor/significant_change.py index 1a4dc65f010..f426674c32d 100644 --- a/homeassistant/components/sensor/significant_change.py +++ b/homeassistant/components/sensor/significant_change.py @@ -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 diff --git a/homeassistant/components/weather/significant_change.py b/homeassistant/components/weather/significant_change.py index bd6571a390e..4bb67c54e19 100644 --- a/homeassistant/components/weather/significant_change.py +++ b/homeassistant/components/weather/significant_change.py @@ -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