2019-07-26 14:41:02 +00:00
|
|
|
"""Support for De Lijn (Flemish public transport) information."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from pydelijn.api import Passages
|
2020-05-20 12:53:01 +00:00
|
|
|
from pydelijn.common import HttpException
|
2019-07-26 14:41:02 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-12-09 10:39:57 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
2019-07-26 14:41:02 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTRIBUTION = "Data provided by data.delijn.be"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NEXT_DEPARTURE = "next_departure"
|
|
|
|
CONF_STOP_ID = "stop_id"
|
|
|
|
CONF_NUMBER_OF_DEPARTURES = "number_of_departures"
|
2019-07-26 14:41:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "De Lijn"
|
2019-07-26 14:41:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_NEXT_DEPARTURE): [
|
|
|
|
{
|
|
|
|
vol.Required(CONF_STOP_ID): cv.string,
|
|
|
|
vol.Optional(CONF_NUMBER_OF_DEPARTURES, default=5): cv.positive_int,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2019-07-26 14:41:02 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-07-26 14:41:02 +00:00
|
|
|
"""Create the sensor."""
|
|
|
|
api_key = config[CONF_API_KEY]
|
|
|
|
|
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
|
|
|
|
sensors = []
|
|
|
|
for nextpassage in config[CONF_NEXT_DEPARTURE]:
|
2020-05-20 12:53:01 +00:00
|
|
|
sensors.append(
|
|
|
|
DeLijnPublicTransportSensor(
|
|
|
|
Passages(
|
|
|
|
hass.loop,
|
|
|
|
nextpassage[CONF_STOP_ID],
|
|
|
|
nextpassage[CONF_NUMBER_OF_DEPARTURES],
|
|
|
|
api_key,
|
|
|
|
session,
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-07-26 14:41:02 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class DeLijnPublicTransportSensor(SensorEntity):
|
2019-07-26 14:41:02 +00:00
|
|
|
"""Representation of a Ruter sensor."""
|
|
|
|
|
2021-12-09 10:39:57 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
|
|
|
_attr_icon = "mdi:bus"
|
2021-05-31 08:50:11 +00:00
|
|
|
|
2020-05-20 12:53:01 +00:00
|
|
|
def __init__(self, line):
|
2019-07-26 14:41:02 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.line = line
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2019-07-26 14:41:02 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Get the latest data from the De Lijn API."""
|
2020-05-20 12:53:01 +00:00
|
|
|
try:
|
|
|
|
await self.line.get_passages()
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_name = await self.line.get_stopname()
|
2020-05-20 12:53:01 +00:00
|
|
|
except HttpException:
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_available = False
|
2020-05-20 12:53:01 +00:00
|
|
|
_LOGGER.error("De Lijn http error")
|
2019-07-26 14:41:02 +00:00
|
|
|
return
|
2020-05-20 12:53:01 +00:00
|
|
|
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_extra_state_attributes["stopname"] = self._attr_name
|
2020-05-20 12:53:01 +00:00
|
|
|
|
2021-11-14 02:22:36 +00:00
|
|
|
if not self.line.passages:
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_available = False
|
2021-11-14 02:22:36 +00:00
|
|
|
return
|
|
|
|
|
2019-07-26 14:41:02 +00:00
|
|
|
try:
|
|
|
|
first = self.line.passages[0]
|
2021-12-09 10:39:57 +00:00
|
|
|
if (first_passage := first["due_at_realtime"]) is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
first_passage = first["due_at_schedule"]
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_native_value = first_passage
|
|
|
|
self._attr_extra_state_attributes.update(
|
|
|
|
{
|
|
|
|
"line_number_public": first["line_number_public"],
|
|
|
|
"line_transport_type": first["line_transport_type"],
|
|
|
|
"final_destination": first["final_destination"],
|
|
|
|
"due_at_schedule": first["due_at_schedule"],
|
|
|
|
"due_at_realtime": first["due_at_realtime"],
|
|
|
|
"is_realtime": first["is_realtime"],
|
|
|
|
"next_passages": self.line.passages,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self._attr_available = True
|
2021-11-14 02:22:36 +00:00
|
|
|
except (KeyError) as error:
|
|
|
|
_LOGGER.error("Invalid data received from De Lijn: %s", error)
|
2021-12-09 10:39:57 +00:00
|
|
|
self._attr_available = False
|