1 """Support for De Lijn (Flemish public transport) information."""
3 from __future__
import annotations
5 from datetime
import datetime
8 from pydelijn.api
import Passages
9 from pydelijn.common
import HttpException
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
24 _LOGGER = logging.getLogger(__name__)
26 ATTRIBUTION =
"Data provided by data.delijn.be"
28 CONF_NEXT_DEPARTURE =
"next_departure"
29 CONF_STOP_ID =
"stop_id"
30 CONF_NUMBER_OF_DEPARTURES =
"number_of_departures"
32 DEFAULT_NAME =
"De Lijn"
34 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
36 vol.Required(CONF_API_KEY): cv.string,
37 vol.Required(CONF_NEXT_DEPARTURE): [
39 vol.Required(CONF_STOP_ID): cv.string,
40 vol.Optional(CONF_NUMBER_OF_DEPARTURES, default=5): cv.positive_int,
48 "line_transport_type",
59 async_add_entities: AddEntitiesCallback,
60 discovery_info: DiscoveryInfoType |
None =
None,
62 """Create the sensor."""
63 api_key = config[CONF_API_KEY]
71 nextpassage[CONF_STOP_ID],
72 nextpassage[CONF_NUMBER_OF_DEPARTURES],
78 for nextpassage
in config[CONF_NEXT_DEPARTURE]
85 """Representation of a Ruter sensor."""
87 _attr_attribution = ATTRIBUTION
88 _attr_device_class = SensorDeviceClass.TIMESTAMP
89 _attr_icon =
"mdi:bus"
92 """Initialize the sensor."""
97 """Get the latest data from the De Lijn API."""
99 await self.
lineline.get_passages()
101 except HttpException:
103 _LOGGER.error(
"De Lijn http error")
108 if not self.
lineline.passages:
113 first = self.
lineline.passages[0]
114 if (first_passage := first[
"due_at_realtime"])
is None:
115 first_passage = first[
"due_at_schedule"]
117 first_passage,
"%Y-%m-%dT%H:%M:%S%z"
120 for key
in AUTO_ATTRIBUTES:
125 except KeyError
as error:
126 _LOGGER.error(
"Invalid data received from De Lijn: %s", error)
_attr_extra_state_attributes
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)