from homeassistant.components.sensor import SensorEntity 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 from .sncf import SNCF, SNCFJourney def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType = None ) -> 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()