Add more details
This commit is contained in:
parent
3406b41b0c
commit
97f81ad7a6
1 changed files with 34 additions and 6 deletions
|
@ -18,6 +18,12 @@ from homeassistant.helpers.entity import Entity
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_RESOURCE = 'http://transport.opendata.ch/v1/'
|
_RESOURCE = 'http://transport.opendata.ch/v1/'
|
||||||
|
|
||||||
|
ATTR_DEPARTURE_TIME1 = 'Next departure'
|
||||||
|
ATTR_DEPARTURE_TIME2 = 'Next on departure'
|
||||||
|
ATTR_START = 'Start'
|
||||||
|
ATTR_TARGET = 'Destination'
|
||||||
|
ATTR_REMAINING_TIME = 'Remaining time'
|
||||||
|
|
||||||
# Return cached results if last scan was less then this time ago
|
# Return cached results if last scan was less then this time ago
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||||
|
|
||||||
|
@ -52,7 +58,9 @@ class SwissPublicTransportSensor(Entity):
|
||||||
|
|
||||||
def __init__(self, data, journey):
|
def __init__(self, data, journey):
|
||||||
self.data = data
|
self.data = data
|
||||||
self._name = '{}-{}'.format(journey[2], journey[3])
|
self._name = 'Next Departure'
|
||||||
|
self._from = journey[2]
|
||||||
|
self._to = journey[3]
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -65,12 +73,26 @@ class SwissPublicTransportSensor(Entity):
|
||||||
""" Returns the state of the device. """
|
""" Returns the state of the device. """
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state_attributes(self):
|
||||||
|
""" Returns the state attributes. """
|
||||||
|
if self._times is not None:
|
||||||
|
return {
|
||||||
|
ATTR_DEPARTURE_TIME1: self._times[0],
|
||||||
|
ATTR_DEPARTURE_TIME2: self._times[1],
|
||||||
|
ATTR_START: self._from,
|
||||||
|
ATTR_TARGET: self._to,
|
||||||
|
ATTR_REMAINING_TIME: '{}'.format(
|
||||||
|
':'.join(str(self._times[2]).split(':')[:2]))
|
||||||
|
}
|
||||||
|
|
||||||
# pylint: disable=too-many-branches
|
# pylint: disable=too-many-branches
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Gets the latest data from opendata.ch and updates the states. """
|
""" Gets the latest data from opendata.ch and updates the states. """
|
||||||
times = self.data.update()
|
self.data.update()
|
||||||
|
self._times = self.data.times
|
||||||
try:
|
try:
|
||||||
self._state = ', '.join(times)
|
self._state = self._times[0]
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -82,6 +104,7 @@ class PublicTransportData(object):
|
||||||
def __init__(self, journey):
|
def __init__(self, journey):
|
||||||
self.start = journey[0]
|
self.start = journey[0]
|
||||||
self.destination = journey[1]
|
self.destination = journey[1]
|
||||||
|
self.times = {}
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
|
@ -94,16 +117,21 @@ class PublicTransportData(object):
|
||||||
'to=' + self.destination + '&' +
|
'to=' + self.destination + '&' +
|
||||||
'fields[]=connections/from/departureTimestamp/&' +
|
'fields[]=connections/from/departureTimestamp/&' +
|
||||||
'fields[]=connections/',
|
'fields[]=connections/',
|
||||||
timeout=10)
|
timeout=30)
|
||||||
connections = response.json()['connections'][:2]
|
connections = response.json()['connections'][:2]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return [
|
self.times = [
|
||||||
dt_util.datetime_to_time_str(
|
dt_util.datetime_to_time_str(
|
||||||
dt_util.as_local(dt_util.utc_from_timestamp(
|
dt_util.as_local(dt_util.utc_from_timestamp(
|
||||||
item['from']['departureTimestamp']))
|
item['from']['departureTimestamp']))
|
||||||
)
|
)
|
||||||
for item in connections
|
for item in connections
|
||||||
]
|
]
|
||||||
|
self.times.append(
|
||||||
|
dt_util.as_local(
|
||||||
|
dt_util.utc_from_timestamp(
|
||||||
|
connections[0]['from']['departureTimestamp'])) -
|
||||||
|
dt_util.as_local(dt_util.utcnow()))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ['n/a']
|
self.times = ['n/a']
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue