Add Black
This commit is contained in:
1 parent
0490167a12
commit
da05dfe708
16 files changed
+401
-272
No files matched your search
@@ -21,42 +21,42 @@ 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_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'
|
||||
ATTR_DUE_IN = "due_in"
|
||||
ATTR_DUE_AT = "due_at"
|
||||
ATTR_NEXT_UP = "next_departures"
|
||||
|
||||
ATTRIBUTION = "Data provided by rejseplanen.dk"
|
||||
|
||||
CONF_STOP_ID = 'stop_id'
|
||||
CONF_ROUTE = 'route'
|
||||
CONF_DIRECTION = 'direction'
|
||||
CONF_DEPARTURE_TYPE = 'departure_type'
|
||||
CONF_STOP_ID = "stop_id"
|
||||
CONF_ROUTE = "route"
|
||||
CONF_DIRECTION = "direction"
|
||||
CONF_DEPARTURE_TYPE = "departure_type"
|
||||
|
||||
DEFAULT_NAME = 'Next departure'
|
||||
ICON = 'mdi:bus'
|
||||
DEFAULT_NAME = "Next departure"
|
||||
ICON = "mdi:bus"
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=1)
|
||||
|
||||
BUS_TYPES = ['BUS', 'EXB', 'TB']
|
||||
TRAIN_TYPES = ['LET', 'S', 'REG', 'IC', 'LYN', 'TOG']
|
||||
METRO_TYPES = ['M']
|
||||
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,
|
||||
vol.Optional(CONF_ROUTE, default=[]):
|
||||
vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_DIRECTION, default=[]):
|
||||
vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_DEPARTURE_TYPE, default=[]):
|
||||
vol.All(cv.ensure_list,
|
||||
[vol.In([*BUS_TYPES, *TRAIN_TYPES, *METRO_TYPES])])
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_STOP_ID): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_ROUTE, default=[]): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_DIRECTION, default=[]): vol.All(cv.ensure_list, [cv.string]),
|
||||
vol.Optional(CONF_DEPARTURE_TYPE, default=[]): vol.All(
|
||||
cv.ensure_list, [vol.In([*BUS_TYPES, *TRAIN_TYPES, *METRO_TYPES])]
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def due_in_minutes(timestamp):
|
||||
@@ -64,8 +64,9 @@ def due_in_minutes(timestamp):
|
||||
|
||||
The timestamp should be in the format day.month.year hour:minute
|
||||
"""
|
||||
diff = datetime.strptime(
|
||||
timestamp, "%d.%m.%y %H:%M") - dt_util.now().replace(tzinfo=None)
|
||||
diff = datetime.strptime(timestamp, "%d.%m.%y %H:%M") - dt_util.now().replace(
|
||||
tzinfo=None
|
||||
)
|
||||
|
||||
return int(diff.total_seconds() // 60)
|
||||
|
||||
@@ -79,8 +80,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
departure_type = config[CONF_DEPARTURE_TYPE]
|
||||
|
||||
data = PublicTransportData(stop_id, route, direction, departure_type)
|
||||
add_devices([RejseplanenTransportSensor(
|
||||
data, stop_id, route, direction, name)], True)
|
||||
add_devices(
|
||||
[RejseplanenTransportSensor(data, stop_id, route, direction, name)], True
|
||||
)
|
||||
|
||||
|
||||
class RejseplanenTransportSensor(Entity):
|
||||
@@ -124,14 +126,14 @@ class RejseplanenTransportSensor(Entity):
|
||||
ATTR_STOP_NAME: self._times[0][ATTR_STOP_NAME],
|
||||
ATTR_STOP_ID: self._stop_id,
|
||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||
ATTR_NEXT_UP: next_up
|
||||
ATTR_NEXT_UP: next_up,
|
||||
}
|
||||
return {k: v for k, v in params.items() if v}
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit this state is expressed in."""
|
||||
return 'min'
|
||||
return "min"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -148,7 +150,7 @@ class RejseplanenTransportSensor(Entity):
|
||||
pass
|
||||
|
||||
|
||||
class PublicTransportData():
|
||||
class PublicTransportData:
|
||||
"""The Class for handling the data retrieval."""
|
||||
|
||||
def __init__(self, stop_id, route, direction, departure_type):
|
||||
@@ -161,16 +163,21 @@ class PublicTransportData():
|
||||
|
||||
def empty_result(self):
|
||||
"""Object returned when no departures are found."""
|
||||
return [{ATTR_DUE_IN: 'n/a',
|
||||
ATTR_DUE_AT: 'n/a',
|
||||
ATTR_TYPE: 'n/a',
|
||||
ATTR_ROUTE: self.route,
|
||||
ATTR_DIRECTION: 'n/a',
|
||||
ATTR_STOP_NAME: 'n/a'}]
|
||||
return [
|
||||
{
|
||||
ATTR_DUE_IN: "n/a",
|
||||
ATTR_DUE_AT: "n/a",
|
||||
ATTR_TYPE: "n/a",
|
||||
ATTR_ROUTE: self.route,
|
||||
ATTR_DIRECTION: "n/a",
|
||||
ATTR_STOP_NAME: "n/a",
|
||||
}
|
||||
]
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data from rejseplanen."""
|
||||
import rjpl
|
||||
|
||||
self.info = []
|
||||
|
||||
def intersection(lst1, lst2):
|
||||
@@ -179,12 +186,9 @@ class PublicTransportData():
|
||||
|
||||
# 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))
|
||||
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(
|
||||
@@ -192,7 +196,7 @@ class PublicTransportData():
|
||||
timeout=5,
|
||||
useTrain=use_train,
|
||||
useBus=use_bus,
|
||||
useMetro=use_metro
|
||||
useMetro=use_metro,
|
||||
)
|
||||
except rjpl.rjplAPIError as error:
|
||||
_LOGGER.debug("API returned error: %s", error)
|
||||
@@ -204,36 +208,40 @@ class PublicTransportData():
|
||||
return
|
||||
|
||||
# Filter result
|
||||
results = [d for d in results if 'cancelled' not in d]
|
||||
results = [d for d in results if "cancelled" not in d]
|
||||
if self.route:
|
||||
results = [d for d in results if d['name'] in self.route]
|
||||
results = [d for d in results if d["name"] in self.route]
|
||||
if self.direction:
|
||||
results = [d for d in results if d['direction'] in self.direction]
|
||||
results = [d for d in results if d["direction"] in self.direction]
|
||||
if self.departure_type:
|
||||
results = [d for d in results if d['type'] in self.departure_type]
|
||||
results = [d for d in results if d["type"] in self.departure_type]
|
||||
|
||||
for item in results:
|
||||
route = item.get('name')
|
||||
route = item.get("name")
|
||||
|
||||
due_at_date = item.get('rtDate')
|
||||
due_at_time = item.get('rtTime')
|
||||
due_at_date = item.get("rtDate")
|
||||
due_at_time = item.get("rtTime")
|
||||
|
||||
if due_at_date is None:
|
||||
due_at_date = item.get('date') # Scheduled date
|
||||
due_at_date = item.get("date") # Scheduled date
|
||||
if due_at_time is None:
|
||||
due_at_time = item.get('time') # Scheduled time
|
||||
due_at_time = item.get("time") # Scheduled time
|
||||
|
||||
if (due_at_date is not None and
|
||||
due_at_time is not None and
|
||||
route is not None):
|
||||
due_at = '{} {}'.format(due_at_date, due_at_time)
|
||||
if (
|
||||
due_at_date is not None
|
||||
and due_at_time is not None
|
||||
and route is not None
|
||||
):
|
||||
due_at = "{} {}".format(due_at_date, due_at_time)
|
||||
|
||||
departure_data = {ATTR_DUE_IN: due_in_minutes(due_at),
|
||||
ATTR_DUE_AT: due_at,
|
||||
ATTR_TYPE: item.get('type'),
|
||||
ATTR_ROUTE: route,
|
||||
ATTR_DIRECTION: item.get('direction'),
|
||||
ATTR_STOP_NAME: item.get('stop')}
|
||||
departure_data = {
|
||||
ATTR_DUE_IN: due_in_minutes(due_at),
|
||||
ATTR_DUE_AT: due_at,
|
||||
ATTR_TYPE: item.get("type"),
|
||||
ATTR_ROUTE: route,
|
||||
ATTR_DIRECTION: item.get("direction"),
|
||||
ATTR_STOP_NAME: item.get("stop"),
|
||||
}
|
||||
self.info.append(departure_data)
|
||||
|
||||
if not self.info:
|
||||
|
||||
Reference in new issue
Block a user