From d633ee029d0949fc5bc75c4784e003d89b873417 Mon Sep 17 00:00:00 2001 From: Cornelius Hoffmann <40914430+Cornelicorn@users.noreply.github.com> Date: Thu, 11 Apr 2024 03:08:31 -0700 Subject: [PATCH] feat: Allow excluding nearby stations (#34) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BVG groups nearby stations together, e.g. Milastraße also shows departures for Raumerstraße. This is visible in the stop id given in the departure response. Allow excluding stations from the list of departures, so you can only show departures that actually depart at your stop, if wanted. --- README.md | 1 + custom_components/berlin_transport/config_flow.py | 2 ++ custom_components/berlin_transport/const.py | 1 + custom_components/berlin_transport/sensor.py | 14 +++++++++++++- custom_components/berlin_transport/strings.json | 1 + .../berlin_transport/translations/en.json | 1 + 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eefd0d3..9ee0601 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ sensor: - name: "Stargarder Str." # currently you have to add more than one stop to track stop_id: 900000110501 # direction: 900000100002 # Optional stop_id to limit departures for a specific direction (same URL as to find the stop_id), multiple Values can be specified using a comma separated list + # excluded_stops: 900110502,900007102 # Exclude these stop IDs from the departures, duplicate departures may be shown for nearby stations # walking_time: 5 # Optional parameter with value in minutes that hide transport closer than N minutes # show_official_line_colors: true # Optionally enable official VBB line colors. By default predefined colors will be used. # duration: 30 # Optional (default 10), query departures for how many minutes from now? diff --git a/custom_components/berlin_transport/config_flow.py b/custom_components/berlin_transport/config_flow.py index 92175a0..40bacfa 100644 --- a/custom_components/berlin_transport/config_flow.py +++ b/custom_components/berlin_transport/config_flow.py @@ -19,6 +19,7 @@ CONF_DEPARTURES_STOP_ID, CONF_DEPARTURES_NAME, CONF_DEPARTURES_DIRECTION, + CONF_DEPARTURES_EXCLUDED_STOPS, CONF_DEPARTURES_DURATION, CONF_DEPARTURES_WALKING_TIME, CONF_SHOW_API_LINE_COLORS, @@ -37,6 +38,7 @@ DATA_SCHEMA = vol.Schema( { vol.Optional(CONF_DEPARTURES_DIRECTION): cv.string, + vol.Optional(CONF_DEPARTURES_EXCLUDED_STOPS): cv.string, vol.Optional(CONF_DEPARTURES_DURATION): cv.positive_int, vol.Optional(CONF_DEPARTURES_WALKING_TIME, default=1): cv.positive_int, vol.Optional(CONF_SHOW_API_LINE_COLORS, default=False): cv.boolean, diff --git a/custom_components/berlin_transport/const.py b/custom_components/berlin_transport/const.py index b2f97da..f3a5a92 100644 --- a/custom_components/berlin_transport/const.py +++ b/custom_components/berlin_transport/const.py @@ -10,6 +10,7 @@ CONF_DEPARTURES = "departures" CONF_DEPARTURES_NAME = "name" CONF_DEPARTURES_STOP_ID = "stop_id" +CONF_DEPARTURES_EXCLUDED_STOPS = "excluded_stops" CONF_DEPARTURES_WALKING_TIME = "walking_time" CONF_DEPARTURES_DIRECTION = "direction" CONF_DEPARTURES_DURATION = "duration" diff --git a/custom_components/berlin_transport/sensor.py b/custom_components/berlin_transport/sensor.py index 1ac514f..fd238e8 100644 --- a/custom_components/berlin_transport/sensor.py +++ b/custom_components/berlin_transport/sensor.py @@ -23,6 +23,7 @@ API_MAX_RESULTS, CONF_DEPARTURES, CONF_DEPARTURES_DIRECTION, + CONF_DEPARTURES_EXCLUDED_STOPS, CONF_DEPARTURES_DURATION, CONF_DEPARTURES_STOP_ID, CONF_DEPARTURES_WALKING_TIME, @@ -58,6 +59,7 @@ vol.Required(CONF_DEPARTURES_NAME): cv.string, vol.Required(CONF_DEPARTURES_STOP_ID): cv.positive_int, vol.Optional(CONF_DEPARTURES_DIRECTION): cv.string, + vol.Optional(CONF_DEPARTURES_EXCLUDED_STOPS): cv.string, vol.Optional(CONF_DEPARTURES_DURATION): cv.positive_int, vol.Optional(CONF_DEPARTURES_WALKING_TIME, default=1): cv.positive_int, vol.Optional(CONF_SHOW_API_LINE_COLORS, default=False): cv.boolean, @@ -98,6 +100,7 @@ def __init__(self, hass: HomeAssistant, config: dict) -> None: self.hass: HomeAssistant = hass self.config: dict = config self.stop_id: int = config[CONF_DEPARTURES_STOP_ID] + self.excluded_stops: str | None = config.get(CONF_DEPARTURES_EXCLUDED_STOPS) self.sensor_name: str | None = config.get(CONF_DEPARTURES_NAME) self.direction: str | None = config.get(CONF_DEPARTURES_DIRECTION) self.duration: int | None = config.get(CONF_DEPARTURES_DURATION) @@ -177,8 +180,17 @@ def fetch_directional_departure(self, direction: str | None) -> list[Departure]: _LOGGER.error(f"API invalid JSON: {ex}") return [] + if self.excluded_stops is None: + excluded_stops = [] + else: + excluded_stops = self.excluded_stops.split(",") + # convert api data into objects - return [Departure.from_dict(departure) for departure in departures.get("departures")] + return [ + Departure.from_dict(departure) + for departure in departures.get("departures") + if departure["stop"]["id"] not in excluded_stops + ] def fetch_departures(self) -> list[Departure]: departures = [] diff --git a/custom_components/berlin_transport/strings.json b/custom_components/berlin_transport/strings.json index 23322d7..e072bca 100644 --- a/custom_components/berlin_transport/strings.json +++ b/custom_components/berlin_transport/strings.json @@ -22,6 +22,7 @@ "data": { "walking_time": "Walking time in minutes", "direction": "Filter departures by direction", + "excluded_stops": "Exclude nearby stops with IDs", "show_official_line_colors": "Enable official VBB line colors", "duration": "Show departures for how many minutes?", "suburban": "Include S-Bahn", diff --git a/custom_components/berlin_transport/translations/en.json b/custom_components/berlin_transport/translations/en.json index 23322d7..e072bca 100644 --- a/custom_components/berlin_transport/translations/en.json +++ b/custom_components/berlin_transport/translations/en.json @@ -22,6 +22,7 @@ "data": { "walking_time": "Walking time in minutes", "direction": "Filter departures by direction", + "excluded_stops": "Exclude nearby stops with IDs", "show_official_line_colors": "Enable official VBB line colors", "duration": "Show departures for how many minutes?", "suburban": "Include S-Bahn",