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 GitHub
parent 0b8cf47613
commit 0e48a658f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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