Fix aemet temperatures with a value of 0 (#47680)

* aemet: catch TypeError exceptions

format_float() and format_int() should also catch possible TypeError
exceptions.

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>

* 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 <noltari@gmail.com>

* aemet: group format int/float exceptions

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas 2021-03-10 10:25:04 +01:00 committed by GitHub
parent 5c70dd8ab4
commit 5bc0e9a50f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -98,7 +98,7 @@ def format_float(value) -> float:
"""Try converting string to float.""" """Try converting string to float."""
try: try:
return float(value) return float(value)
except ValueError: except (TypeError, ValueError):
return None return None
@ -106,7 +106,7 @@ def format_int(value) -> int:
"""Try converting string to int.""" """Try converting string to int."""
try: try:
return int(value) return int(value)
except ValueError: except (TypeError, ValueError):
return None return None
@ -535,9 +535,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
def _get_temperature(day_data, hour): def _get_temperature(day_data, hour):
"""Get temperature (hour) from weather data.""" """Get temperature (hour) from weather data."""
val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE], hour) val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE], hour)
if val:
return format_int(val) return format_int(val)
return None
@staticmethod @staticmethod
def _get_temperature_day(day_data): def _get_temperature_day(day_data):
@ -545,9 +543,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
val = get_forecast_day_value( val = get_forecast_day_value(
day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MAX day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MAX
) )
if val:
return format_int(val) return format_int(val)
return None
@staticmethod @staticmethod
def _get_temperature_low_day(day_data): def _get_temperature_low_day(day_data):
@ -555,17 +551,13 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
val = get_forecast_day_value( val = get_forecast_day_value(
day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MIN day_data[AEMET_ATTR_TEMPERATURE], key=AEMET_ATTR_MIN
) )
if val:
return format_int(val) return format_int(val)
return None
@staticmethod @staticmethod
def _get_temperature_feeling(day_data, hour): def _get_temperature_feeling(day_data, hour):
"""Get temperature from weather data.""" """Get temperature from weather data."""
val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE_FEELING], hour) val = get_forecast_hour_value(day_data[AEMET_ATTR_TEMPERATURE_FEELING], hour)
if val:
return format_int(val) return format_int(val)
return None
def _get_town_id(self): def _get_town_id(self):
"""Get town ID from weather data.""" """Get town ID from weather data."""