mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
GTFS fixes (#2119)
* Change to official PyGTFS source * Threading fixes for GTFS * Actually pygtfs 0.1.3 * Update requirements_all.txt * Update gtfs version
This commit is contained in:
parent
3ce6c732ab
commit
0f1c4d2f8c
@ -7,14 +7,15 @@ https://home-assistant.io/components/sensor.gtfs/
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
|
import threading
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
REQUIREMENTS = ["https://github.com/robbiet480/pygtfs/archive/"
|
REQUIREMENTS = ["https://github.com/robbiet480/pygtfs/archive/"
|
||||||
"432414b720c580fb2667a0a48f539118a2d95969.zip#"
|
"00546724e4bbcb3053110d844ca44e2246267dd8.zip#"
|
||||||
"pygtfs==0.1.2"]
|
"pygtfs==0.1.3"]
|
||||||
|
|
||||||
ICON = "mdi:train"
|
ICON = "mdi:train"
|
||||||
|
|
||||||
@ -152,9 +153,22 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.error("The given GTFS data file/folder was not found!")
|
_LOGGER.error("The given GTFS data file/folder was not found!")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
import pygtfs
|
||||||
|
|
||||||
|
split_file_name = os.path.splitext(config["data"])
|
||||||
|
|
||||||
|
sqlite_file = "{}.sqlite".format(split_file_name[0])
|
||||||
|
joined_path = os.path.join(gtfs_dir, sqlite_file)
|
||||||
|
gtfs = pygtfs.Schedule(joined_path)
|
||||||
|
|
||||||
|
# pylint: disable=no-member
|
||||||
|
if len(gtfs.feeds) < 1:
|
||||||
|
pygtfs.append_feed(gtfs, os.path.join(gtfs_dir,
|
||||||
|
config["data"]))
|
||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
dev.append(GTFSDepartureSensor(config["data"], gtfs_dir,
|
dev.append(GTFSDepartureSensor(gtfs, config["origin"],
|
||||||
config["origin"], config["destination"]))
|
config["destination"]))
|
||||||
add_devices(dev)
|
add_devices(dev)
|
||||||
|
|
||||||
# pylint: disable=too-many-instance-attributes,too-few-public-methods
|
# pylint: disable=too-many-instance-attributes,too-few-public-methods
|
||||||
@ -163,16 +177,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
class GTFSDepartureSensor(Entity):
|
class GTFSDepartureSensor(Entity):
|
||||||
"""Implementation of an GTFS departures sensor."""
|
"""Implementation of an GTFS departures sensor."""
|
||||||
|
|
||||||
def __init__(self, data_source, gtfs_folder, origin, destination):
|
def __init__(self, pygtfs, origin, destination):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._data_source = data_source
|
self._pygtfs = pygtfs
|
||||||
self._gtfs_folder = gtfs_folder
|
|
||||||
self.origin = origin
|
self.origin = origin
|
||||||
self.destination = destination
|
self.destination = destination
|
||||||
self._name = "GTFS Sensor"
|
self._name = "GTFS Sensor"
|
||||||
self._unit_of_measurement = "min"
|
self._unit_of_measurement = "min"
|
||||||
self._state = 0
|
self._state = 0
|
||||||
self._attributes = {}
|
self._attributes = {}
|
||||||
|
self.lock = threading.Lock()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -202,19 +216,8 @@ class GTFSDepartureSensor(Entity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from GTFS and update the states."""
|
"""Get the latest data from GTFS and update the states."""
|
||||||
import pygtfs
|
with self.lock:
|
||||||
|
self._departure = get_next_departure(self._pygtfs, self.origin,
|
||||||
split_file_name = os.path.splitext(self._data_source)
|
|
||||||
|
|
||||||
sqlite_file = "{}.sqlite".format(split_file_name[0])
|
|
||||||
gtfs = pygtfs.Schedule(os.path.join(self._gtfs_folder, sqlite_file))
|
|
||||||
|
|
||||||
# pylint: disable=no-member
|
|
||||||
if len(gtfs.feeds) < 1:
|
|
||||||
pygtfs.append_feed(gtfs, os.path.join(self._gtfs_folder,
|
|
||||||
self._data_source))
|
|
||||||
|
|
||||||
self._departure = get_next_departure(gtfs, self.origin,
|
|
||||||
self.destination)
|
self.destination)
|
||||||
self._state = self._departure["minutes_until_departure"]
|
self._state = self._departure["minutes_until_departure"]
|
||||||
|
|
||||||
@ -258,6 +261,7 @@ class GTFSDepartureSensor(Entity):
|
|||||||
append_keys(dict_for_table(route), "Route")
|
append_keys(dict_for_table(route), "Route")
|
||||||
append_keys(dict_for_table(trip), "Trip")
|
append_keys(dict_for_table(trip), "Trip")
|
||||||
append_keys(dict_for_table(origin_station), "Origin Station")
|
append_keys(dict_for_table(origin_station), "Origin Station")
|
||||||
append_keys(dict_for_table(destination_station), "Destination Station")
|
append_keys(dict_for_table(destination_station),
|
||||||
|
"Destination Station")
|
||||||
append_keys(origin_stop_time, "Origin Stop")
|
append_keys(origin_stop_time, "Origin Stop")
|
||||||
append_keys(destination_stop_time, "Destination Stop")
|
append_keys(destination_stop_time, "Destination Stop")
|
||||||
|
@ -131,7 +131,7 @@ https://github.com/rkabadi/pyedimax/archive/365301ce3ff26129a7910c501ead09ea625f
|
|||||||
https://github.com/rkabadi/temper-python/archive/3dbdaf2d87b8db9a3cd6e5585fc704537dd2d09b.zip#temperusb==1.2.3
|
https://github.com/rkabadi/temper-python/archive/3dbdaf2d87b8db9a3cd6e5585fc704537dd2d09b.zip#temperusb==1.2.3
|
||||||
|
|
||||||
# homeassistant.components.sensor.gtfs
|
# homeassistant.components.sensor.gtfs
|
||||||
https://github.com/robbiet480/pygtfs/archive/432414b720c580fb2667a0a48f539118a2d95969.zip#pygtfs==0.1.2
|
https://github.com/robbiet480/pygtfs/archive/00546724e4bbcb3053110d844ca44e2246267dd8.zip#pygtfs==0.1.3
|
||||||
|
|
||||||
# homeassistant.components.scene.hunterdouglas_powerview
|
# homeassistant.components.scene.hunterdouglas_powerview
|
||||||
https://github.com/sander76/powerviewApi/archive/master.zip#powerviewApi==0.2
|
https://github.com/sander76/powerviewApi/archive/master.zip#powerviewApi==0.2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user