From baa79303a7147ec1a67b9112ea696bc27d8d44df Mon Sep 17 00:00:00 2001 From: Claudia Pellegrino Date: Thu, 19 Sep 2024 16:11:13 +0200 Subject: [PATCH] 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 --- .../components/rmvtransport/sensor.py | 2 +- tests/components/rmvtransport/test_sensor.py | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/rmvtransport/sensor.py b/homeassistant/components/rmvtransport/sensor.py index f9ad4e24631..8fd437e7e1d 100644 --- a/homeassistant/components/rmvtransport/sensor.py +++ b/homeassistant/components/rmvtransport/sensor.py @@ -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 diff --git a/tests/components/rmvtransport/test_sensor.py b/tests/components/rmvtransport/test_sensor.py index c17eaac2105..47728be438c 100644 --- a/tests/components/rmvtransport/test_sensor.py +++ b/tests/components/rmvtransport/test_sensor.py @@ -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 (