Fix OpenWeatherMap forecast timestamp (#45124)

pull/45143/head^2
Philip Allgaier 2021-01-14 08:47:45 +01:00 committed by GitHub
parent 17cb071173
commit 402a0ea7da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -1,7 +1,10 @@
"""Support for the OpenWeatherMap (OWM) service.""" """Support for the OpenWeatherMap (OWM) service."""
import datetime
from .abstract_owm_sensor import AbstractOpenWeatherMapSensor from .abstract_owm_sensor import AbstractOpenWeatherMapSensor
from .const import ( from .const import (
ATTR_API_FORECAST, ATTR_API_FORECAST,
DEVICE_CLASS_TIMESTAMP,
DOMAIN, DOMAIN,
ENTRY_NAME, ENTRY_NAME,
ENTRY_WEATHER_COORDINATOR, ENTRY_WEATHER_COORDINATOR,
@ -95,5 +98,10 @@ class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor):
"""Return the state of the device.""" """Return the state of the device."""
forecasts = self._weather_coordinator.data.get(ATTR_API_FORECAST) forecasts = self._weather_coordinator.data.get(ATTR_API_FORECAST)
if forecasts is not None and len(forecasts) > 0: if forecasts is not None and len(forecasts) > 0:
return forecasts[0].get(self._sensor_type, None) value = forecasts[0].get(self._sensor_type, None)
if self._device_class is DEVICE_CLASS_TIMESTAMP:
value = datetime.datetime.fromtimestamp(
value, datetime.timezone.utc
).isoformat()
return value
return None return None

View File

@ -138,7 +138,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
def _convert_forecast(self, entry): def _convert_forecast(self, entry):
forecast = { forecast = {
ATTR_FORECAST_TIME: entry.reference_time("unix") * 1000, ATTR_FORECAST_TIME: entry.reference_time("unix"),
ATTR_FORECAST_PRECIPITATION: self._calc_precipitation( ATTR_FORECAST_PRECIPITATION: self._calc_precipitation(
entry.rain, entry.snow entry.rain, entry.snow
), ),