From 971d933140b5112e1c9813fcc280d61497ef6f46 Mon Sep 17 00:00:00 2001 From: Eliseo Martelli Date: Thu, 24 Jan 2019 02:05:16 +0100 Subject: [PATCH] [FIX] Time reporting incorrect in sensor.gtt (#20362) * quick fix * remove print statement * fixes * remove lambda * added pylint disable * should be fine now --- homeassistant/components/sensor/gtt.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/gtt.py b/homeassistant/components/sensor/gtt.py index f0e141f3549..a64c743381d 100644 --- a/homeassistant/components/sensor/gtt.py +++ b/homeassistant/components/sensor/gtt.py @@ -79,8 +79,7 @@ class GttSensor(Entity): def update(self): """Update device state.""" self.data.get_data() - next_time = datetime.strptime( - self.data.state_bus['time'][0]['run'], "%H:%M") + next_time = get_datetime(self.data.state_bus) self._state = next_time.isoformat() @@ -99,8 +98,7 @@ class GttData: def get_data(self): """Get the data from the api.""" self.bus_list = self._pygtt.get_by_stop(self._stop) - self.bus_list.sort(key=lambda b: - datetime.strptime(b['time'][0]['run'], "%H:%M")) + self.bus_list.sort(key=get_datetime) if self._bus_name is not None: self.state_bus = self.get_bus_by_name() @@ -113,3 +111,13 @@ class GttData: for bus in self.bus_list: if bus['bus_name'] == self._bus_name: return bus + + +def get_datetime(bus): + """Get the datetime from a bus.""" + bustime = datetime.strptime(bus['time'][0]['run'], "%H:%M") + now = datetime.now() + bustime = bustime.replace(year=now.year, month=now.month, day=now.day) + if bustime < now: + bustime = bustime + timedelta(days=1) + return bustime