Search specific train in Nederlandse Spoorwegen (#28898)
* Nederlandse Spoorwegen: search for specific trip * Reformatting with Black * Resolve pylint error * Reformat with black.pull/31418/head
parent
f701b2245a
commit
75f1e573e4
|
@ -20,6 +20,7 @@ CONF_ROUTES = "routes"
|
||||||
CONF_FROM = "from"
|
CONF_FROM = "from"
|
||||||
CONF_TO = "to"
|
CONF_TO = "to"
|
||||||
CONF_VIA = "via"
|
CONF_VIA = "via"
|
||||||
|
CONF_TIME = "time"
|
||||||
|
|
||||||
ICON = "mdi:train"
|
ICON = "mdi:train"
|
||||||
|
|
||||||
|
@ -31,6 +32,7 @@ ROUTE_SCHEMA = vol.Schema(
|
||||||
vol.Required(CONF_FROM): cv.string,
|
vol.Required(CONF_FROM): cv.string,
|
||||||
vol.Required(CONF_TO): cv.string,
|
vol.Required(CONF_TO): cv.string,
|
||||||
vol.Optional(CONF_VIA): cv.string,
|
vol.Optional(CONF_VIA): cv.string,
|
||||||
|
vol.Optional(CONF_TIME): cv.time,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,6 +70,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
departure.get(CONF_FROM),
|
departure.get(CONF_FROM),
|
||||||
departure.get(CONF_TO),
|
departure.get(CONF_TO),
|
||||||
departure.get(CONF_VIA),
|
departure.get(CONF_VIA),
|
||||||
|
departure.get(CONF_TIME),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if sensors:
|
if sensors:
|
||||||
|
@ -88,13 +91,14 @@ def valid_stations(stations, given_stations):
|
||||||
class NSDepartureSensor(Entity):
|
class NSDepartureSensor(Entity):
|
||||||
"""Implementation of a NS Departure Sensor."""
|
"""Implementation of a NS Departure Sensor."""
|
||||||
|
|
||||||
def __init__(self, nsapi, name, departure, heading, via):
|
def __init__(self, nsapi, name, departure, heading, via, time):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._nsapi = nsapi
|
self._nsapi = nsapi
|
||||||
self._name = name
|
self._name = name
|
||||||
self._departure = departure
|
self._departure = departure
|
||||||
self._via = via
|
self._via = via
|
||||||
self._heading = heading
|
self._heading = heading
|
||||||
|
self._time = time
|
||||||
self._state = None
|
self._state = None
|
||||||
self._trips = None
|
self._trips = None
|
||||||
|
|
||||||
|
@ -180,15 +184,29 @@ class NSDepartureSensor(Entity):
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the trip information."""
|
"""Get the trip information."""
|
||||||
|
|
||||||
|
# If looking for a specific trip time, update around that trip time only.
|
||||||
|
if self._time and (
|
||||||
|
(datetime.now() + timedelta(minutes=30)).time() < self._time
|
||||||
|
or (datetime.now() - timedelta(minutes=30)).time() > self._time
|
||||||
|
):
|
||||||
|
self._state = None
|
||||||
|
self._trips = None
|
||||||
|
return
|
||||||
|
|
||||||
|
# Set the search parameter to search from a specific trip time or to just search for next trip.
|
||||||
|
if self._time:
|
||||||
|
trip_time = (
|
||||||
|
datetime.today()
|
||||||
|
.replace(hour=self._time.hour, minute=self._time.minute)
|
||||||
|
.strftime("%d-%m-%Y %H:%M")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
trip_time = datetime.now().strftime("%d-%m-%Y %H:%M")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._trips = self._nsapi.get_trips(
|
self._trips = self._nsapi.get_trips(
|
||||||
datetime.now().strftime("%d-%m-%Y %H:%M"),
|
trip_time, self._departure, self._via, self._heading, True, 0, 2,
|
||||||
self._departure,
|
|
||||||
self._via,
|
|
||||||
self._heading,
|
|
||||||
True,
|
|
||||||
0,
|
|
||||||
2,
|
|
||||||
)
|
)
|
||||||
if self._trips:
|
if self._trips:
|
||||||
if self._trips[0].departure_time_actual is None:
|
if self._trips[0].departure_time_actual is None:
|
||||||
|
|
Loading…
Reference in New Issue