mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
improve gpmdp (#2951)
This commit is contained in:
parent
c5fd665151
commit
0c91ba4a50
@ -33,8 +33,9 @@ def request_configuration(hass, config, url, add_devices_callback):
|
|||||||
return
|
return
|
||||||
from websocket import create_connection
|
from websocket import create_connection
|
||||||
websocket = create_connection((url), timeout=1)
|
websocket = create_connection((url), timeout=1)
|
||||||
websocket.send('{"namespace": "connect", "method": "connect",'
|
websocket.send(json.dumps({'namespace': 'connect',
|
||||||
'"arguments": ["Home Assistant"]}')
|
'method': 'connect',
|
||||||
|
'arguments': ['Home Assistant']}))
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def gpmdp_configuration_callback(callback_data):
|
def gpmdp_configuration_callback(callback_data):
|
||||||
@ -49,14 +50,14 @@ def request_configuration(hass, config, url, add_devices_callback):
|
|||||||
continue
|
continue
|
||||||
if msg['payload'] != "CODE_REQUIRED":
|
if msg['payload'] != "CODE_REQUIRED":
|
||||||
continue
|
continue
|
||||||
websocket.send('{"namespace": "connect",'
|
pin = callback_data.get('pin')
|
||||||
'"method": "connect",'
|
websocket.send(json.dumps({'namespace': 'connect',
|
||||||
'"arguments": ["Home Assistant",'
|
'method': 'connect',
|
||||||
' "' + callback_data.get('pin') + '"]}')
|
'arguments': ['Home Assistant', pin]}))
|
||||||
tmpmsg = json.loads(websocket.recv())
|
tmpmsg = json.loads(websocket.recv())
|
||||||
if tmpmsg['channel'] == 'time':
|
if tmpmsg['channel'] == 'time':
|
||||||
_LOGGER.error('Error setting up GPMDP. Please pause'
|
_LOGGER.error('Error setting up GPMDP. Please pause'
|
||||||
'the desktop player and try again.')
|
' the desktop player and try again.')
|
||||||
break
|
break
|
||||||
code = tmpmsg['payload']
|
code = tmpmsg['payload']
|
||||||
if code == 'CODE_REQUIRED':
|
if code == 'CODE_REQUIRED':
|
||||||
@ -65,11 +66,11 @@ def request_configuration(hass, config, url, add_devices_callback):
|
|||||||
add_devices_callback)
|
add_devices_callback)
|
||||||
_save_config(hass.config.path(GPMDP_CONFIG_FILE),
|
_save_config(hass.config.path(GPMDP_CONFIG_FILE),
|
||||||
{"CODE": code})
|
{"CODE": code})
|
||||||
websocket.send('{"namespace": "connect",'
|
websocket.send(json.dumps({'namespace': 'connect',
|
||||||
'"method": "connect",'
|
'method': 'connect',
|
||||||
'"arguments": ["Home Assistant",'
|
'arguments': ['Home Assistant', code]}))
|
||||||
' "' + code + '"]}')
|
|
||||||
websocket.close()
|
websocket.close()
|
||||||
|
break
|
||||||
|
|
||||||
_CONFIGURING['gpmdp'] = configurator.request_config(
|
_CONFIGURING['gpmdp'] = configurator.request_config(
|
||||||
hass, "GPM Desktop Player", gpmdp_configuration_callback,
|
hass, "GPM Desktop Player", gpmdp_configuration_callback,
|
||||||
@ -159,6 +160,9 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
self._title = None
|
self._title = None
|
||||||
self._artist = None
|
self._artist = None
|
||||||
self._albumart = None
|
self._albumart = None
|
||||||
|
self._seek_position = None
|
||||||
|
self._duration = None
|
||||||
|
self._request_id = 0
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def get_ws(self):
|
def get_ws(self):
|
||||||
@ -176,33 +180,46 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
self._ws = None
|
self._ws = None
|
||||||
return self._ws
|
return self._ws
|
||||||
|
|
||||||
|
def send_msg_with_req_id(self, method):
|
||||||
|
"""Send ws messages to GPMDP and verify request id in response."""
|
||||||
|
from websocket import _exceptions
|
||||||
|
try:
|
||||||
|
websocket = self.get_ws()
|
||||||
|
if websocket is None:
|
||||||
|
self._status = STATE_OFF
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self._request_id += 1
|
||||||
|
websocket.send(json.dumps({'namespace': 'playback',
|
||||||
|
'method': method,
|
||||||
|
'requestID': self._request_id}))
|
||||||
|
while True:
|
||||||
|
msg = json.loads(websocket.recv())
|
||||||
|
if 'requestID' in msg:
|
||||||
|
if msg['requestID'] == self._request_id:
|
||||||
|
return msg
|
||||||
|
except (_exceptions.WebSocketTimeoutException,
|
||||||
|
_exceptions.WebSocketProtocolException,
|
||||||
|
_exceptions.WebSocketPayloadException):
|
||||||
|
return
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest details from the player."""
|
"""Get the latest details from the player."""
|
||||||
websocket = self.get_ws()
|
playstate = self.send_msg_with_req_id('isPlaying')
|
||||||
if websocket is None:
|
if playstate is None:
|
||||||
self._status = STATE_OFF
|
|
||||||
return
|
return
|
||||||
else:
|
self._status = STATE_PLAYING if playstate['value'] else STATE_PAUSED
|
||||||
receiving = True
|
time_data = self.send_msg_with_req_id('getCurrentTime')
|
||||||
while receiving:
|
if time_data is None:
|
||||||
from websocket import _exceptions
|
return
|
||||||
try:
|
self._seek_position = int(time_data['value'] / 1000)
|
||||||
msg = json.loads(websocket.recv())
|
track_data = self.send_msg_with_req_id('getCurrentTrack')
|
||||||
if msg['channel'] == 'lyrics':
|
if track_data is None:
|
||||||
receiving = False # end of now playing data
|
return
|
||||||
elif msg['channel'] == 'playState':
|
self._title = track_data['value']['title']
|
||||||
if msg['payload'] is True:
|
self._artist = track_data['value']['artist']
|
||||||
self._status = STATE_PLAYING
|
self._albumart = track_data['value']['albumArt']
|
||||||
else:
|
self._duration = int(track_data['value']['duration'] / 1000)
|
||||||
self._status = STATE_PAUSED
|
|
||||||
elif msg['channel'] == 'track':
|
|
||||||
self._title = (msg['payload']['title'])
|
|
||||||
self._artist = (msg['payload']['artist'])
|
|
||||||
self._albumart = (msg['payload']['albumArt'])
|
|
||||||
except (_exceptions.WebSocketTimeoutException,
|
|
||||||
_exceptions.WebSocketProtocolException,
|
|
||||||
_exceptions.WebSocketPayloadException):
|
|
||||||
return
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_content_type(self):
|
def media_content_type(self):
|
||||||
@ -229,6 +246,16 @@ class GPMDP(MediaPlayerDevice):
|
|||||||
"""Image url of current playing media."""
|
"""Image url of current playing media."""
|
||||||
return self._albumart
|
return self._albumart
|
||||||
|
|
||||||
|
@property
|
||||||
|
def media_seek_position(self):
|
||||||
|
"""Time in seconds of current seek positon."""
|
||||||
|
return self._seek_position
|
||||||
|
|
||||||
|
@property
|
||||||
|
def media_duration(self):
|
||||||
|
"""Time in seconds of current song duration."""
|
||||||
|
return self._duration
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the device."""
|
"""Return the name of the device."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user