Skip to content

Commit e1fc3f4

Browse files
Guillaume De Saint MartinGuillaumeDSM
authored andcommitted
[Community] handle missing devices
1 parent fe4f1f4 commit e1fc3f4

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

octobot/community/authentication.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CommunityAuthentication(authentication.Authenticator):
3939
"""
4040
ALLOWED_TIME_DELAY = 1 * commons_constants.MINUTE_TO_SECONDS
4141
LOGIN_TIMEOUT = 20
42+
DEVICE_NOT_FOUND_RETRY_DELAY = 1
4243
AUTHORIZATION_HEADER = "authorization"
4344
SESSION_HEADER = "X-Session"
4445
GQL_AUTHORIZATION_HEADER = "Authorization"
@@ -234,20 +235,31 @@ async def update_selected_device(self):
234235
async def _load_device_if_selected(self):
235236
# 1. use user selected device id if any
236237
if saved_uuid := self._get_saved_gql_device_id():
237-
await self.select_device(saved_uuid)
238-
else:
239-
# 2. fetch all user devices and create one if none, otherwise ask use for which one to use
240-
await self.load_user_devices()
241-
if len(self.user_account.get_all_user_devices_raw_data()) == 0:
242-
await self.select_device(
243-
self.user_account.get_device_id(
244-
await self.create_new_device()
245-
)
238+
try:
239+
await self.select_device(saved_uuid)
240+
return
241+
except errors.DeviceNotFoundError:
242+
# proceed to 2.
243+
pass
244+
# 2. fetch all user devices and create one if none, otherwise ask use for which one to use
245+
await self.load_user_devices()
246+
if len(self.user_account.get_all_user_devices_raw_data()) == 0:
247+
await self.select_device(
248+
self.user_account.get_device_id(
249+
await self.create_new_device()
246250
)
247-
# more than one possible device, can't auto-select one
251+
)
252+
# more than one possible device, can't auto-select one
248253

249254
async def select_device(self, device_id):
250-
self.user_account.set_selected_device_raw_data(await self.fetch_device(device_id))
255+
fetched_device = await self.fetch_device(device_id)
256+
if fetched_device is None:
257+
# retry after some time, if still None, there is an issue
258+
await asyncio.sleep(self.DEVICE_NOT_FOUND_RETRY_DELAY)
259+
fetched_device = await self.fetch_device(device_id)
260+
if fetched_device is None:
261+
raise errors.DeviceNotFoundError(f"Can't find device with id: {device_id}")
262+
self.user_account.set_selected_device_raw_data(fetched_device)
251263
self.user_account.gql_device_id = device_id
252264
self._save_gql_device_id(self.user_account.gql_device_id)
253265
await self.on_new_device_select()

octobot/community/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ class StatusCodeRequestError(RequestError):
2626

2727
class DeviceError(commons_authentication.UnavailableError):
2828
pass
29+
30+
31+
class DeviceNotFoundError(DeviceError):
32+
pass

octobot/community/feeds/community_mqtt_feed.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,21 @@ def _on_subscribe(self, client, mid, qos, properties):
228228
for subscription, granted_qos in zip(subscriptions, qos):
229229
# in case of bad suback code, we can resend subscription
230230
if granted_qos >= gmqtt.constants.SubAckReasonCode.UNSPECIFIED_ERROR.value:
231-
self.logger.warning(f"Retrying subscribe, client_id: {client._client_id}, mid: {mid}, "
231+
self.logger.warning(f"Retrying subscribe to {[s.topic for s in subscriptions]}, "
232+
f"client_id: {client._client_id}, mid: {mid}, "
232233
f"reason code: {granted_qos}, properties {properties}")
233234
if self._subscription_attempts < self.MAX_SUBSCRIPTION_ATTEMPTS * len(subscriptions):
234235
self._subscription_attempts += 1
235236
client.resubscribe(subscription)
236237
else:
237238
self.logger.error(f"Max subscription attempts reached, stopping subscription "
238-
f"to {[s.topic for s in subscriptions]}. Are you copying this "
239+
f"to {[s.topic for s in subscriptions]}. Are you subscribing to this "
239240
f"strategy on your OctoBot account ?")
240241
return
241242
else:
242243
self._subscription_attempts = 0
243-
self.logger.info(f"Subscribed, client_id: {client._client_id}, mid {mid}, QOS: {granted_qos}, "
244-
f"properties {properties}")
244+
self.logger.info(f"Subscribed, client_id: {client._client_id}, mid {mid}, QOS: {granted_qos}, "
245+
f"properties {properties}")
245246

246247
def _register_callbacks(self, client):
247248
client.on_connect = self._on_connect

0 commit comments

Comments
 (0)