mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Error handling, in case no connections are available (#12010)
* Error handling, in case no connections are available * Fix elif to if
This commit is contained in:
parent
e084a260c6
commit
170a0c9888
@ -67,15 +67,17 @@ class DeutscheBahnSensor(Entity):
|
|||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
connections = self.data.connections[0]
|
connections = self.data.connections[0]
|
||||||
connections['next'] = self.data.connections[1]['departure']
|
if len(self.data.connections) > 1:
|
||||||
connections['next_on'] = self.data.connections[2]['departure']
|
connections['next'] = self.data.connections[1]['departure']
|
||||||
|
if len(self.data.connections) > 2:
|
||||||
|
connections['next_on'] = self.data.connections[2]['departure']
|
||||||
return connections
|
return connections
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest delay from bahn.de and updates the state."""
|
"""Get the latest delay from bahn.de and updates the state."""
|
||||||
self.data.update()
|
self.data.update()
|
||||||
self._state = self.data.connections[0].get('departure', 'Unknown')
|
self._state = self.data.connections[0].get('departure', 'Unknown')
|
||||||
if self.data.connections[0]['delay'] != 0:
|
if self.data.connections[0].get('delay', 0) != 0:
|
||||||
self._state += " + {}".format(self.data.connections[0]['delay'])
|
self._state += " + {}".format(self.data.connections[0]['delay'])
|
||||||
|
|
||||||
|
|
||||||
@ -96,6 +98,9 @@ class SchieneData(object):
|
|||||||
self.connections = self.schiene.connections(
|
self.connections = self.schiene.connections(
|
||||||
self.start, self.goal, dt_util.as_local(dt_util.utcnow()))
|
self.start, self.goal, dt_util.as_local(dt_util.utcnow()))
|
||||||
|
|
||||||
|
if not self.connections:
|
||||||
|
self.connections = [{}]
|
||||||
|
|
||||||
for con in self.connections:
|
for con in self.connections:
|
||||||
# Detail info is not useful. Having a more consistent interface
|
# Detail info is not useful. Having a more consistent interface
|
||||||
# simplifies usage of template sensors.
|
# simplifies usage of template sensors.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user