Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 21, 2024
1 parent c33feb5 commit 620773c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ To run this code for test I am doing::
# Pauses the first player
ym.pause_player(next(iter(ym.players)))
# Sleep will let the terminal show events coming back. For dev today.
time.sleep(60)
time.sleep(60)
37 changes: 21 additions & 16 deletions yoto_api/YotoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import requests
import logging
import datetime
import time
import paho.mqtt.client as mqtt

from datetime import timedelta
Expand Down Expand Up @@ -281,48 +280,54 @@ def _get_cards(self, token: Token) -> dict:
# }
# }


def connect_mqtt(self, token: Token, deviceId: str):
def on_message(client, userdata, message):
# Process MQTT Message
_LOGGER.debug(f"{DOMAIN} - MQTT Message: {str(message.payload.decode('utf-8'))}")
_LOGGER.debug(
f"{DOMAIN} - MQTT Message: {str(message.payload.decode('utf-8'))}"
)
_LOGGER.debug(f"{DOMAIN} - MQTT Topic: {message.topic}")
_LOGGER.debug(f"{DOMAIN} - MQTT QOS: {message.qos}")
_LOGGER.debug(f"{DOMAIN} - MQTT Retain: {message.retain}")

client=mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id="DASH"+deviceId, transport="websockets")
client.username_pw_set(username=deviceId + "?x-amz-customauthorizer-name=" + self.MQTT_AUTH_NAME,password=token.access_token)
client = mqtt.Client(
mqtt.CallbackAPIVersion.VERSION1,
client_id="DASH" + deviceId,
transport="websockets",
)
client.username_pw_set(
username=deviceId + "?x-amz-customauthorizer-name=" + self.MQTT_AUTH_NAME,
password=token.access_token,
)
# client.on_connect = on_message
client.on_message = on_message
client.tls_set()
client.connect(host=self.MQTT_URL, port=443)
client.loop_start()
client.subscribe("device/"+deviceId+"/events")
client.subscribe("device/"+deviceId+"/status")
client.subscribe("device/"+deviceId+"/response")
client.subscribe("device/" + deviceId + "/events")
client.subscribe("device/" + deviceId + "/status")
client.subscribe("device/" + deviceId + "/response")
# Command not needed but helps sniffing traffic
client.subscribe("device/"+deviceId+"/command")
client.subscribe("device/" + deviceId + "/command")

#time.sleep(60)
#client.loop_stop()
# time.sleep(60)
# client.loop_stop()
return client


def card_pause(self, client, deviceId):
topic = "device/" + deviceId + "/command/card-pause"
payload = ""
self._publish_command(client, topic, payload)
# MQTT Message: {"status":{"card-pause":"OK","req_body":""}}

def card_play(self, client, deviceId):
topic = "device/" + deviceId + "/command/card-play"
self._publish_command(self, client, topic, "card-play")
#MQTT Message: {"status":{"card-play":"OK","req_body":"{\"uri\":\"https://yoto.io/7JtVV\",\"secondsIn\":0,\"cutOff\":0,\"chapterKey\":\"01\",\"trackKey\":\"01\",\"requestId\":\"5385910e-f853-4f34-99a4-d2ed94f02f6d\"}"}}
# MQTT Message: {"status":{"card-play":"OK","req_body":"{\"uri\":\"https://yoto.io/7JtVV\",\"secondsIn\":0,\"cutOff\":0,\"chapterKey\":\"01\",\"trackKey\":\"01\",\"requestId\":\"5385910e-f853-4f34-99a4-d2ed94f02f6d\"}"}}

def _publish_command(self, client, topic, payload):
client.publish(topic, payload)


def _get_card_detail(self, token: Token, cardid: str) -> dict:
############## Details below from snooping JSON requests of the app ######################

Expand Down
22 changes: 20 additions & 2 deletions yoto_api/YotoPlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ class YotoPlayer:

# {'devices': [{'deviceId': 'y23IBS76kCaOSrGlz29XhIFO', 'name': 'Yoto Player', 'description': 'nameless.limit', 'online': False, 'releaseChannel': 'general', 'deviceType': 'v3', 'deviceFamily': 'v3', 'deviceGroup': '', 'hasUserGivenName': False}]}

# Mqtt response:
# Mqtt response:

{"status":{"battery":4174,"powerCaps":"0x02","batteryLevel":100,"batteryTemp":0,"batteryData":"0:0:0","batteryLevelRaw":100,"free":3875896,"freeDMA":100298,"free32":100638,"upTime":130228,"utcTime":1713713466,"aliveTime":4103240,"qiOtp":0,"nightlightMode":"0x194a55","temp":"notSupported"}}
{
"status": {
"battery": 4174,
"powerCaps": "0x02",
"batteryLevel": 100,
"batteryTemp": 0,
"batteryData": "0:0:0",
"batteryLevelRaw": 100,
"free": 3875896,
"freeDMA": 100298,
"free32": 100638,
"upTime": 130228,
"utcTime": 1713713466,
"aliveTime": 4103240,
"qiOtp": 0,
"nightlightMode": "0x194a55",
"temp": "notSupported",
}
}

0 comments on commit 620773c

Please sign in to comment.