diff --git a/homeassistant/components/sensor/deutsche_bahn.py b/homeassistant/components/sensor/deutsche_bahn.py index c443829a3bb..0261288f27e 100644 --- a/homeassistant/components/sensor/deutsche_bahn.py +++ b/homeassistant/components/sensor/deutsche_bahn.py @@ -71,15 +71,17 @@ class DeutscheBahnSensor(Entity): def device_state_attributes(self): """Return the state attributes.""" connections = self.data.connections[0] - connections['next'] = self.data.connections[1]['departure'] - connections['next_on'] = self.data.connections[2]['departure'] + if len(self.data.connections) > 1: + connections['next'] = self.data.connections[1]['departure'] + if len(self.data.connections) > 2: + connections['next_on'] = self.data.connections[2]['departure'] return connections def update(self): """Get the latest delay from bahn.de and updates the state.""" self.data.update() 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']) @@ -102,6 +104,9 @@ class SchieneData(object): self.start, self.goal, dt_util.as_local(dt_util.utcnow()), self.only_direct) + if not self.connections: + self.connections = [{}] + for con in self.connections: # Detail info is not useful. Having a more consistent interface # simplifies usage of template sensors.