From fcb84d951e8022ace87bb19be259f401c9920ddc Mon Sep 17 00:00:00 2001 From: Vikram Gorla Date: Thu, 20 Sep 2018 08:45:16 +0200 Subject: [PATCH] On-demand update of swiss public transport sensor (#16723) * Sensor values to be updated only when required (only after the train has crossed). Looking at the documentation on https://transport.opendata.ch/docs.html, delay information is available only on stationboard, so no need to query often if there is no "real"time info. * Bumping up version of python_opendata_transport to 0.1.4 in order to catch client errors like throttling rejection (HTTP 429) * pleasing the hound * bumping python_opendata_transport to 0.1.4 --- .../components/sensor/swiss_public_transport.py | 10 ++++++---- requirements_all.txt | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/swiss_public_transport.py b/homeassistant/components/sensor/swiss_public_transport.py index 6f44350c5cf..6b34930075a 100644 --- a/homeassistant/components/sensor/swiss_public_transport.py +++ b/homeassistant/components/sensor/swiss_public_transport.py @@ -16,7 +16,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity import homeassistant.util.dt as dt_util -REQUIREMENTS = ['python_opendata_transport==0.1.3'] +REQUIREMENTS = ['python_opendata_transport==0.1.4'] _LOGGER = logging.getLogger(__name__) @@ -80,6 +80,7 @@ class SwissPublicTransportSensor(Entity): self._name = name self._from = start self._to = destination + self._remaining_time = "" @property def name(self): @@ -98,7 +99,7 @@ class SwissPublicTransportSensor(Entity): if self._opendata is None: return - remaining_time = dt_util.parse_datetime( + self._remaining_time = dt_util.parse_datetime( self._opendata.connections[0]['departure']) -\ dt_util.as_local(dt_util.utcnow()) @@ -111,7 +112,7 @@ class SwissPublicTransportSensor(Entity): ATTR_DEPARTURE_TIME2: self._opendata.connections[2]['departure'], ATTR_START: self._opendata.from_name, ATTR_TARGET: self._opendata.to_name, - ATTR_REMAINING_TIME: '{}'.format(remaining_time), + ATTR_REMAINING_TIME: '{}'.format(self._remaining_time), ATTR_ATTRIBUTION: CONF_ATTRIBUTION, } return attr @@ -126,6 +127,7 @@ class SwissPublicTransportSensor(Entity): from opendata_transport.exceptions import OpendataTransportError try: - await self._opendata.async_get_data() + if self._remaining_time.total_seconds() < 0: + await self._opendata.async_get_data() except OpendataTransportError: _LOGGER.error("Unable to retrieve data from transport.opendata.ch") diff --git a/requirements_all.txt b/requirements_all.txt index e77995681e5..cea393faa6b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1170,7 +1170,7 @@ python-vlc==1.1.2 python-wink==1.10.1 # homeassistant.components.sensor.swiss_public_transport -python_opendata_transport==0.1.3 +python_opendata_transport==0.1.4 # homeassistant.components.zwave python_openzwave==0.4.9