Fix weather handling in zamg (#85635)

* TypeError handling in weather

* Check for None

* Use walrus operator as proposed
pull/91923/head^2
Daniel Gangl 2023-05-15 19:26:02 +02:00 committed by GitHub
parent 48ab74eedf
commit d12ea0040f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 9 deletions

View File

@ -64,8 +64,16 @@ class ZamgWeather(CoordinatorEntity, WeatherEntity):
def native_temperature(self) -> float | None:
"""Return the platform temperature."""
try:
return float(self.coordinator.data[self.station_id]["TL"]["data"])
except (KeyError, ValueError):
if (
value := self.coordinator.data[self.station_id]["TLAM"]["data"]
) is not None:
return float(value)
if (
value := self.coordinator.data[self.station_id]["TL"]["data"]
) is not None:
return float(value)
return None
except (KeyError, ValueError, TypeError):
return None
@property
@ -73,7 +81,7 @@ class ZamgWeather(CoordinatorEntity, WeatherEntity):
"""Return the pressure."""
try:
return float(self.coordinator.data[self.station_id]["P"]["data"])
except (KeyError, ValueError):
except (KeyError, ValueError, TypeError):
return None
@property
@ -81,21 +89,37 @@ class ZamgWeather(CoordinatorEntity, WeatherEntity):
"""Return the humidity."""
try:
return float(self.coordinator.data[self.station_id]["RFAM"]["data"])
except (KeyError, ValueError):
except (KeyError, ValueError, TypeError):
return None
@property
def native_wind_speed(self) -> float | None:
"""Return the wind speed."""
try:
return float(self.coordinator.data[self.station_id]["FFAM"]["data"])
except (KeyError, ValueError):
if (
value := self.coordinator.data[self.station_id]["FFAM"]["data"]
) is not None:
return float(value)
if (
value := self.coordinator.data[self.station_id]["FFX"]["data"]
) is not None:
return float(value)
return None
except (KeyError, ValueError, TypeError):
return None
@property
def wind_bearing(self) -> float | str | None:
def wind_bearing(self) -> float | None:
"""Return the wind bearing."""
try:
return self.coordinator.data[self.station_id]["DD"]["data"]
except (KeyError, ValueError):
if (
value := self.coordinator.data[self.station_id]["DD"]["data"]
) is not None:
return float(value)
if (
value := self.coordinator.data[self.station_id]["DDX"]["data"]
) is not None:
return float(value)
return None
except (KeyError, ValueError, TypeError):
return None