From 1e21de655f963117d5b5fe207b2a0f22c1613021 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sat, 10 Apr 2021 11:58:58 +0200 Subject: [PATCH] Move all external call to asyn/await --- custom_components/georide/__init__.py | 6 +++--- custom_components/georide/binary_sensor.py | 14 +++++++------- custom_components/georide/device_tracker.py | 12 ++++++------ custom_components/georide/sensor.py | 10 +++++----- custom_components/georide/switch.py | 18 +++++++++--------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index 0f6457f..dcb5acb 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -111,7 +111,7 @@ async def async_unload_entry(hass, entry): return True -def connect_socket(context): +async def connect_socket(context): """subscribe to GeoRide socket""" _LOGGER.info("GeoRide socket connexion") socket = GeoRideSocket() @@ -123,7 +123,7 @@ def connect_socket(context): context.socket = socket socket.init() - socket.connect(context.get_token()) + socket.connect(await context.get_token()) class GeoRideContext: @@ -169,7 +169,7 @@ class GeoRideContext: """ GeoRide tracker list """ self._georide_trackers = trackers - def get_token(self): + async def get_token(self): """ here we return the current valid tocken """ jwt_data = jwt.decode(self._token, verify=False) exp_timestamp = jwt_data['exp'] diff --git a/custom_components/georide/binary_sensor.py b/custom_components/georide/binary_sensor.py index 58032d5..fa6b025 100644 --- a/custom_components/georide/binary_sensor.py +++ b/custom_components/georide/binary_sensor.py @@ -15,11 +15,11 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613 """Set up GeoRide tracker based off an entry.""" georide_context = hass.data[GEORIDE_DOMAIN]["context"] - - if georide_context.get_token() is None: + token = await georide_context.get_token() + if token is None: return False - trackers = GeoRideApi.get_trackers(georide_context.get_token()) + trackers = GeoRideApi.get_trackers(token) binary_sensor_entities = [] for tracker in trackers: @@ -50,10 +50,10 @@ class GeoRideStolenBinarySensorEntity(BinarySensorEntity): self._state = 0 - def update(self): + async def async_update(self): """ update the current tracker""" _LOGGER.info('update') - self._data = self._get_tracker_callback(self._tracker_id) + self._data = await self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name self._state = self._data.is_stolen @@ -107,10 +107,10 @@ class GeoRideCrashedBinarySensorEntity(BinarySensorEntity): self._state = 0 - def update(self): + async def async_update(self): """ update the current tracker""" _LOGGER.info('update') - self._data = self._get_tracker_callback(self._tracker_id) + self._data = await self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name self._state = self._data.is_crashed diff --git a/custom_components/georide/device_tracker.py b/custom_components/georide/device_tracker.py index f7e80a9..edbd38c 100644 --- a/custom_components/georide/device_tracker.py +++ b/custom_components/georide/device_tracker.py @@ -16,14 +16,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d """Set up Georide tracker based off an entry.""" georide_context = hass.data[GEORIDE_DOMAIN]["context"] - - if georide_context.get_token() is None: + token = await georide_context.get_token() + if token is None: return False - _LOGGER.debug('Current GeoRide token: %s', georide_context.get_token()) + _LOGGER.debug('Current GeoRide token: %s', token) - trackers = GeoRideApi.get_trackers(georide_context.get_token()) + trackers = GeoRideApi.get_trackers(token) tracker_entities = [] @@ -118,9 +118,9 @@ class GeoRideTrackerEntity(TrackerEntity): """No polling needed.""" return True - def update(self): + async def async_update(self): """ update the current tracker""" _LOGGER.info('update') - self._data = self._get_tracker_callback(self._tracker_id) + self._data = await self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name diff --git a/custom_components/georide/sensor.py b/custom_components/georide/sensor.py index 52c1a49..ffb4344 100644 --- a/custom_components/georide/sensor.py +++ b/custom_components/georide/sensor.py @@ -17,11 +17,11 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613 """Set up GeoRide tracker based off an entry.""" georide_context = hass.data[GEORIDE_DOMAIN]["context"] - - if georide_context.get_token() is None: + token = await georide_context.get_token() + if token is None: return False - trackers = GeoRideApi.get_trackers(georide_context.get_token()) + trackers = GeoRideApi.get_trackers(token) odometer_switch_entities = [] for tracker in trackers: @@ -50,10 +50,10 @@ class GeoRideOdometerSensorEntity(SwitchEntity): self._state = 0 - def update(self): + async def async_update(self): """ update the current tracker""" _LOGGER.info('update') - self._data = self._get_tracker_callback(self._tracker_id) + self._data = await self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name self._state = self._data.odometer diff --git a/custom_components/georide/switch.py b/custom_components/georide/switch.py index 6997798..b72d1e4 100644 --- a/custom_components/georide/switch.py +++ b/custom_components/georide/switch.py @@ -17,13 +17,13 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613 """Set up GeoRide tracker based off an entry.""" georide_context = hass.data[GEORIDE_DOMAIN]["context"] - - if georide_context.get_token() is None: + token = await georide_context.get_token() + if token is None: return False - _LOGGER.info('Current georide token: %s', georide_context.get_token()) - trackers = GeoRideApi.get_trackers(georide_context.get_token()) + _LOGGER.info('Current georide token: %s', token) + trackers = await GeoRideApi.get_trackers(token) lock_switch_entities = [] for tracker in trackers: @@ -56,7 +56,7 @@ class GeoRideLockSwitchEntity(SwitchEntity): 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) + success = GeoRideApi.lock_tracker(await self._get_token_callback(), self._tracker_id) if success: self._data.is_locked = True self._is_on = True @@ -64,7 +64,7 @@ class GeoRideLockSwitchEntity(SwitchEntity): 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) + success = GeoRideApi.unlock_tracker(await self._get_token_callback(), self._tracker_id) if success: self._data.is_locked = False self._is_on = False @@ -72,16 +72,16 @@ class GeoRideLockSwitchEntity(SwitchEntity): async def async_toggle(self, **kwargs): """ toggle lock the georide tracker """ _LOGGER.info('async_toggle %s', kwargs) - result = GeoRideApi.toogle_lock_tracker(self._get_token_callback(), + result = await GeoRideApi.toogle_lock_tracker(await self._get_token_callback(), self._tracker_id) self._data.is_locked = result self._is_on = result - def update(self): + async def async_update(self): """ update the current tracker""" _LOGGER.info('update') - self._data = self._get_tracker_callback(self._tracker_id) + self._data = await self._get_tracker_callback(self._tracker_id) self._name = self._data.tracker_name self._is_on = self._data.is_locked