From c4cc9f84891ca1f060f9828ea53e258ea845f3d7 Mon Sep 17 00:00:00 2001
From: polgarc <29740673+polgarc@users.noreply.github.com>
Date: Thu, 3 Oct 2024 01:25:17 +0200
Subject: [PATCH] Add line numbers to swiss-public-transport (#127332)

* add line numbers

* keep only one line sensor

* fix unt tests
---
 .../swiss_public_transport/coordinator.py     |  2 +
 .../swiss_public_transport/icons.json         |  3 ++
 .../swiss_public_transport/sensor.py          |  5 ++
 .../swiss_public_transport/strings.json       |  3 ++
 .../fixtures/connections.json                 | 48 ++++++++++++-------
 .../swiss_public_transport/test_init.py       |  3 ++
 6 files changed, 48 insertions(+), 16 deletions(-)

diff --git a/homeassistant/components/swiss_public_transport/coordinator.py b/homeassistant/components/swiss_public_transport/coordinator.py
index 114215520ac..f91f9a7c768 100644
--- a/homeassistant/components/swiss_public_transport/coordinator.py
+++ b/homeassistant/components/swiss_public_transport/coordinator.py
@@ -34,6 +34,7 @@ class DataConnection(TypedDict):
     train_number: str
     transfers: int
     delay: int
+    line: str
 
 
 def calculate_duration_in_seconds(duration_text: str) -> int | None:
@@ -104,6 +105,7 @@ class SwissPublicTransportDataUpdateCoordinator(
                 destination=self._opendata.to_name,
                 remaining_time=str(self.remaining_time(connections[i]["departure"])),
                 delay=connections[i]["delay"],
+                line=connections[i]["line"],
             )
             for i in range(limit)
             if len(connections) > i and connections[i] is not None
diff --git a/homeassistant/components/swiss_public_transport/icons.json b/homeassistant/components/swiss_public_transport/icons.json
index 0f868c18c1f..06a640a06b2 100644
--- a/homeassistant/components/swiss_public_transport/icons.json
+++ b/homeassistant/components/swiss_public_transport/icons.json
@@ -21,6 +21,9 @@
       },
       "delay": {
         "default": "mdi:clock-plus"
+      },
+      "line": {
+        "default": "mdi:transit-connection-variant"
       }
     }
   },
diff --git a/homeassistant/components/swiss_public_transport/sensor.py b/homeassistant/components/swiss_public_transport/sensor.py
index c186b963705..eb73ce03062 100644
--- a/homeassistant/components/swiss_public_transport/sensor.py
+++ b/homeassistant/components/swiss_public_transport/sensor.py
@@ -71,6 +71,11 @@ SENSORS: tuple[SwissPublicTransportSensorEntityDescription, ...] = (
         native_unit_of_measurement=UnitOfTime.MINUTES,
         value_fn=lambda data_connection: data_connection["delay"],
     ),
+    SwissPublicTransportSensorEntityDescription(
+        key="line",
+        translation_key="line",
+        value_fn=lambda data_connection: data_connection["line"],
+    ),
 )
 
 
diff --git a/homeassistant/components/swiss_public_transport/strings.json b/homeassistant/components/swiss_public_transport/strings.json
index 29e73978538..b3bfd9aea8f 100644
--- a/homeassistant/components/swiss_public_transport/strings.json
+++ b/homeassistant/components/swiss_public_transport/strings.json
@@ -46,6 +46,9 @@
       },
       "delay": {
         "name": "Delay"
+      },
+      "line": {
+        "name": "Line"
       }
     }
   },
diff --git a/tests/components/swiss_public_transport/fixtures/connections.json b/tests/components/swiss_public_transport/fixtures/connections.json
index 4edead56f14..f2cd1014e63 100644
--- a/tests/components/swiss_public_transport/fixtures/connections.json
+++ b/tests/components/swiss_public_transport/fixtures/connections.json
@@ -5,7 +5,8 @@
     "platform": 0,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:04:00+0100",
@@ -13,7 +14,8 @@
     "platform": 1,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": null
   },
   {
     "departure": "2024-01-06T18:05:00+0100",
@@ -21,7 +23,8 @@
     "platform": 2,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:06:00+0100",
@@ -29,7 +32,8 @@
     "platform": 3,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:07:00+0100",
@@ -37,7 +41,8 @@
     "platform": 4,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:08:00+0100",
@@ -45,7 +50,8 @@
     "platform": 5,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:09:00+0100",
@@ -53,7 +59,8 @@
     "platform": 6,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:10:00+0100",
@@ -61,7 +68,8 @@
     "platform": 7,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:11:00+0100",
@@ -69,7 +77,8 @@
     "platform": 8,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:12:00+0100",
@@ -77,7 +86,8 @@
     "platform": 9,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:13:00+0100",
@@ -85,7 +95,8 @@
     "platform": 10,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:14:00+0100",
@@ -93,7 +104,8 @@
     "platform": 11,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:15:00+0100",
@@ -101,7 +113,8 @@
     "platform": 12,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:16:00+0100",
@@ -109,7 +122,8 @@
     "platform": 13,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:17:00+0100",
@@ -117,7 +131,8 @@
     "platform": 14,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   },
   {
     "departure": "2024-01-06T18:18:00+0100",
@@ -125,6 +140,7 @@
     "platform": 15,
     "transfers": 0,
     "duration": "10",
-    "delay": 0
+    "delay": 0,
+    "line": "T10"
   }
 ]
diff --git a/tests/components/swiss_public_transport/test_init.py b/tests/components/swiss_public_transport/test_init.py
index 7ee8b696499..9ad4a8d50b0 100644
--- a/tests/components/swiss_public_transport/test_init.py
+++ b/tests/components/swiss_public_transport/test_init.py
@@ -36,6 +36,7 @@ CONNECTIONS = [
         "transfers": 0,
         "duration": "10",
         "delay": 0,
+        "line": "T10",
     },
     {
         "departure": "2024-01-06T18:04:00+0100",
@@ -44,6 +45,7 @@ CONNECTIONS = [
         "transfers": 0,
         "duration": "10",
         "delay": 0,
+        "line": "T10",
     },
     {
         "departure": "2024-01-06T18:05:00+0100",
@@ -52,6 +54,7 @@ CONNECTIONS = [
         "transfers": 0,
         "duration": "10",
         "delay": 0,
+        "line": "T10",
     },
 ]
 
-- 
GitLab