From 0e137d8be5c8fc89f1748b0291ea14fe74c66d10 Mon Sep 17 00:00:00 2001 From: Maciej Dems Date: Sun, 22 Aug 2021 23:37:42 +0200 Subject: [PATCH] Pairing bugs fixed --- main.py | 2 +- philipstv/api/__init__.py | 14 +++++++++---- philipstv/gui/__init__.py | 44 ++++++++++++++++++++++++++------------- philipstv/gui/strings.py | 3 ++- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 2e384fb..3d77b2d 100755 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -__version__ = '1.0.1' +__version__ = '1.0.2' import philipstv.gui diff --git a/philipstv/api/__init__.py b/philipstv/api/__init__.py index 33615fe..cd5f4af 100644 --- a/philipstv/api/__init__.py +++ b/philipstv/api/__init__.py @@ -33,7 +33,11 @@ def __init__(self, msg=None): class ApiError(requests.RequestException): - pass + def __str__(self): + try: + return self.response['error_text'] + except: + return super().__str__() class PhilipsAPI: @@ -131,7 +135,7 @@ def wakeup(self, waketime=None): waketime (float, optional): Time to wait after waking the TV. If None, class defaults are used. """ if waketime is None: waketime = self._waketime - if self.mac is not None: + if self.mac: send_magic_packet(self.mac) sleep(waketime) @@ -211,7 +215,7 @@ def pair_request(self): 'id': user } request_data = {'scope': ['read', 'write', 'control'], 'device': device} - resp = self.post('pair/request', request_data, auth=False) + resp = self.post('pair/request', request_data, auth=False, timeout=self._timeout*2) if resp is None or resp['error_id'] != 'SUCCESS': raise ApiError(response=resp) return {'device': device, 'user': user, 'passwd': resp['auth_key'], 'auth_timestamp': resp['timestamp']} @@ -233,7 +237,9 @@ def pair_grant(self, pin, device, user, passwd, auth_timestamp): 'auth_signature': 'authsignature' } grant_data = {'auth': auth, 'device': device} - self.post('pair/grant', grant_data, auth=HTTPDigestAuth(user, passwd)) + resp = self.post('pair/grant', grant_data, auth=HTTPDigestAuth(user, passwd)) + if resp is None or resp['error_id'] != 'SUCCESS': + raise ApiError(response=resp) self._user = user self._passwd = passwd self._create_auth() diff --git a/philipstv/gui/__init__.py b/philipstv/gui/__init__.py index 61308b4..3328222 100644 --- a/philipstv/gui/__init__.py +++ b/philipstv/gui/__init__.py @@ -18,7 +18,7 @@ from .lang import l, DEFAULT as DEFAULT_LANG from .strings import STRINGS -from ..api import PhilipsAPI, NotAuthorized +from ..api import PhilipsAPI, NotAuthorized, ApiError from ..api.discover import PhilipsTVDiscover BASE_PATH = os.path.dirname(os.path.abspath(__file__)) @@ -49,12 +49,18 @@ def on_release(self): app.config.set('philipstv', 'mac', '') app.config.write() app.setup_auth() - app.root.current = 'remote' try: app.api.get_applications() except NotAuthorized: - app.pair(app.set_mac) + def after_pair(): + app.config.set('philipstv', 'host', app.api.host) + app.config.write() + app.root.current = 'remote' + app.set_mac() + app.config.set('philipstv', 'host', '') + app.pair(after_pair) else: + app.root.current = 'remote' app.set_mac() except Exception as err: toast(l.tr(err), 4.0) @@ -313,22 +319,30 @@ def pair(self, callback=None): except Exception as err: toast(l.tr(err), 4.0) return - if data is not None: + self._pair_stage2(data, callback) - def on_pin_entered(instance): - try: - self.api.pair_grant(pin=instance.ids.pin_value.text, **data) - except Exception as err: - toast(l.tr(err), 4.0) + def _pair_stage2(self, data, callback=None): + def on_pin_entered(instance): + del self._popup + try: + self.api.pair_grant(pin=instance.ids.pin_value.text, **data) + except ApiError as err: + if err.response['error_id'] == 'INVALID_PIN': + self._pair_stage2(data, callback) + toast(l.tr("Invalid PIN"), 4.0) else: - self.save_auth() - if callback is not None: - callback() + toast(l.tr(err), 4.0) + except Exception as err: + toast(l.tr(err), 4.0) + else: + self.save_auth() + if callback is not None: + callback() - popup = Factory.PinPopup() - popup.bind(on_dismiss=on_pin_entered) - popup.open() + self._popup = Factory.PinPopup() + self._popup.bind(on_dismiss=on_pin_entered) + self._popup.open() def keypress(self, key): try: diff --git a/philipstv/gui/strings.py b/philipstv/gui/strings.py index 4d0ff3c..56765db 100644 --- a/philipstv/gui/strings.py +++ b/philipstv/gui/strings.py @@ -29,6 +29,7 @@ 'Lightness': "Jasność", 'Saturation': "Nasycenie", 'Enter PIN displayed on your TV': "Wprowadź kod PIN wyświetlony na ekranie telewizora", + 'Invalid PIN': "Niepoprawny kod PIN", '_advanced_settings_help': 'Otwórz ustawienia telewizora i wybierz [b]Wireless and Networks[/b] > [b]Wired of Wi-Fi[/b] ' '> [b]View network settings[/b]. Zanotuj wyświetlony adres IP. Następnie wprowadź go poniżej ' @@ -37,7 +38,7 @@ 'Cannot reach TV. Make sure you have set correct IP and Wake-on-Lan on your TV is on.': "Nie można połączyć się z telewizorem. Upewnij się, że masz ustawiony prawidłowy adres IP, " "a funkcja Wake-on-Lan na telewizorze jest włączona. ", - 'Remote not authorized. Please pair again.': "Pilot nie posiada autoryzacji. Proszę spować go ponownie.", + 'Remote not authorized. Please pair again.': "Pilot nie posiada autoryzacji. Proszę sparować go ponownie.", 'No host. Please set IP of your TV.': "Brak zdefiniowanego adresu IP telewizora. Proszę go podać w ustawieniach.", 'If your TV is not on the list, make sure, it is turned on and the phone is in the same WiFi as the TV...\n\n': "Jeżeli Twojego telewizora nie ma na liście powyżej, upewnij się, że jest on włączony, a telefon jest "