Make combined rmvtransport filters work (#126255)

rmvtransport: make filters always effective

In the `rmvtransport` integration, the three config attributes
`destination`, `lines`, and `time_offset` all act as filters. The
expectation is that if multiple filters are given, all of them take
effect.

However, as a consequence of using `elif` in the loop body, if a
`destination` filter has been configured, then both the `lines` and the
`time_offset` filters are ignored and have no effect.

Replace the `elif` with an `if` clause to allow all filter settings to
work as intended.

CC: @cgtobi
pull/126267/head
Claudia Pellegrino 2024-09-19 16:11:13 +02:00 committed by GitHub
parent b2d669ac3c
commit baa79303a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 1 deletions

View File

@ -271,7 +271,7 @@ class RMVDepartureData:
if not dest_found:
continue
elif (
if (
self._lines
and journey["number"] not in self._lines
or journey["minutes"] < self._time_offset

View File

@ -32,6 +32,23 @@ VALID_CONFIG_MISC = {
}
VALID_CONFIG_DEST = {
"sensor": {
"platform": "rmvtransport",
"next_departure": [
{
"station": "3000010",
"destinations": [
"Frankfurt (Main) Flughafen Regionalbahnhof",
"Frankfurt (Main) Stadion",
],
"lines": [12, "S8"],
"time_offset": 15,
}
],
}
}
VALID_CONFIG_DEST_ONLY = {
"sensor": {
"platform": "rmvtransport",
"next_departure": [
@ -144,6 +161,19 @@ def get_departures_mock():
"info_long": None,
"icon": "https://products/32_pic.png",
},
{
"product": "Bus",
"number": 12,
"trainId": "1234568",
"direction": "Frankfurt (Main) Hugo-Junkers-Straße/Schleife",
"departure_time": datetime.datetime(2018, 8, 6, 14, 30),
"minutes": 16,
"delay": 0,
"stops": ["Frankfurt (Main) Stadion"],
"info": None,
"info_long": None,
"icon": "https://products/32_pic.png",
},
],
}
@ -215,6 +245,26 @@ async def test_rmvtransport_dest_config(hass: HomeAssistant) -> None:
assert await async_setup_component(hass, "sensor", VALID_CONFIG_DEST)
await hass.async_block_till_done()
state = hass.states.get("sensor.frankfurt_main_hauptbahnhof")
assert state is not None
assert state.state == "16"
assert (
state.attributes["direction"] == "Frankfurt (Main) Hugo-Junkers-Straße/Schleife"
)
assert state.attributes["line"] == 12
assert state.attributes["minutes"] == 16
assert state.attributes["departure_time"] == datetime.datetime(2018, 8, 6, 14, 30)
async def test_rmvtransport_dest_only_config(hass: HomeAssistant) -> None:
"""Test destination configuration."""
with patch(
"RMVtransport.RMVtransport.get_departures",
return_value=get_departures_mock(),
):
assert await async_setup_component(hass, "sensor", VALID_CONFIG_DEST_ONLY)
await hass.async_block_till_done()
state = hass.states.get("sensor.frankfurt_main_hauptbahnhof")
assert state.state == "11"
assert (