2022-01-03 21:20:31 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2022-01-03 20:56:40 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.const import TIME_MINUTES
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2022-01-03 21:21:23 +01:00
|
|
|
from .sncf import SNCF, SNCFJourney
|
2022-01-03 20:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
2022-01-03 21:22:52 +01:00
|
|
|
discovery_info: DiscoveryInfoType = None
|
2022-01-03 20:56:40 +01:00
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor platform."""
|
|
|
|
add_entities([SNCFNextJourneys(config)])
|
|
|
|
|
|
|
|
|
|
|
|
class SNCFNextJourneys(SensorEntity):
|
|
|
|
def __init__(self, config: ConfigType):
|
|
|
|
self._config: ConfigType = config
|
|
|
|
self._next_schedules: list[SNCFJourney] = []
|
|
|
|
self._sncf = SNCF(self._config["token"])
|
|
|
|
self._sncf.set_source_stop_point(self._config["source_stop_point"])
|
|
|
|
self._sncf.set_dest_stop_point(self._config["dest_stop_point"])
|
|
|
|
self._sncf.max_transfers = self._config["max_transfers"]
|
|
|
|
self._sncf.max_duration_secs = self._config["max_duration_secs"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TIME_MINUTES
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return SNCF.api_date_to_datetime(self._next_schedules[0]["sections"][0]['departure_date_time'])
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self._next_schedules = self._sncf.get_next_journeys()
|