Add more typings to nmbs sensor (#135359)

This commit is contained in:
Simon Lamon 2025-01-11 13:04:37 +01:00 committed by GitHub
parent 74c3e9629f
commit 8e2b284a7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
from __future__ import annotations
import logging
from typing import Any
from pyrail import iRail
import voluptuous as vol
@ -180,31 +181,37 @@ class NMBSLiveBoard(SensorEntity):
_attr_attribution = "https://api.irail.be/"
def __init__(self, api_client, live_station, station_from, station_to):
def __init__(
self,
api_client: iRail,
live_station: dict[str, Any],
station_from: dict[str, Any],
station_to: dict[str, Any],
) -> None:
"""Initialize the sensor for getting liveboard data."""
self._station = live_station
self._api_client = api_client
self._station_from = station_from
self._station_to = station_to
self._attrs = {}
self._state = None
self._attrs: dict[str, Any] | None = {}
self._state: str | None = None
self.entity_registry_enabled_default = False
@property
def name(self):
def name(self) -> str:
"""Return the sensor default name."""
return f"Trains in {self._station["standardname"]}"
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return the unique ID."""
unique_id = f"{self._station}_{self._station_from}_{self._station_to}"
return f"nmbs_live_{unique_id}"
@property
def icon(self):
def icon(self) -> str:
"""Return the default icon or an alert icon if delays."""
if self._attrs and int(self._attrs["delay"]) > 0:
return DEFAULT_ICON_ALERT
@ -212,12 +219,12 @@ class NMBSLiveBoard(SensorEntity):
return DEFAULT_ICON
@property
def native_value(self):
def native_value(self) -> str | None:
"""Return sensor state."""
return self._state
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the sensor attributes if data is available."""
if self._state is None or not self._attrs:
return None
@ -270,8 +277,14 @@ class NMBSSensor(SensorEntity):
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
def __init__(
self, api_client, name, show_on_map, station_from, station_to, excl_vias
):
self,
api_client: iRail,
name: str,
show_on_map: bool,
station_from: dict[str, Any],
station_to: dict[str, Any],
excl_vias: bool,
) -> None:
"""Initialize the NMBS connection sensor."""
self._name = name
self._show_on_map = show_on_map
@ -280,7 +293,7 @@ class NMBSSensor(SensorEntity):
self._station_to = station_to
self._excl_vias = excl_vias
self._attrs = {}
self._attrs: dict[str, Any] | None = {}
self._state = None
@property
@ -298,7 +311,7 @@ class NMBSSensor(SensorEntity):
return self._name
@property
def icon(self):
def icon(self) -> str:
"""Return the sensor default icon or an alert icon if any delay."""
if self._attrs:
delay = get_delay_in_minutes(self._attrs["departure"]["delay"])
@ -308,7 +321,7 @@ class NMBSSensor(SensorEntity):
return "mdi:train"
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return sensor attributes if data is available."""
if self._state is None or not self._attrs:
return None
@ -355,12 +368,12 @@ class NMBSSensor(SensorEntity):
return attrs
@property
def native_value(self):
def native_value(self) -> int | None:
"""Return the state of the device."""
return self._state
@property
def station_coordinates(self):
def station_coordinates(self) -> list[float]:
"""Get the lat, long coordinates for station."""
if self._state is None or not self._attrs:
return []
@ -370,7 +383,7 @@ class NMBSSensor(SensorEntity):
return [latitude, longitude]
@property
def is_via_connection(self):
def is_via_connection(self) -> bool:
"""Return whether the connection goes through another station."""
if not self._attrs:
return False