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
This commit is contained in:
G Johansson 2021-12-22 21:28:18 +01:00 committed by Franck Nijhof
parent d10c5f459f
commit 9d235618ff
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3

View File

@ -16,6 +16,7 @@ from homeassistant.const import (
) )
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util.dt import get_time_zone
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -131,12 +132,15 @@ class TrainSensor(SensorEntity):
self._state = None self._state = None
self._departure_state = None self._departure_state = None
self._delay_in_minutes = None self._delay_in_minutes = None
self._timezone = get_time_zone("Europe/Stockholm")
async def async_update(self): async def async_update(self):
"""Retrieve latest state.""" """Retrieve latest state."""
if self._time is not None: if self._time is not None:
departure_day = next_departuredate(self._weekday) departure_day = next_departuredate(self._weekday)
when = datetime.combine(departure_day, self._time) when = datetime.combine(departure_day, self._time).astimezone(
self._timezone
)
try: try:
self._state = await self._train_api.async_get_train_stop( self._state = await self._train_api.async_get_train_stop(
self._from_station, self._to_station, when self._from_station, self._to_station, when
@ -193,8 +197,8 @@ class TrainSensor(SensorEntity):
"""Return the departure state.""" """Return the departure state."""
if (state := self._state) is not None: if (state := self._state) is not None:
if state.time_at_location 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: if state.estimated_time_at_location is not None:
return state.estimated_time_at_location return state.estimated_time_at_location.astimezone(self._timezone)
return state.advertised_time_at_location return state.advertised_time_at_location.astimezone(self._timezone)
return None return None