Skip to content

Commit

Permalink
removed restriction of single day trips and converted '1 day hh:mm:ss…
Browse files Browse the repository at this point in the history
…' to 'hh:mm:ss' (#11)
  • Loading branch information
anand-p-r authored Feb 6, 2021
1 parent 727c524 commit 75fc68a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
20 changes: 13 additions & 7 deletions custom_components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
STOP_GTFS,
STOP_CODE,
ROUTE,
ROUTE_QUERY,
ROUTE_QUERY_WITH_LIMIT,
MIN_TIME_BETWEEN_UPDATES,
COORDINATOR,
UNDO_UPDATE_LISTENER,
Expand All @@ -27,7 +27,9 @@
ALL,
VAR_ID,
VAR_CURR_EPOCH,
VAR_SECS_LEFT,
VAR_LIMIT,
LIMIT,
SECS_IN_DAY,
_LOGGER
)

Expand Down Expand Up @@ -150,6 +152,13 @@ def parse_data(data, line_from_user = ""):
if arrival is None:
arrival = route.get("scheduledArrival", 0)

## Arrival time is num of secs from midnight when the trip started.
## If the trip starts on this day and arrival time is next day (e.g late night trips)
## the arrival time shows the number of secs more than 24hrs ending up with a
## 1 day, hh:mm:ss on the displays. This corrects it.
if arrival >= SECS_IN_DAY:
arrival = arrival - SECS_IN_DAY

route_dict[DICT_KEY_ARRIVAL] = str(datetime.timedelta(seconds=arrival))
route_dict[DICT_KEY_DEST] = route.get("headsign", "")

Expand Down Expand Up @@ -209,18 +218,15 @@ def parse_data(data, line_from_user = ""):
async with timeout(10):

# Find all the trips for the day
now = datetime.datetime.now()
secs_passed = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
sec_left_in_day = int((24*60*60) - secs_passed)
current_epoch = int(time.time())
variables = {
VAR_ID: self.gtfs_id.upper(),
VAR_CURR_EPOCH: current_epoch,
VAR_SECS_LEFT: sec_left_in_day}
VAR_LIMIT: LIMIT}

# Asynchronous request
data = await self._hass.async_add_executor_job(
graph_client.execute, ROUTE_QUERY, variables
graph_client.execute, ROUTE_QUERY_WITH_LIMIT, variables
)

self.route_data = parse_data(data, self.route)
Expand Down
39 changes: 38 additions & 1 deletion custom_components/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
VAR_ID = "id"
VAR_SECS_LEFT = "sec_left_in_day"
VAR_CURR_EPOCH = "current_epoch"
VAR_LIMIT = "limit"

# Dict keys
DICT_KEY_ROUTE = "route"
Expand All @@ -43,6 +44,9 @@

ATTRIBUTION = "Data provided by Helsinki Regional Transport(HSL HRT)"

LIMIT = 1500
SECS_IN_DAY = 24*60*60

STOP_ID_QUERY = """
query ($name_code: String!) {
stops (name: $name_code) {
Expand All @@ -68,7 +72,7 @@
}
"""

ROUTE_QUERY = """
ROUTE_QUERY_WITH_RANGE = """
query ($id: String!, $current_epoch: Long!, $sec_left_in_day: Int!) {
stop (id: $id) {
name
Expand Down Expand Up @@ -99,4 +103,37 @@
}
}
}
"""

ROUTE_QUERY_WITH_LIMIT = """
query ($id: String!, $current_epoch: Long!, $limit: Int!) {
stop (id: $id) {
name
code
gtfsId
routes {
shortName
patterns {
headsign
}
}
stoptimesWithoutPatterns (startTime: $current_epoch, numberOfDepartures: $limit){
scheduledArrival
realtimeArrival
arrivalDelay
scheduledDeparture
realtimeDeparture
departureDelay
realtime
realtimeState
serviceDay
headsign
trip {
route {
shortName
}
}
}
}
}
"""
1 change: 0 additions & 1 deletion custom_components/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
_LOGGER,
DOMAIN,
COORDINATOR,
ROUTE_QUERY,
STOP_GTFS,
STOP_NAME,
STOP_CODE,
Expand Down

0 comments on commit 75fc68a

Please sign in to comment.