Skip to content

Commit

Permalink
Improve timezone handling with providers covering multiple TZ
Browse files Browse the repository at this point in the history
  • Loading branch information
vingerha committed Oct 8, 2024
1 parent 03e39e1 commit a41622d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
27 changes: 21 additions & 6 deletions custom_components/gtfs2/gtfs_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def get_next_departure(self):
return {}

"""Get next departures from data."""
if self.hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant configuration")
timezone = "UTC"
else:
timezone=dt_util.get_time_zone(self.hass.config.time_zone)

schedule = self._data["schedule"]
route_type = self._data["route_type"]

Expand Down Expand Up @@ -99,6 +95,7 @@ def get_next_departure(self):
route.route_long_name,route.route_short_name,
start_station.stop_id as origin_stop_id,
start_station.stop_name as origin_stop_name,
start_station.stop_timezone as origin_stop_timezone,
time(origin_stop_time.arrival_time) AS origin_arrival_time,
time(origin_stop_time.departure_time) AS origin_depart_time,
date(origin_stop_time.departure_time) AS origin_depart_date,
Expand All @@ -109,6 +106,7 @@ def get_next_departure(self):
origin_stop_time.stop_sequence AS origin_stop_sequence,
origin_stop_time.timepoint AS origin_stop_timepoint,
end_station.stop_name as dest_stop_name,
end_station.stop_timezone as dest_stop_timezone,
time(destination_stop_time.arrival_time) AS dest_arrival_time,
time(destination_stop_time.departure_time) AS dest_depart_time,
destination_stop_time.drop_off_type AS dest_drop_off_type,
Expand Down Expand Up @@ -149,6 +147,7 @@ def get_next_departure(self):
route.route_long_name,route.route_short_name,
start_station.stop_id as origin_stop_id,
start_station.stop_name as origin_stop_name,
start_station.stop_timezone as origin_stop_timezone,
time(origin_stop_time.arrival_time) AS origin_arrival_time,
time(origin_stop_time.departure_time) AS origin_depart_time,
date(origin_stop_time.departure_time) AS origin_depart_date,
Expand All @@ -159,6 +158,7 @@ def get_next_departure(self):
origin_stop_time.stop_sequence AS origin_stop_sequence,
origin_stop_time.timepoint AS origin_stop_timepoint,
end_station.stop_name as dest_stop_name,
end_station.stop_timezone as dest_stop_timezone,
time(destination_stop_time.arrival_time) AS dest_arrival_time,
time(destination_stop_time.departure_time) AS dest_depart_time,
destination_stop_time.drop_off_type AS dest_drop_off_type,
Expand Down Expand Up @@ -329,8 +329,21 @@ def get_next_departure(self):
f"{dest_depart.strftime(dt_util.DATE_STR_FORMAT)} {item['dest_depart_time']}"
)
# align on timezone
if self.hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant configuration")
timezone = "UTC"
else:
timezone = dt_util.get_time_zone(self.hass.config.time_zone)
_LOGGER.debug("Timezone HA: %s",timezone)
if item["origin_stop_timezone"]:
timezone = dt_util.get_time_zone(item["origin_stop_timezone"])
if item["dest_stop_timezone"]:
timezone_dest = dt_util.get_time_zone(item["origin_stop_timezone"])
else:
timezone_dest = timezone

depart_time = dt_util.parse_datetime(origin_depart_time).replace(tzinfo=timezone)
arrival_time = dt_util.parse_datetime(dest_arrival_time).replace(tzinfo=timezone)
arrival_time = dt_util.parse_datetime(dest_arrival_time).replace(tzinfo=timezone_dest)
origin_arrival_time = dt_util.as_utc(datetime.datetime.strptime(origin_arrival_time, "%Y-%m-%d %H:%M:%S")).isoformat()
origin_depart_time = dt_util.as_utc(datetime.datetime.strptime(origin_depart_time, "%Y-%m-%d %H:%M:%S")).isoformat()
dest_arrival_time = dt_util.as_utc(datetime.datetime.strptime(dest_arrival_time, "%Y-%m-%d %H:%M:%S")).isoformat()
Expand Down Expand Up @@ -369,7 +382,9 @@ def get_next_departure(self):
"departure_time": depart_time,
"arrival_time": arrival_time,
"origin_stop_time": origin_stop_time,
"origin_stop_timezone": item["origin_stop_timezone"],
"destination_stop_time": destination_stop_time,
"destination_stop_timezone": item["dest_stop_timezone"],
"destination_stop_name": item["dest_stop_name"],
"next_departures": timetable_remaining,
"next_departures_lines": timetable_remaining_line,
Expand Down
21 changes: 13 additions & 8 deletions custom_components/gtfs2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
ATTR_WHEELCHAIR,
ATTR_WHEELCHAIR_DESTINATION,
ATTR_WHEELCHAIR_ORIGIN,
ATTR_TIMEZONE_ORIGIN,
ATTR_TIMEZONE_DESTINATION,
BICYCLE_ALLOWED_DEFAULT,
BICYCLE_ALLOWED_OPTIONS,
DEFAULT_NAME,
Expand Down Expand Up @@ -216,14 +218,14 @@ def _update_attrs(self): # noqa: C901 PLR0911
# Define the state as a Agency TZ, then help TZ (which is UTC if no HA TZ set)
if not self._departure:
self._state = None
elif self._agency:
_LOGGER.debug(
"Self._departure time for state value TZ: %s ",
{self._departure.get("departure_time")},
)
self._state = self._departure["departure_time"].replace(
tzinfo=dt_util.get_time_zone(self._agency.agency_timezone)
)
#elif self._agency:
# _LOGGER.debug(
# "Self._departure time for state value TZ: %s ",
# {self._departure.get("departure_time")},
# )
# self._state = self._departure["departure_time"].replace(
# tzinfo=dt_util.get_time_zone(self._agency.agency_timezone)
# )
else:
_LOGGER.debug(
"Self._departure time from helper: %s",
Expand Down Expand Up @@ -371,13 +373,15 @@ def _update_attrs(self): # noqa: C901 PLR0911
self._attributes[ATTR_TIMEPOINT_ORIGIN] = TIMEPOINT_OPTIONS.get(
self._departure["origin_stop_time"]["Timepoint"], TIMEPOINT_DEFAULT
)
self._attributes[ATTR_TIMEZONE_ORIGIN] = self._departure.get("origin_stop_timezone", None)
else:
self.remove_keys(prefix)

if "destination_stop_time" in self._departure:
_LOGGER.debug("Destination_stop_time %s", self._departure["destination_stop_time"])
else:
_LOGGER.debug("No destination_stop_time, possibly no service today")

prefix = "destination_stop"
if self._departure:
self.append_keys(self._departure["destination_stop_time"], prefix)
Expand All @@ -392,6 +396,7 @@ def _update_attrs(self): # noqa: C901 PLR0911
self._attributes[ATTR_TIMEPOINT_DESTINATION] = TIMEPOINT_OPTIONS.get(
self._departure["destination_stop_time"]["Timepoint"], TIMEPOINT_DEFAULT
)
self._attributes[ATTR_TIMEZONE_DESTINATION] = self._departure.get("destination_stop_timezone", None)
else:
self.remove_keys(prefix)

Expand Down

0 comments on commit a41622d

Please sign in to comment.