From 14300c52783f1dd64067af985f38f9312a87d080 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 7 Nov 2019 21:40:09 +0100 Subject: [PATCH 1/3] Add polling off all tracker each hours --- README.md | 2 +- custom_components/georide/__init__.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 731dd5e..0bb6289 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Georide Home assistant -![Logo Georide](./georide-logo.png) +![Logo Georide](/georide-logo.png) ⚠️ This is not an official implementation diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index 2ff357c..1b12b70 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -62,9 +62,6 @@ async def async_setup(hass, config): ) ) - - - # Return boolean to indicate that initialization was successful. return True @@ -88,8 +85,7 @@ async def async_setup_entry(hass, entry): hass.data[DOMAIN]["context"] = context # We add trackers to the context - trackers = GeorideApi.get_trackers(token) - context.georide_trackers = trackers + context.refresh_trackers() hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, "device_tracker")) @@ -193,8 +189,11 @@ class GeorideContext: def get_tracker(self, tracker_id): """ here we return last tracker by id""" if not self._thread_started: - _LOGGER.info("Satr the thread") + _LOGGER.info("Start the thread") self._hass.async_add_executor_job(connect_socket, self) + # We refresh the tracker list each hours + ha_event.async_track_time_interval( + self._hass, self.refresh_trackers(), timedelta(hours=1)) self._thread_started = True for tracker in self._georide_trackers: @@ -202,6 +201,12 @@ class GeorideContext: return tracker return None + def refresh_trackers(self): + """Used to refresh the tracker list""" + _LOGGER.info("Tracker list refresh") + self._georide_trackers = GeorideApi.get_trackers(self.get_token()) + + @property def socket(self): """ hold the georide socket """ From 66bd335a8e662e993b6c4875d40a3cbcb232ddc2 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sat, 9 Nov 2019 10:04:47 +0100 Subject: [PATCH 2/3] Add component to default HACS --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 0bb6289..ae04c23 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ ![Logo Georide](/georide-logo.png) ⚠️ This is not an official implementation - -[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs) +[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg?style=for-the-badge)](https://www.gnu.org/licenses/gpl-3.0) Official GeoRide website: https://georide.fr/ From a2e65694fc7e6cfceb0c5f979ae5423ae3997a77 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sat, 9 Nov 2019 10:26:24 +0100 Subject: [PATCH 3/3] Increase interval to refresh each 10 minutes --- custom_components/georide/__init__.py | 12 ++++++++---- custom_components/georide/const.py | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index 1b12b70..32a1313 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -31,6 +31,7 @@ from .const import ( CONF_TOKEN, TRACKER_ID, TOKEN_SAFE_DAY, + MIN_UNTIL_REFRESH, DOMAIN ) @@ -137,7 +138,7 @@ class GeorideContext: self._token = token self._socket = None self._thread_started = False - + self._previous_refresh = math.floor(time.time()/60) @property def hass(self): """ hass """ @@ -174,7 +175,6 @@ class GeorideContext: exp_timestamp = jwt_data['exp'] epoch = math.ceil(time.time()) - if (exp_timestamp - TOKEN_SAFE_DAY) < epoch: _LOGGER.info("Time reached, renew token") account = GeorideApi.get_authorisation_token(self._email, self._password) @@ -188,12 +188,16 @@ class GeorideContext: def get_tracker(self, tracker_id): """ here we return last tracker by id""" + epoch_min = math.floor(time.time()/60) + if (epoch_min % MIN_UNTIL_REFRESH) == 0: + if epoch_min != self._previous_refresh: + self._previous_refresh = epoch_min + self.refresh_trackers() + if not self._thread_started: _LOGGER.info("Start the thread") self._hass.async_add_executor_job(connect_socket, self) # We refresh the tracker list each hours - ha_event.async_track_time_interval( - self._hass, self.refresh_trackers(), timedelta(hours=1)) self._thread_started = True for tracker in self._georide_trackers: diff --git a/custom_components/georide/const.py b/custom_components/georide/const.py index 9d623a7..3e7fa23 100644 --- a/custom_components/georide/const.py +++ b/custom_components/georide/const.py @@ -8,4 +8,6 @@ CONF_TOKEN = "token" TRACKER_ID = "trackerId" +MIN_UNTIL_REFRESH = 10 + TOKEN_SAFE_DAY = 432000 # five days