From 5bc0e9a50f06253df69e74d8eab05debc69ce415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Wed, 10 Mar 2021 10:25:04 +0100 Subject: [PATCH] Fix aemet temperatures with a value of 0 (#47680) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * aemet: catch TypeError exceptions format_float() and format_int() should also catch possible TypeError exceptions. Signed-off-by: Álvaro Fernández Rojas * aemet: correctly parse temperatures with a value of 0 Right now, when a temperature with a value of 0 is provided by the API, the if condition isn't satisfied, return None instead of 0. Signed-off-by: Álvaro Fernández Rojas * aemet: group format int/float exceptions Signed-off-by: Álvaro Fernández Rojas --- .../aemet/weather_update_coordinator.py | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/aemet/weather_update_coordinator.py b/homeassistant/components/aemet/weather_update_coordinator.py index 619429c9a5b..1a70baa6765 100644 --- a/homeassistant/components/aemet/weather_update_coordinator.py +++ b/homeassistant/components/aemet/weather_update_coordinator.py @@ -98,7 +98,7 @@ def format_float(value) -> float: """Try converting string to float.""" try: return float(value) - except ValueError: + except (TypeError, ValueError): return None @@ -106,7 +106,7 @@ def format_int(value) -> int: """Try converting string to int.""" try: return int(value) - except ValueError: + except (TypeError, ValueError): return None @@ -535,9 +535,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): def _get_temperature(day_data, hour): """Get temperature (hour) from weather data.""" val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE], hour) - if val: - return format_int(val) - return None + return format_int(val) @staticmethod def _get_temperature_day(day_data): @@ -545,9 +543,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): val = get_forecast_day_value( day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MAX ) - if val: - return format_int(val) - return None + return format_int(val) @staticmethod def _get_temperature_low_day(day_data): @@ -555,17 +551,13 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): val = get_forecast_day_value( day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MIN ) - if val: - return format_int(val) - return None + return format_int(val) @staticmethod def _get_temperature_feeling(day_data, hour): """Get temperature from weather data.""" val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE_FEELING], hour) - if val: - return format_int(val) - return None + return format_int(val) def _get_town_id(self): """Get town ID from weather data."""