Skip to content

Commit

Permalink
feat: Allow excluding nearby stations (#34)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Cornelicorn authored Apr 11, 2024
1 parent b743bdf commit d633ee0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions custom_components/berlin_transport/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions custom_components/berlin_transport/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion custom_components/berlin_transport/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
Expand Down
1 change: 1 addition & 0 deletions custom_components/berlin_transport/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions custom_components/berlin_transport/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d633ee0

Please sign in to comment.