diff --git a/homeassistant/components/nederlandse_spoorwegen/sensor.py b/homeassistant/components/nederlandse_spoorwegen/sensor.py index 5477aaf0e2b..7e72db57441 100644 --- a/homeassistant/components/nederlandse_spoorwegen/sensor.py +++ b/homeassistant/components/nederlandse_spoorwegen/sensor.py @@ -20,6 +20,7 @@ CONF_ROUTES = "routes" CONF_FROM = "from" CONF_TO = "to" CONF_VIA = "via" +CONF_TIME = "time" ICON = "mdi:train" @@ -31,6 +32,7 @@ ROUTE_SCHEMA = vol.Schema( vol.Required(CONF_FROM): cv.string, vol.Required(CONF_TO): 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_TO), departure.get(CONF_VIA), + departure.get(CONF_TIME), ) ) if sensors: @@ -88,13 +91,14 @@ def valid_stations(stations, given_stations): class NSDepartureSensor(Entity): """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.""" self._nsapi = nsapi self._name = name self._departure = departure self._via = via self._heading = heading + self._time = time self._state = None self._trips = None @@ -180,15 +184,29 @@ class NSDepartureSensor(Entity): @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """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: self._trips = self._nsapi.get_trips( - datetime.now().strftime("%d-%m-%Y %H:%M"), - self._departure, - self._via, - self._heading, - True, - 0, - 2, + trip_time, self._departure, self._via, self._heading, True, 0, 2, ) if self._trips: if self._trips[0].departure_time_actual is None: