Skip to content

Commit

Permalink
fix/pairing_checks_for_offline (#29)
Browse files Browse the repository at this point in the history
* fix/pairing_checks_for_offline

* unicode copy pasta shenanigans

* Update pairing.py

* Update pairing.py

* fix/pairing_checks

* use BackendType enum

* use get_backend_type()

* skip remote check for offline

* cast to bool
  • Loading branch information
JarbasAl authored Feb 15, 2023
1 parent c1df4f8 commit a9a9c98
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions ovos_backend_client/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from ovos_backend_client.exceptions import BackendDown, InternetDown, HTTPError
from ovos_backend_client.identity import IdentityManager
from ovos_backend_client.backends.selene import SELENE_API_URL
from ovos_backend_client.backends import BackendType, get_backend_type


PAIRING_BACKENDS = [BackendType.PERSONAL,
BackendType.SELENE]


def is_backend_disabled():
Expand All @@ -34,6 +39,8 @@ def decorated(*args, **kwargs):

def has_been_paired():
""" Determine if this device has ever been paired with a backend
identity2.json must exist, device has been assigned a uuid
Returns:
bool: True if ever paired with backend (not factory reset)
Expand All @@ -57,10 +64,17 @@ def is_paired(ignore_errors=True, url=None, version="v1", identity_file=None, ba
if is_backend_disabled():
return True

# check if pairing is valid
backend_type = backend_type or get_backend_type()
api = DeviceApi(url=url, version=version, identity_file=identity_file, backend_type=backend_type)
return api.identity.uuid and check_remote_pairing(ignore_errors, url=url, version=version,
identity_file=identity_file, backend_type=backend_type)

# check if pairing is valid
if backend_type in PAIRING_BACKENDS:
return api.identity.uuid and \
check_remote_pairing(ignore_errors, url=url, version=version,
identity_file=identity_file,
backend_type=backend_type)
else:
return bool(api.identity.uuid)


def check_remote_pairing(ignore_errors, url=None, version="v1", identity_file=None, backend_type=None):
Expand All @@ -72,6 +86,10 @@ def check_remote_pairing(ignore_errors, url=None, version="v1", identity_file=No
Returns:
True if pairing checks out, otherwise False.
"""
backend_type = backend_type or get_backend_type()
if backend_type not in PAIRING_BACKENDS:
return has_been_paired() # uuid assigned locally

try:
return bool(DeviceApi(url=url, version=version,
identity_file=identity_file, backend_type=backend_type).get())
Expand Down

0 comments on commit a9a9c98

Please sign in to comment.