Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions homeassistant/components/sensor/gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CONF_DATA = 'data'
CONF_DESTINATION = 'destination'
CONF_ORIGIN = 'origin'
CONF_OFFSET = 'offset'

DEFAULT_NAME = 'GTFS Sensor'
DEFAULT_PATH = 'gtfs'
Expand All @@ -38,15 +39,17 @@
vol.Required(CONF_DESTINATION): cv.string,
vol.Required(CONF_DATA): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_OFFSET, default=datetime.timedelta(0)):
cv.time_period_dict,
})


def get_next_departure(sched, start_station_id, end_station_id):
def get_next_departure(sched, start_station_id, end_station_id, offset):
"""Get the next departure for the given schedule."""
origin_station = sched.stops_by_id(start_station_id)[0]
destination_station = sched.stops_by_id(end_station_id)[0]

now = datetime.datetime.now()
now = datetime.datetime.now() + offset
day_name = now.strftime('%A').lower()
now_str = now.strftime('%H:%M:%S')

Expand Down Expand Up @@ -98,7 +101,7 @@ def get_next_departure(sched, start_station_id, end_station_id):
if item == {}:
return None

today = datetime.datetime.today().strftime('%Y-%m-%d')
today = now.strftime('%Y-%m-%d')
departure_time_string = '{} {}'.format(today, item[2])
arrival_time_string = '{} {}'.format(today, item[3])
departure_time = datetime.datetime.strptime(departure_time_string,
Expand Down Expand Up @@ -155,6 +158,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
origin = config.get(CONF_ORIGIN)
destination = config.get(CONF_DESTINATION)
name = config.get(CONF_NAME)
offset = config.get(CONF_OFFSET)

if not os.path.exists(gtfs_dir):
os.makedirs(gtfs_dir)
Expand All @@ -175,17 +179,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if len(gtfs.feeds) < 1:
pygtfs.append_feed(gtfs, os.path.join(gtfs_dir, data))

add_devices([GTFSDepartureSensor(gtfs, name, origin, destination)])
add_devices([GTFSDepartureSensor(gtfs, name, origin, destination, offset)])


class GTFSDepartureSensor(Entity):
"""Implementation of an GTFS departures sensor."""

def __init__(self, pygtfs, name, origin, destination):
def __init__(self, pygtfs, name, origin, destination, offset):
"""Initialize the sensor."""
self._pygtfs = pygtfs
self.origin = origin
self.destination = destination
self._offset = offset
self._custom_name = name
self._name = ''
self._unit_of_measurement = 'min'
Expand Down Expand Up @@ -223,7 +228,7 @@ def update(self):
"""Get the latest data from GTFS and update the states."""
with self.lock:
self._departure = get_next_departure(
self._pygtfs, self.origin, self.destination)
self._pygtfs, self.origin, self.destination, self._offset)
if not self._departure:
self._state = 0
self._attributes = {'Info': 'No more departures today'}
Expand All @@ -249,6 +254,7 @@ def update(self):

# Build attributes
self._attributes = {}
self._attributes['offset'] = self._offset.seconds / 60

def dict_for_table(resource):
"""Return a dict for the SQLAlchemy resource given."""
Expand Down