Improve and align Rejseplanen with other transport components (Breaking) (#25375)

* Improve and align Rejseplanen with other transport components (Breaking)

* Fix empty list check

* Remove pointless list cast

* Clean up redundant definition of transport types

* Add docstring

* Simplify _times check
This commit is contained in:
Martin Eberhardt 2019-07-31 18:48:54 +02:00 committed by Martin Hjelmare
parent 3d11c45edd
commit 90dc81c1b3

View file

@ -21,14 +21,14 @@ from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
ATTR_STOP_ID = 'Stop ID'
ATTR_STOP_NAME = 'Stop'
ATTR_ROUTE = 'Route'
ATTR_TYPE = 'Type'
ATTR_DIRECTION = "Direction"
ATTR_DUE_IN = 'Due in'
ATTR_DUE_AT = 'Due at'
ATTR_NEXT_UP = 'Later departure'
ATTR_STOP_ID = 'stop_id'
ATTR_STOP_NAME = 'stop'
ATTR_ROUTE = 'route'
ATTR_TYPE = 'type'
ATTR_DIRECTION = "direction"
ATTR_DUE_IN = 'due_in'
ATTR_DUE_AT = 'due_at'
ATTR_NEXT_UP = 'next_departures'
ATTRIBUTION = "Data provided by rejseplanen.dk"
@ -42,6 +42,10 @@ ICON = 'mdi:bus'
SCAN_INTERVAL = timedelta(minutes=1)
BUS_TYPES = ['BUS', 'EXB', 'TB']
TRAIN_TYPES = ['LET', 'S', 'REG', 'IC', 'LYN', 'TOG']
METRO_TYPES = ['M']
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_STOP_ID): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
@ -51,7 +55,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_DEPARTURE_TYPE, default=[]):
vol.All(cv.ensure_list,
[vol.In(list(['BUS', 'EXB', 'M', 'S', 'REG']))])
[vol.In([*BUS_TYPES, *TRAIN_TYPES, *METRO_TYPES])])
})
@ -104,14 +108,13 @@ class RejseplanenTransportSensor(Entity):
@property
def device_state_attributes(self):
"""Return the state attributes."""
if self._times is not None:
next_up = None
if not self._times:
return None
next_up = []
if len(self._times) > 1:
next_up = ('{} towards {} in {} from {}'.format(
self._times[1][ATTR_ROUTE],
self._times[1][ATTR_DIRECTION],
str(self._times[1][ATTR_DUE_IN]),
self._times[1][ATTR_STOP_NAME]))
next_up = self._times[1:]
params = {
ATTR_DUE_IN: str(self._times[0][ATTR_DUE_IN]),
ATTR_DUE_AT: self._times[0][ATTR_DUE_AT],
@ -170,8 +173,27 @@ class PublicTransportData():
import rjpl
self.info = []
def intersection(lst1, lst2):
"""Return items contained in both lists"""
return list(set(lst1) & set(lst2))
# Limit search to selected types, to get more results
all_types = not bool(self.departure_type)
use_train = all_types or bool(
intersection(TRAIN_TYPES, self.departure_type))
use_bus = all_types or bool(
intersection(BUS_TYPES, self.departure_type))
use_metro = all_types or bool(
intersection(METRO_TYPES, self.departure_type))
try:
results = rjpl.departureBoard(int(self.stop_id), timeout=5)
results = rjpl.departureBoard(
int(self.stop_id),
timeout=5,
useTrain=use_train,
useBus=use_bus,
useMetro=use_metro
)
except rjpl.rjplAPIError as error:
_LOGGER.debug("API returned error: %s", error)
self.info = self.empty_result()