Skip to content

Commit

Permalink
Pairing bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
macdems committed Aug 22, 2021
1 parent 90b7f6b commit 0e137d8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
__version__ = '1.0.1'
__version__ = '1.0.2'

import philipstv.gui

Expand Down
14 changes: 10 additions & 4 deletions philipstv/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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']}
Expand All @@ -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()
Expand Down
44 changes: 29 additions & 15 deletions philipstv/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion philipstv/gui/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand All @@ -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 "
Expand Down

0 comments on commit 0e137d8

Please sign in to comment.