From 394ccae8a1217fdfde32353814ce9ff582afcd79 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Fri, 19 Nov 2021 18:06:39 -0700 Subject: [PATCH] Migrate appropriate Ambient PWS sensors to use datetime state objects (#59942) --- homeassistant/components/ambient_station/sensor.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index 66bccb30b55..58ac081efbf 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -1,6 +1,8 @@ """Support for Ambient Weather Station sensors.""" from __future__ import annotations +from datetime import datetime + from homeassistant.components.sensor import ( STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING, @@ -643,6 +645,11 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity): @callback def update_from_latest_data(self) -> None: """Fetch new state data for the sensor.""" - self._attr_native_value = self._ambient.stations[self._mac_address][ - ATTR_LAST_DATA - ][self.entity_description.key] + raw = self._ambient.stations[self._mac_address][ATTR_LAST_DATA][ + self.entity_description.key + ] + + if self.entity_description.key == TYPE_LASTRAIN: + self._attr_native_value = datetime.strptime(raw, "%Y-%m-%dT%H:%M:%S.%f%z") + else: + self._attr_native_value = raw