Repository navigation
Expand file tree
/
Copy pathsimple_client.py
More file actions
executable file
·155 lines (123 loc) · 4.65 KB
/
Copy pathsimple_client.py
File metadata and controls
executable file
·155 lines (123 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
"""
Simple example of connecting to the Yoto API and controlling a player.
This demonstrates:
1. Authentication using device code flow
2. Getting player status
3. Basic player control (pause)
Requirements:
- yoto_api library installed
- YOTO_CLIENT_ID environment variable set
"""
import logging
import os
import sys
import time
from pathlib import Path
try:
from yoto_api import YotoManager
except ImportError:
print("Error: yoto_api library not found.")
print("Install it with: pip install yoto_api")
sys.exit(1)
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def main():
"""Main function to demonstrate Yoto API usage."""
# Get client ID from environment
client_id = os.getenv("YOTO_CLIENT_ID")
if not client_id:
logger.error("YOTO_CLIENT_ID environment variable not set")
logger.info("Get your client ID from: https://yoto.dev/get-started/start-here/")
sys.exit(1)
# Initialize Yoto Manager
logger.info("Initializing Yoto Manager...")
ym = YotoManager(client_id=client_id)
# Check for stored refresh token
token_file = Path(".yoto_refresh_token")
refresh_token = None
if token_file.exists():
refresh_token = token_file.read_text().strip()
logger.info("Found stored refresh token, attempting to use it...")
try:
ym.set_refresh_token(refresh_token)
ym.check_and_refresh_token()
logger.info("Successfully authenticated with stored token!")
except Exception as e:
logger.warning(f"Stored token failed: {e}")
logger.info("Will perform device code flow authentication...")
refresh_token = None
# If no valid token, do device code flow
if not refresh_token:
logger.info("Starting device code flow authentication...")
device_info = ym.device_code_flow_start()
print("\n" + "=" * 60)
print("AUTHENTICATION REQUIRED")
print("=" * 60)
print(f"\n1. Go to: {device_info['verification_uri']}")
print(f"2. Enter code: {device_info['user_code']}")
print("\nWaiting for authorization...")
print("(This typically takes 15-30 seconds after you approve)\n")
# Wait for user to complete authorization
time.sleep(20)
try:
ym.device_code_flow_complete()
logger.info("Authentication successful!")
# Save refresh token for future use
if hasattr(ym.token, "refresh_token"):
token_file.write_text(ym.token.refresh_token)
logger.info(f"Refresh token saved to {token_file}")
except Exception as e:
logger.error(f"Authentication failed: {e}")
sys.exit(1)
# Get and display player information
logger.info("Fetching player status...")
ym.update_players_status()
if not ym.players:
logger.warning("No players found on your account")
return
print("\n" + "=" * 60)
print("YOTO PLAYERS")
print("=" * 60 + "\n")
for player_id, player in ym.players.items():
print(f"Player: {player.name}")
print(f" ID: {player_id}")
print(f" Online: {player.online}")
volume = player.volume if hasattr(player, "volume") and player.volume is not None else 50
print(f" Volume: {volume}%")
if hasattr(player, "playing") and player.playing:
print(" Status: Playing")
else:
print(" Status: Not playing")
if hasattr(player, "battery_level"):
print(f" Battery: {player.battery_level}%")
print()
# Connect to MQTT for real-time events
logger.info("Connecting to MQTT for real-time events...")
try:
ym.connect_to_events()
logger.info("Connected to MQTT successfully!")
except Exception as e:
logger.error(f"Failed to connect to MQTT: {e}")
return
# Example: Pause the first player
first_player_id = next(iter(ym.players.keys()))
print(f"\nExample: Pausing player {ym.players[first_player_id].name}...")
try:
ym.pause_player(first_player_id)
logger.info("Pause command sent!")
except Exception as e:
logger.error(f"Failed to pause player: {e}")
# Keep alive to receive MQTT events
print("\nListening for events for 30 seconds...")
print("(Try interacting with your Yoto player to see events)\n")
try:
time.sleep(30)
except KeyboardInterrupt:
logger.info("Interrupted by user")
logger.info("Example complete!")
if __name__ == "__main__":
main()