From cc45b02a3a39f5a06dbe2d85eb09a3c31529de96 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Wed, 30 Oct 2019 22:19:31 +0100 Subject: [PATCH] Chandge switch from async to realtume update --- custom_components/georide/__init__.py | 16 +++++----------- custom_components/georide/switch.py | 20 +++++++++----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index 7a1e98c..b21178e 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -183,14 +183,10 @@ class GeorideContext: self._pending_msg.append(data) @callback - def on_lock_callback(self, data_string): + def on_lock_callback(self, data): """on lock callback""" - _LOGGER.info("On lock received %s", data_string) - data = data_string - _LOGGER.info("On lock received %s", data['trackerId']) - + _LOGGER.info("On lock received") for tracker in self._georide_trackers: - if tracker.tracker_id == data['trackerId']: tracker.locked_latitude = data['lockedLatitude'] tracker.locked_longitude = data['lockedLongitude'] @@ -198,20 +194,18 @@ class GeorideContext: return @callback - def on_device_callback(self, data_string): + def on_device_callback(self, data): """on device callback""" _LOGGER.info("On device received") - data = data_string.json() for tracker in self._georide_trackers: if tracker.tracker_id == data['trackerId']: tracker.status = data['status'] return @callback - def on_position_callback(self, data_string): + def on_position_callback(self, data): """on position callback""" - _LOGGER.info("On position received %s", data_string) - data = data_string.json() + _LOGGER.info("On position received") for tracker in self._georide_trackers: if tracker.tracker_id == data['trackerId']: tracker.latitude = data['latitude'] diff --git a/custom_components/georide/switch.py b/custom_components/georide/switch.py index 49b43a8..e0e030f 100644 --- a/custom_components/georide/switch.py +++ b/custom_components/georide/switch.py @@ -53,39 +53,37 @@ class GeorideLockSwitchEntity(SwitchDevice): self._state = {} - async def async_turn_on(self, **kwargs): + def turn_on(self, **kwargs): """ lock the georide tracker """ _LOGGER.info('async_turn_on %s', kwargs) success = GeorideApi.lock_tracker(self._get_token_callback(), self._tracker_id) if success: + self._data.is_locked = True self._is_on = True - async def async_turn_off(self, **kwargs): + def turn_off(self, **kwargs): """ unlock the georide tracker """ _LOGGER.info('async_turn_off %s', kwargs) success = GeorideApi.unlock_tracker(self._get_token_callback(), self._tracker_id) if success: + self._data.is_locked = False self._is_on = False async def async_toggle(self, **kwargs): """ toggle lock the georide tracker """ _LOGGER.info('async_toggle %s', kwargs) - self._is_on = GeorideApi.toogle_lock_tracker(self._get_token_callback(), - self._tracker_id) + result = GeorideApi.toogle_lock_tracker(self._get_token_callback(), + self._tracker_id) + self._data.is_locked = result + self._is_on = result - @property - def should_poll(self): - """No polling needed.""" - return True - - async def async_update(self): + def update(self): """ update the current tracker""" _LOGGER.info('async_update ') self._data = self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name self._is_on = self._data.is_locked - return @property def unique_id(self):