From 74e3cac8b54e6889cbfa4f18d9de5916b45a6d6f Mon Sep 17 00:00:00 2001 From: Arjan <44190435+vingerha@users.noreply.github.com> Date: Tue, 25 Apr 2023 21:02:02 +0200 Subject: [PATCH] Fix timezone issue (#92005) --- homeassistant/components/gtfs/sensor.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/gtfs/sensor.py b/homeassistant/components/gtfs/sensor.py index 77e1d0f7d33..9fac4d01926 100644 --- a/homeassistant/components/gtfs/sensor.py +++ b/homeassistant/components/gtfs/sensor.py @@ -610,14 +610,6 @@ class GTFSDepartureSensor(SensorEntity): self._include_tomorrow, ) - # Define the state as a UTC timestamp with ISO 8601 format - if not self._departure: - self._state = None - else: - self._state = self._departure["departure_time"].replace( - tzinfo=dt_util.UTC - ) - # Fetch trip and route details once, unless updated if not self._departure: self._trip = None @@ -648,6 +640,19 @@ class GTFSDepartureSensor(SensorEntity): ) self._agency = False + # Define the state as a UTC timestamp with ISO 8601 format + if not self._departure: + self._state = None + else: + if self._agency: + self._state = self._departure["departure_time"].replace( + tzinfo=dt_util.get_time_zone(self._agency.agency_timezone) + ) + else: + self._state = self._departure["departure_time"].replace( + tzinfo=dt_util.UTC + ) + # Assign attributes, icon and name self.update_attributes()