mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Add more typings to nmbs sensor (#135359)
This commit is contained in:
parent
74c3e9629f
commit
8e2b284a7f
@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from pyrail import iRail
|
from pyrail import iRail
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -180,31 +181,37 @@ class NMBSLiveBoard(SensorEntity):
|
|||||||
|
|
||||||
_attr_attribution = "https://api.irail.be/"
|
_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."""
|
"""Initialize the sensor for getting liveboard data."""
|
||||||
self._station = live_station
|
self._station = live_station
|
||||||
self._api_client = api_client
|
self._api_client = api_client
|
||||||
self._station_from = station_from
|
self._station_from = station_from
|
||||||
self._station_to = station_to
|
self._station_to = station_to
|
||||||
self._attrs = {}
|
self._attrs: dict[str, Any] | None = {}
|
||||||
self._state = None
|
self._state: str | None = None
|
||||||
|
|
||||||
self.entity_registry_enabled_default = False
|
self.entity_registry_enabled_default = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the sensor default name."""
|
"""Return the sensor default name."""
|
||||||
return f"Trains in {self._station["standardname"]}"
|
return f"Trains in {self._station["standardname"]}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique ID."""
|
"""Return the unique ID."""
|
||||||
|
|
||||||
unique_id = f"{self._station}_{self._station_from}_{self._station_to}"
|
unique_id = f"{self._station}_{self._station_from}_{self._station_to}"
|
||||||
return f"nmbs_live_{unique_id}"
|
return f"nmbs_live_{unique_id}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self) -> str:
|
||||||
"""Return the default icon or an alert icon if delays."""
|
"""Return the default icon or an alert icon if delays."""
|
||||||
if self._attrs and int(self._attrs["delay"]) > 0:
|
if self._attrs and int(self._attrs["delay"]) > 0:
|
||||||
return DEFAULT_ICON_ALERT
|
return DEFAULT_ICON_ALERT
|
||||||
@ -212,12 +219,12 @@ class NMBSLiveBoard(SensorEntity):
|
|||||||
return DEFAULT_ICON
|
return DEFAULT_ICON
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self) -> str | None:
|
||||||
"""Return sensor state."""
|
"""Return sensor state."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||||
"""Return the sensor attributes if data is available."""
|
"""Return the sensor attributes if data is available."""
|
||||||
if self._state is None or not self._attrs:
|
if self._state is None or not self._attrs:
|
||||||
return None
|
return None
|
||||||
@ -270,8 +277,14 @@ class NMBSSensor(SensorEntity):
|
|||||||
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
|
_attr_native_unit_of_measurement = UnitOfTime.MINUTES
|
||||||
|
|
||||||
def __init__(
|
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."""
|
"""Initialize the NMBS connection sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._show_on_map = show_on_map
|
self._show_on_map = show_on_map
|
||||||
@ -280,7 +293,7 @@ class NMBSSensor(SensorEntity):
|
|||||||
self._station_to = station_to
|
self._station_to = station_to
|
||||||
self._excl_vias = excl_vias
|
self._excl_vias = excl_vias
|
||||||
|
|
||||||
self._attrs = {}
|
self._attrs: dict[str, Any] | None = {}
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -298,7 +311,7 @@ class NMBSSensor(SensorEntity):
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self) -> str:
|
||||||
"""Return the sensor default icon or an alert icon if any delay."""
|
"""Return the sensor default icon or an alert icon if any delay."""
|
||||||
if self._attrs:
|
if self._attrs:
|
||||||
delay = get_delay_in_minutes(self._attrs["departure"]["delay"])
|
delay = get_delay_in_minutes(self._attrs["departure"]["delay"])
|
||||||
@ -308,7 +321,7 @@ class NMBSSensor(SensorEntity):
|
|||||||
return "mdi:train"
|
return "mdi:train"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||||
"""Return sensor attributes if data is available."""
|
"""Return sensor attributes if data is available."""
|
||||||
if self._state is None or not self._attrs:
|
if self._state is None or not self._attrs:
|
||||||
return None
|
return None
|
||||||
@ -355,12 +368,12 @@ class NMBSSensor(SensorEntity):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self) -> int | None:
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def station_coordinates(self):
|
def station_coordinates(self) -> list[float]:
|
||||||
"""Get the lat, long coordinates for station."""
|
"""Get the lat, long coordinates for station."""
|
||||||
if self._state is None or not self._attrs:
|
if self._state is None or not self._attrs:
|
||||||
return []
|
return []
|
||||||
@ -370,7 +383,7 @@ class NMBSSensor(SensorEntity):
|
|||||||
return [latitude, longitude]
|
return [latitude, longitude]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_via_connection(self):
|
def is_via_connection(self) -> bool:
|
||||||
"""Return whether the connection goes through another station."""
|
"""Return whether the connection goes through another station."""
|
||||||
if not self._attrs:
|
if not self._attrs:
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user