Skip to content

Commit

Permalink
fix offset not working #47
Browse files Browse the repository at this point in the history
  • Loading branch information
FaserF committed Aug 24, 2024
1 parent a04fb96 commit 162c695
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion custom_components/deutschebahn/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"requirements": [
"schiene==0.26"
],
"version": "3.0.1"
"version": "3.0.2"
}
65 changes: 46 additions & 19 deletions custom_components/deutschebahn/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,49 @@ async def async_update(self):
_LOGGER.exception(f"Cannot retrieve data for direction: '{self.start}' '{self.goal}' - Most likely it is a temporary API issue from DB and will stop working after a HA restart/some time.")

def fetch_schiene_connections(hass, self, ignored_products):
raw_data = self.schiene.connections(
self.start,
self.goal,
dt_util.as_local(dt_util.utcnow() + self.offset),
self.only_direct,
)
_LOGGER.debug(f"Fetched data: {raw_data}")

data = []
for connection in raw_data:
if len(data) == self.max_connections:
break
elif set(connection["products"]).intersection(ignored_products):
continue

data.append(connection)
_LOGGER.debug(f"Filtered data: {data}")

return data
"""Fetch connections from Schiene API and apply offset filter."""
try:
raw_data = self.schiene.connections(
self.start,
self.goal,
dt_util.as_local(dt_util.utcnow() + self.offset),
self.only_direct,
)
_LOGGER.debug(f"Fetched raw data: {raw_data}")

current_time = dt_util.as_local(dt_util.utcnow() + self.offset)
_LOGGER.debug(f"Current time with offset: {current_time}")

data = []
for connection in raw_data:
departure_time = dt_util.parse_time(connection.get("departure"))
if departure_time:
# Combine date with time, then make sure datetime is aware
departure_datetime = datetime.combine(datetime.now().date(), departure_time)
# Assume timezone is the same as current_time's timezone
departure_datetime = dt_util.as_local(departure_datetime)

delay_info = connection.get("delay", {"delay_departure": 0, "delay_arrival": 0})
delay_departure = delay_info.get("delay_departure", 0)
delay_arrival = delay_info.get("delay_arrival", 0)
departure_datetime += timedelta(minutes=delay_departure)
_LOGGER.debug(f"Departure datetime for connection: {departure_datetime}")

if departure_datetime < current_time:
_LOGGER.debug(f"Connection filtered out, departure time {departure_datetime} is before current time {current_time}")
continue

if len(data) == self.max_connections:
_LOGGER.debug("Reached maximum number of connections to return")
break
elif set(connection["products"]).intersection(ignored_products):
_LOGGER.debug(f"Connection filtered out due to ignored products: {connection['products']}")
continue

data.append(connection)

_LOGGER.debug(f"Filtered data: {data}")
return data
except Exception as e:
_LOGGER.exception(f"Error fetching or processing connections: {e}")
return []

0 comments on commit 162c695

Please sign in to comment.