From 62c7e5408b14e80952920b8dfb6664edbb29363c Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 8 Jul 2021 10:09:30 +0200 Subject: [PATCH] Ensure Forecast.Solar returns an iso formatted timestamp (#52669) --- homeassistant/components/forecast_solar/sensor.py | 6 +++++- tests/components/forecast_solar/test_sensor.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/forecast_solar/sensor.py b/homeassistant/components/forecast_solar/sensor.py index a6b1927926efae..b32f1f341be421 100644 --- a/homeassistant/components/forecast_solar/sensor.py +++ b/homeassistant/components/forecast_solar/sensor.py @@ -1,6 +1,8 @@ """Support for the Forecast.Solar sensor service.""" from __future__ import annotations +from datetime import datetime + from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_IDENTIFIERS, ATTR_MANUFACTURER, ATTR_NAME @@ -64,5 +66,7 @@ def __init__( @property def state(self) -> StateType: """Return the state of the sensor.""" - state: StateType = getattr(self.coordinator.data, self._sensor.key) + state: StateType | datetime = getattr(self.coordinator.data, self._sensor.key) + if isinstance(state, datetime): + return state.isoformat() return state diff --git a/tests/components/forecast_solar/test_sensor.py b/tests/components/forecast_solar/test_sensor.py index a3513b86a5dbd5..31c367678c1c48 100644 --- a/tests/components/forecast_solar/test_sensor.py +++ b/tests/components/forecast_solar/test_sensor.py @@ -70,7 +70,7 @@ async def test_sensors( assert entry assert state assert entry.unique_id == f"{entry_id}_power_highest_peak_time_today" - assert state.state == "2021-06-27 13:00:00+00:00" + assert state.state == "2021-06-27T13:00:00+00:00" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Highest Power Peak Time - Today" assert state.attributes.get(ATTR_STATE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TIMESTAMP @@ -82,7 +82,7 @@ async def test_sensors( assert entry assert state assert entry.unique_id == f"{entry_id}_power_highest_peak_time_tomorrow" - assert state.state == "2021-06-27 14:00:00+00:00" + assert state.state == "2021-06-27T14:00:00+00:00" assert ( state.attributes.get(ATTR_FRIENDLY_NAME) == "Highest Power Peak Time - Tomorrow" )