From 402a0ea7dac4cb4f88afa1d862fee637ad1f9648 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Thu, 14 Jan 2021 08:47:45 +0100 Subject: [PATCH] Fix OpenWeatherMap forecast timestamp (#45124) --- homeassistant/components/openweathermap/sensor.py | 10 +++++++++- .../openweathermap/weather_update_coordinator.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/openweathermap/sensor.py b/homeassistant/components/openweathermap/sensor.py index 39c50c3b941..b1ba4ab7625 100644 --- a/homeassistant/components/openweathermap/sensor.py +++ b/homeassistant/components/openweathermap/sensor.py @@ -1,7 +1,10 @@ """Support for the OpenWeatherMap (OWM) service.""" +import datetime + from .abstract_owm_sensor import AbstractOpenWeatherMapSensor from .const import ( ATTR_API_FORECAST, + DEVICE_CLASS_TIMESTAMP, DOMAIN, ENTRY_NAME, ENTRY_WEATHER_COORDINATOR, @@ -95,5 +98,10 @@ class OpenWeatherMapForecastSensor(AbstractOpenWeatherMapSensor): """Return the state of the device.""" forecasts = self._weather_coordinator.data.get(ATTR_API_FORECAST) 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 diff --git a/homeassistant/components/openweathermap/weather_update_coordinator.py b/homeassistant/components/openweathermap/weather_update_coordinator.py index b4ddb40c046..605e6f9edc1 100644 --- a/homeassistant/components/openweathermap/weather_update_coordinator.py +++ b/homeassistant/components/openweathermap/weather_update_coordinator.py @@ -138,7 +138,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): def _convert_forecast(self, entry): forecast = { - ATTR_FORECAST_TIME: entry.reference_time("unix") * 1000, + ATTR_FORECAST_TIME: entry.reference_time("unix"), ATTR_FORECAST_PRECIPITATION: self._calc_precipitation( entry.rain, entry.snow ),