|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import datetime as dt |
5 | 6 | from datetime import datetime, timedelta |
6 | 7 | import logging |
| 8 | +from typing import Any |
7 | 9 |
|
| 10 | +from ns_api import NSAPI, Trip |
8 | 11 | import requests |
9 | 12 | import voluptuous as vol |
10 | 13 |
|
@@ -142,31 +145,39 @@ class NSDepartureSensor(SensorEntity): |
142 | 145 | _attr_attribution = "Data provided by NS" |
143 | 146 | _attr_icon = "mdi:train" |
144 | 147 |
|
145 | | - def __init__(self, nsapi, name, departure, heading, via, time): |
| 148 | + def __init__( |
| 149 | + self, |
| 150 | + nsapi: NSAPI, |
| 151 | + name: str, |
| 152 | + departure: str, |
| 153 | + heading: str, |
| 154 | + via: str | None, |
| 155 | + time: dt.time | None, |
| 156 | + ) -> None: |
146 | 157 | """Initialize the sensor.""" |
147 | 158 | self._nsapi = nsapi |
148 | 159 | self._name = name |
149 | 160 | self._departure = departure |
150 | 161 | self._via = via |
151 | 162 | self._heading = heading |
152 | 163 | self._time = time |
153 | | - self._state = None |
154 | | - self._trips = None |
155 | | - self._first_trip = None |
156 | | - self._next_trip = None |
| 164 | + self._state: str | None = None |
| 165 | + self._trips: list[Trip] | None = None |
| 166 | + self._first_trip: Trip | None = None |
| 167 | + self._next_trip: Trip | None = None |
157 | 168 |
|
158 | 169 | @property |
159 | | - def name(self): |
| 170 | + def name(self) -> str: |
160 | 171 | """Return the name of the sensor.""" |
161 | 172 | return self._name |
162 | 173 |
|
163 | 174 | @property |
164 | | - def native_value(self): |
| 175 | + def native_value(self) -> str | None: |
165 | 176 | """Return the next departure time.""" |
166 | 177 | return self._state |
167 | 178 |
|
168 | 179 | @property |
169 | | - def extra_state_attributes(self): |
| 180 | + def extra_state_attributes(self) -> dict[str, Any] | None: |
170 | 181 | """Return the state attributes.""" |
171 | 182 | if not self._trips or self._first_trip is None: |
172 | 183 | return None |
@@ -236,6 +247,7 @@ def extra_state_attributes(self): |
236 | 247 | ): |
237 | 248 | attributes["arrival_delay"] = True |
238 | 249 |
|
| 250 | + assert self._next_trip is not None |
239 | 251 | # Next attributes |
240 | 252 | if self._next_trip.departure_time_actual is not None: |
241 | 253 | attributes["next"] = self._next_trip.departure_time_actual.strftime("%H:%M") |
|
0 commit comments