Skip to content

Commit a651080

Browse files
authored
Merge pull request #77 from sinricpro/5.0.0-dev
5.0.0 dev
2 parents 2ae421a + 9d70231 commit a651080

46 files changed

Lines changed: 1497 additions & 109 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
## [3.1.1]
1+
## [5.0.0]
2+
- feat: Speaker, GarageDoor examples added.
3+
- feat: setMode, setRangeValue - instance id support added.
4+
- fix: Signature mismatch issue fixed.
5+
- fix: setSetting command response format.
26

3-
### Fix
7+
## [4.0.0]
48

5-
* Logging
9+
- BREAKING CHANGE: Remove `restoreDeviceStates` in order to change this at device level from server side instead of fixed value in client sdk.
610

7-
## [3.1.0]
11+
## [3.1.1]
812

9-
### Features
13+
- Fix Logging
14+
15+
## [3.1.0]
1016

11-
* Replaced with new SDK
17+
- Replaced with new SDK

examples/customdevice/customdevice_example.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@
2626
"color": {"r": 255, "g": 255, "b": 255},
2727
}
2828

29+
async def on_range_value(rangeValue: int, instance_id: str) -> bool:
30+
"""Handle range value changes."""
31+
if instance_id:
32+
print(f"[Range] Instance '{instance_id}' set to {rangeValue}")
33+
else:
34+
print(f"[Range] Set to {rangeValue}")
35+
return True
36+
37+
38+
async def on_mode_state(mode: str, instance_id: str) -> bool:
39+
"""Handle mode state changes."""
40+
if instance_id:
41+
print(f"[Mode] Instance '{instance_id}' set to {mode}")
42+
else:
43+
print(f"[Mode] Set to {mode}")
44+
return True
2945

3046
async def on_power_state(state: bool) -> bool:
3147
"""Handle power state changes."""
@@ -85,7 +101,6 @@ async def on_lock_state(state: str) -> bool:
85101
print(f"\n[Lock] State set to {state}")
86102
return True
87103

88-
89104
async def on_setting(setting: str, value: Any) -> bool:
90105
"""Handle device setting changes."""
91106
print(f"\n[Setting] {setting} = {value}")
@@ -134,6 +149,8 @@ async def main() -> None:
134149

135150
# Common
136151
custom_device.on_setting(on_setting)
152+
custom_device.on_mode_state(on_mode_state)
153+
custom_device.on_range_value(on_range_value)
137154

138155
# Add device to SinricPro
139156
sinric_pro.add(custom_device)

examples/fan/fan_example.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ async def on_power_state(state: bool) -> bool:
3838
return True
3939

4040

41-
async def on_range_value(speed: int) -> bool:
41+
async def on_range_value(speed: int, instance_id: str) -> bool:
4242
"""
4343
Handle fan speed change requests.
4444
4545
Args:
4646
speed: Fan speed level (0-100)
47+
instance_id: Instance ID for multi-instance range control (not used for fan)
4748
4849
Returns:
4950
True if successful, False otherwise
@@ -64,12 +65,13 @@ async def on_range_value(speed: int) -> bool:
6465
return True
6566

6667

67-
async def on_adjust_range_value(speed_delta: int) -> bool:
68+
async def on_adjust_range_value(speed_delta: int, instance_id: str) -> bool:
6869
"""
6970
Handle relative fan speed adjustment requests.
7071
7172
Args:
7273
speed_delta: Change in speed (-100 to +100)
74+
instance_id: Instance ID for multi-instance range control (not used for fan)
7375
7476
Returns:
7577
True if successful, False otherwise

examples/garagedoor/garage_door_example.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
current_state = "Close" # "Open" or "Close"
1515

16-
async def on_mode_state(state: str) -> bool:
16+
async def on_mode_state(state: str, instance_id: str) -> bool:
1717
"""
1818
Handle garage door open/close requests.
19-
19+
2020
Args:
2121
state: "Open" or "Close"
22+
instance_id: Instance ID (not used for garage door)
2223
"""
2324
global current_state
2425
print(f"Garage door command: {state}")
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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())

examples/temperaturesensor/temperature_sensor_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
from sinricpro import SinricPro, SinricProTemperatureSensor, SinricProConfig
1212

1313
# Device ID from SinricPro portal
14-
DEVICE_ID = "YOUR_DEVICE_ID_HERE" # Replace with your device ID
14+
DEVICE_ID = "YOUR-DEVICE-ID" # Replace with your device ID
1515

1616
# Credentials from SinricPro portal
17-
APP_KEY = os.getenv("SINRICPRO_APP_KEY", "YOUR_APP_KEY_HERE")
18-
APP_SECRET = os.getenv("SINRICPRO_APP_SECRET", "YOUR_APP_SECRET_HERE")
17+
APP_KEY = os.getenv("SINRICPRO_APP_KEY", "YOUR-APP-KEY")
18+
APP_SECRET = os.getenv("SINRICPRO_APP_SECRET", "YOUR-APP-SECRET")
1919

2020
async def simulate_sensor_readings(sensor: SinricProTemperatureSensor) -> None:
2121
"""Simulate temperature/humidity readings for testing."""

0 commit comments

Comments
 (0)