Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
## [3.1.1]
## [5.0.0]
- feat: Speaker, GarageDoor examples added.
- feat: setMode, setRangeValue - instance id support added.
- fix: Signature mismatch issue fixed.
- fix: setSetting command response format.

### Fix
## [4.0.0]

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

## [3.1.0]
## [3.1.1]

### Features
- Fix Logging

## [3.1.0]

* Replaced with new SDK
- Replaced with new SDK
19 changes: 18 additions & 1 deletion examples/customdevice/customdevice_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
"color": {"r": 255, "g": 255, "b": 255},
}

async def on_range_value(rangeValue: int, instance_id: str) -> bool:
"""Handle range value changes."""
if instance_id:
print(f"[Range] Instance '{instance_id}' set to {rangeValue}")
else:
print(f"[Range] Set to {rangeValue}")
return True


async def on_mode_state(mode: str, instance_id: str) -> bool:
"""Handle mode state changes."""
if instance_id:
print(f"[Mode] Instance '{instance_id}' set to {mode}")
else:
print(f"[Mode] Set to {mode}")
return True

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


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

# Common
custom_device.on_setting(on_setting)
custom_device.on_mode_state(on_mode_state)
custom_device.on_range_value(on_range_value)

# Add device to SinricPro
sinric_pro.add(custom_device)
Expand Down
6 changes: 4 additions & 2 deletions examples/fan/fan_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ async def on_power_state(state: bool) -> bool:
return True


async def on_range_value(speed: int) -> bool:
async def on_range_value(speed: int, instance_id: str) -> bool:
"""
Handle fan speed change requests.

Args:
speed: Fan speed level (0-100)
instance_id: Instance ID for multi-instance range control (not used for fan)

Returns:
True if successful, False otherwise
Expand All @@ -64,12 +65,13 @@ async def on_range_value(speed: int) -> bool:
return True


async def on_adjust_range_value(speed_delta: int) -> bool:
async def on_adjust_range_value(speed_delta: int, instance_id: str) -> bool:
"""
Handle relative fan speed adjustment requests.

Args:
speed_delta: Change in speed (-100 to +100)
instance_id: Instance ID for multi-instance range control (not used for fan)

Returns:
True if successful, False otherwise
Expand Down
5 changes: 3 additions & 2 deletions examples/garagedoor/garage_door_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

current_state = "Close" # "Open" or "Close"

async def on_mode_state(state: str) -> bool:
async def on_mode_state(state: str, instance_id: str) -> bool:
"""
Handle garage door open/close requests.

Args:
state: "Open" or "Close"
instance_id: Instance ID (not used for garage door)
"""
global current_state
print(f"Garage door command: {state}")
Expand Down
157 changes: 157 additions & 0 deletions examples/speaker/speaker_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
"""
SinricPro Speaker Example

Demonstrates:
- Power control
- Volume control
- Mute control
- Media playback controls
- Equalizer settings
- Mode selection
"""

import asyncio
import os

from sinricpro import SinricPro, SinricProSpeaker, SinricProConfig

# Device ID from SinricPro portal
DEVICE_ID = "YOUR_DEVICE_ID_HERE" # Replace with your device ID

# Credentials from SinricPro portal
APP_KEY = os.getenv("SINRICPRO_APP_KEY", "YOUR_APP_KEY_HERE")
APP_SECRET = os.getenv("SINRICPRO_APP_SECRET", "YOUR_APP_SECRET_HERE")

# Speaker state
speaker_state = {
"power": False,
"volume": 50,
"muted": False,
"mode": "MUSIC",
"equalizer": {
"bass": 0,
"midrange": 0,
"treble": 0,
},
}

async def on_power_state(state: bool) -> bool:
"""Handle power state changes from SinricPro."""
print(f"[Power] Speaker turned {'ON' if state else 'OFF'}")
speaker_state["power"] = state
return True


async def on_volume(volume: int) -> bool:
"""Handle volume changes from SinricPro."""
print(f"[Volume] Set to {volume}")
speaker_state["volume"] = volume
return True


async def on_adjust_volume(volume_delta: int) -> bool:
"""Handle volume adjustments from SinricPro."""
print(f"[Volume] Adjust by {'+' if volume_delta > 0 else ''}{volume_delta}")
speaker_state["volume"] = max(0, min(100, speaker_state["volume"] + volume_delta))
print(f" New volume: {speaker_state['volume']}")
return True


async def on_mute(mute: bool) -> bool:
"""Handle mute state changes from SinricPro."""
print(f"[Mute] {'ON' if mute else 'OFF'}")
speaker_state["muted"] = mute
return True


async def on_media_control(control: str) -> bool:
"""Handle media control commands from SinricPro."""
print(f"[Media] {control}")
# control can be: Play, Pause, Stop, Next, Previous, Rewind, FastForward
return True


async def on_set_bands(bands: dict) -> bool:
"""Handle equalizer band settings from SinricPro."""
print(f"[Equalizer] Bands set: {bands}")
speaker_state["equalizer"].update(bands)
return True


async def on_adjust_bands(bands: dict) -> bool:
"""Handle equalizer band adjustments from SinricPro."""
print(f"[Equalizer] Bands adjusted: {bands}")
for band, delta in bands.items():
if band in speaker_state["equalizer"]:
speaker_state["equalizer"][band] += delta
return True


async def on_mode(mode: str, instance_id: str) -> bool:
"""Handle mode changes from SinricPro."""
print(f"[Mode] Set to {mode}")
speaker_state["mode"] = mode
# Mode can be: MUSIC, MOVIE, NIGHT, SPORT, TV, etc.
return True


async def main() -> None:
"""Main function."""
print("=" * 60)
print("SinricPro Smart Speaker Example")
print("=" * 60)

# Create SinricPro instance
sinric_pro = SinricPro.get_instance()

# Create speaker device
my_speaker = SinricProSpeaker(DEVICE_ID)

# Register callbacks
my_speaker.on_power_state(on_power_state)
my_speaker.on_volume(on_volume)
my_speaker.on_adjust_volume(on_adjust_volume)
my_speaker.on_mute(on_mute)
my_speaker.on_media_control(on_media_control)
my_speaker.on_set_bands(on_set_bands)
my_speaker.on_adjust_bands(on_adjust_bands)
my_speaker.on_mode_state(on_mode)

# Add device to SinricPro
sinric_pro.add(my_speaker)

# Configure and connect
config = SinricProConfig(
app_key=APP_KEY,
app_secret=APP_SECRET
)

try:
print("Connecting to SinricPro...")
await sinric_pro.begin(config)
print("Connected! You can now control your speaker via Alexa or Google Home.")
print()
print("Try saying:")
print(' "Alexa, turn on the speaker"')
print(' "Alexa, set volume to 50"')
print(' "Alexa, mute the speaker"')
print(' "Alexa, play music"')
print(' "Alexa, increase bass"')
print()
print("Press Ctrl+C to exit")

# Keep the application running
while True:
await asyncio.sleep(1)

except KeyboardInterrupt:
print("\nShutting down...")
except Exception as e:
print(f"Error: {e}")
finally:
await sinric_pro.stop()


if __name__ == "__main__":
# Run the async main function
asyncio.run(main())
6 changes: 3 additions & 3 deletions examples/temperaturesensor/temperature_sensor_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from sinricpro import SinricPro, SinricProTemperatureSensor, SinricProConfig

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

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

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