From 0e48a658f378ed8c424654cb30f6d8918bb64b6e Mon Sep 17 00:00:00 2001 From: G Johansson Date: Wed, 22 Dec 2021 21:28:18 +0100 Subject: [PATCH] Fix timezone trafikverket_train (#62582) * Bugfix trafikverket train * Change from pytz to hass function * Fix datetime in extra attributes * Fix time timezone * Reset changes extra attributes --- .../components/trafikverket_train/sensor.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/trafikverket_train/sensor.py b/homeassistant/components/trafikverket_train/sensor.py index 08ebb32abcf..57d02e95582 100644 --- a/homeassistant/components/trafikverket_train/sensor.py +++ b/homeassistant/components/trafikverket_train/sensor.py @@ -14,6 +14,7 @@ from homeassistant.components.sensor import ( from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_WEEKDAY, WEEKDAYS from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv +from homeassistant.util.dt import get_time_zone _LOGGER = logging.getLogger(__name__) @@ -129,12 +130,15 @@ class TrainSensor(SensorEntity): self._state = None self._departure_state = None self._delay_in_minutes = None + self._timezone = get_time_zone("Europe/Stockholm") async def async_update(self): """Retrieve latest state.""" if self._time is not None: departure_day = next_departuredate(self._weekday) - when = datetime.combine(departure_day, self._time) + when = datetime.combine(departure_day, self._time).astimezone( + self._timezone + ) try: self._state = await self._train_api.async_get_train_stop( self._from_station, self._to_station, when @@ -191,8 +195,8 @@ class TrainSensor(SensorEntity): """Return the departure state.""" if (state := self._state) is not None: if state.time_at_location is not None: - return state.time_at_location + return state.time_at_location.astimezone(self._timezone) if state.estimated_time_at_location is not None: - return state.estimated_time_at_location - return state.advertised_time_at_location + return state.estimated_time_at_location.astimezone(self._timezone) + return state.advertised_time_at_location.astimezone(self._timezone) return None