Ensure Forecast.Solar returns an iso formatted timestamp (#52669)

This commit is contained in:
Franck Nijhof 2021-07-08 10:09:30 +02:00 committed by GitHub
parent 5ff7c7708d
commit 62c7e5408b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,8 @@
"""Support for the Forecast.Solar sensor service.""" """Support for the Forecast.Solar sensor service."""
from __future__ import annotations from __future__ import annotations
from datetime import datetime
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_IDENTIFIERS, ATTR_MANUFACTURER, ATTR_NAME from homeassistant.const import ATTR_IDENTIFIERS, ATTR_MANUFACTURER, ATTR_NAME
@ -64,5 +66,7 @@ class ForecastSolarSensorEntity(CoordinatorEntity, SensorEntity):
@property @property
def state(self) -> StateType: def state(self) -> StateType:
"""Return the state of the sensor.""" """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 return state

View File

@ -70,7 +70,7 @@ async def test_sensors(
assert entry assert entry
assert state assert state
assert entry.unique_id == f"{entry_id}_power_highest_peak_time_today" 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_FRIENDLY_NAME) == "Highest Power Peak Time - Today"
assert state.attributes.get(ATTR_STATE_CLASS) is None assert state.attributes.get(ATTR_STATE_CLASS) is None
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TIMESTAMP assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TIMESTAMP
@ -82,7 +82,7 @@ async def test_sensors(
assert entry assert entry
assert state assert state
assert entry.unique_id == f"{entry_id}_power_highest_peak_time_tomorrow" 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 ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) == "Highest Power Peak Time - Tomorrow" state.attributes.get(ATTR_FRIENDLY_NAME) == "Highest Power Peak Time - Tomorrow"
) )