|
| 1 | +""" |
| 2 | +SinricPro Speaker Example |
| 3 | +
|
| 4 | +Demonstrates: |
| 5 | +- Power control |
| 6 | +- Volume control |
| 7 | +- Mute control |
| 8 | +- Media playback controls |
| 9 | +- Equalizer settings |
| 10 | +- Mode selection |
| 11 | +""" |
| 12 | + |
| 13 | +import asyncio |
| 14 | +import os |
| 15 | + |
| 16 | +from sinricpro import SinricPro, SinricProSpeaker, SinricProConfig |
| 17 | + |
| 18 | +# Device ID from SinricPro portal |
| 19 | +DEVICE_ID = "YOUR_DEVICE_ID_HERE" # Replace with your device ID |
| 20 | + |
| 21 | +# Credentials from SinricPro portal |
| 22 | +APP_KEY = os.getenv("SINRICPRO_APP_KEY", "YOUR_APP_KEY_HERE") |
| 23 | +APP_SECRET = os.getenv("SINRICPRO_APP_SECRET", "YOUR_APP_SECRET_HERE") |
| 24 | + |
| 25 | +# Speaker state |
| 26 | +speaker_state = { |
| 27 | + "power": False, |
| 28 | + "volume": 50, |
| 29 | + "muted": False, |
| 30 | + "mode": "MUSIC", |
| 31 | + "equalizer": { |
| 32 | + "bass": 0, |
| 33 | + "midrange": 0, |
| 34 | + "treble": 0, |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +async def on_power_state(state: bool) -> bool: |
| 39 | + """Handle power state changes from SinricPro.""" |
| 40 | + print(f"[Power] Speaker turned {'ON' if state else 'OFF'}") |
| 41 | + speaker_state["power"] = state |
| 42 | + return True |
| 43 | + |
| 44 | + |
| 45 | +async def on_volume(volume: int) -> bool: |
| 46 | + """Handle volume changes from SinricPro.""" |
| 47 | + print(f"[Volume] Set to {volume}") |
| 48 | + speaker_state["volume"] = volume |
| 49 | + return True |
| 50 | + |
| 51 | + |
| 52 | +async def on_adjust_volume(volume_delta: int) -> bool: |
| 53 | + """Handle volume adjustments from SinricPro.""" |
| 54 | + print(f"[Volume] Adjust by {'+' if volume_delta > 0 else ''}{volume_delta}") |
| 55 | + speaker_state["volume"] = max(0, min(100, speaker_state["volume"] + volume_delta)) |
| 56 | + print(f" New volume: {speaker_state['volume']}") |
| 57 | + return True |
| 58 | + |
| 59 | + |
| 60 | +async def on_mute(mute: bool) -> bool: |
| 61 | + """Handle mute state changes from SinricPro.""" |
| 62 | + print(f"[Mute] {'ON' if mute else 'OFF'}") |
| 63 | + speaker_state["muted"] = mute |
| 64 | + return True |
| 65 | + |
| 66 | + |
| 67 | +async def on_media_control(control: str) -> bool: |
| 68 | + """Handle media control commands from SinricPro.""" |
| 69 | + print(f"[Media] {control}") |
| 70 | + # control can be: Play, Pause, Stop, Next, Previous, Rewind, FastForward |
| 71 | + return True |
| 72 | + |
| 73 | + |
| 74 | +async def on_set_bands(bands: dict) -> bool: |
| 75 | + """Handle equalizer band settings from SinricPro.""" |
| 76 | + print(f"[Equalizer] Bands set: {bands}") |
| 77 | + speaker_state["equalizer"].update(bands) |
| 78 | + return True |
| 79 | + |
| 80 | + |
| 81 | +async def on_adjust_bands(bands: dict) -> bool: |
| 82 | + """Handle equalizer band adjustments from SinricPro.""" |
| 83 | + print(f"[Equalizer] Bands adjusted: {bands}") |
| 84 | + for band, delta in bands.items(): |
| 85 | + if band in speaker_state["equalizer"]: |
| 86 | + speaker_state["equalizer"][band] += delta |
| 87 | + return True |
| 88 | + |
| 89 | + |
| 90 | +async def on_mode(mode: str, instance_id: str) -> bool: |
| 91 | + """Handle mode changes from SinricPro.""" |
| 92 | + print(f"[Mode] Set to {mode}") |
| 93 | + speaker_state["mode"] = mode |
| 94 | + # Mode can be: MUSIC, MOVIE, NIGHT, SPORT, TV, etc. |
| 95 | + return True |
| 96 | + |
| 97 | + |
| 98 | +async def main() -> None: |
| 99 | + """Main function.""" |
| 100 | + print("=" * 60) |
| 101 | + print("SinricPro Smart Speaker Example") |
| 102 | + print("=" * 60) |
| 103 | + |
| 104 | + # Create SinricPro instance |
| 105 | + sinric_pro = SinricPro.get_instance() |
| 106 | + |
| 107 | + # Create speaker device |
| 108 | + my_speaker = SinricProSpeaker(DEVICE_ID) |
| 109 | + |
| 110 | + # Register callbacks |
| 111 | + my_speaker.on_power_state(on_power_state) |
| 112 | + my_speaker.on_volume(on_volume) |
| 113 | + my_speaker.on_adjust_volume(on_adjust_volume) |
| 114 | + my_speaker.on_mute(on_mute) |
| 115 | + my_speaker.on_media_control(on_media_control) |
| 116 | + my_speaker.on_set_bands(on_set_bands) |
| 117 | + my_speaker.on_adjust_bands(on_adjust_bands) |
| 118 | + my_speaker.on_mode_state(on_mode) |
| 119 | + |
| 120 | + # Add device to SinricPro |
| 121 | + sinric_pro.add(my_speaker) |
| 122 | + |
| 123 | + # Configure and connect |
| 124 | + config = SinricProConfig( |
| 125 | + app_key=APP_KEY, |
| 126 | + app_secret=APP_SECRET |
| 127 | + ) |
| 128 | + |
| 129 | + try: |
| 130 | + print("Connecting to SinricPro...") |
| 131 | + await sinric_pro.begin(config) |
| 132 | + print("Connected! You can now control your speaker via Alexa or Google Home.") |
| 133 | + print() |
| 134 | + print("Try saying:") |
| 135 | + print(' "Alexa, turn on the speaker"') |
| 136 | + print(' "Alexa, set volume to 50"') |
| 137 | + print(' "Alexa, mute the speaker"') |
| 138 | + print(' "Alexa, play music"') |
| 139 | + print(' "Alexa, increase bass"') |
| 140 | + print() |
| 141 | + print("Press Ctrl+C to exit") |
| 142 | + |
| 143 | + # Keep the application running |
| 144 | + while True: |
| 145 | + await asyncio.sleep(1) |
| 146 | + |
| 147 | + except KeyboardInterrupt: |
| 148 | + print("\nShutting down...") |
| 149 | + except Exception as e: |
| 150 | + print(f"Error: {e}") |
| 151 | + finally: |
| 152 | + await sinric_pro.stop() |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + # Run the async main function |
| 157 | + asyncio.run(main()) |
0 commit comments