mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Add offset option to sensor.gtfs (#7980)
* Add offset option to sensor.gtfs * Fix long lines in sensor.gtfs * Expose GTFS offset as an attribute
This commit is contained in:
parent
c1095665e9
commit
edeb92ea42
@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
CONF_DATA = 'data'
|
||||
CONF_DESTINATION = 'destination'
|
||||
CONF_ORIGIN = 'origin'
|
||||
CONF_OFFSET = 'offset'
|
||||
|
||||
DEFAULT_NAME = 'GTFS Sensor'
|
||||
DEFAULT_PATH = 'gtfs'
|
||||
@ -38,15 +39,17 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
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')
|
||||
|
||||
@ -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,
|
||||
@ -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)
|
||||
@ -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'
|
||||
@ -223,7 +228,7 @@ class GTFSDepartureSensor(Entity):
|
||||
"""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'}
|
||||
@ -249,6 +254,7 @@ class GTFSDepartureSensor(Entity):
|
||||
|
||||
# Build attributes
|
||||
self._attributes = {}
|
||||
self._attributes['offset'] = self._offset.seconds / 60
|
||||
|
||||
def dict_for_table(resource):
|
||||
"""Return a dict for the SQLAlchemy resource given."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user