|
| 1 | +"""SinricPro Device Settings Example - Handle device-level configuration.""" |
| 2 | +import asyncio |
| 3 | +import os |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from sinricpro import SinricPro, SinricProBlinds, SinricProConfig |
| 7 | + |
| 8 | +# Device ID from SinricPro portal |
| 9 | +DEVICE_ID = "YOUR_DEVICE_ID_HERE" # Replace with your device ID |
| 10 | + |
| 11 | +# Credentials from SinricPro portal |
| 12 | +APP_KEY = os.getenv("SINRICPRO_APP_KEY", "YOUR_APP_KEY_HERE") |
| 13 | +APP_SECRET = os.getenv("SINRICPRO_APP_SECRET", "YOUR_APP_SECRET_HERE") |
| 14 | + |
| 15 | +# Device state |
| 16 | +power_state = False |
| 17 | +position = 0 # 0-100 |
| 18 | + |
| 19 | +# Device-specific settings |
| 20 | +device_settings = { |
| 21 | + "id_tilt": 50, # Tilt angle (0-100) |
| 22 | + "id_direction": "up", # Movement direction preference |
| 23 | + "id_speed": "normal", # Movement speed: slow, normal, fast |
| 24 | + "id_auto_close": False, # Auto-close after timeout |
| 25 | + "id_close_timeout": 300, # Auto-close timeout in seconds |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +async def on_power_state(state: bool) -> bool: |
| 30 | + """Handle power state change requests.""" |
| 31 | + global power_state |
| 32 | + print(f"\n[Callback] Power: {'ON' if state else 'OFF'}") |
| 33 | + power_state = state |
| 34 | + return True |
| 35 | + |
| 36 | + |
| 37 | +async def on_device_setting(setting_id: str, value: Any) -> bool: |
| 38 | + """ |
| 39 | + Handle device-level setting changes. |
| 40 | +
|
| 41 | + Device settings are configuration values specific to this device, |
| 42 | + such as tilt angle, movement direction, speed preferences, etc. |
| 43 | +
|
| 44 | + Args: |
| 45 | + setting_id: The setting identifier (e.g., "tilt", "direction") |
| 46 | + value: The new value for the setting (can be int, float, bool, or string) |
| 47 | +
|
| 48 | + Returns: |
| 49 | + True if the setting was applied successfully, False otherwise |
| 50 | + """ |
| 51 | + print(f"\n[Device Setting] {setting_id} = {value} (type: {type(value).__name__})") |
| 52 | + |
| 53 | + # Handle tilt setting |
| 54 | + if setting_id == "id_tilt": |
| 55 | + if isinstance(value, (int, float)) and 0 <= value <= 100: |
| 56 | + device_settings["tilt"] = int(value) |
| 57 | + print(f" Tilt angle set to {int(value)}%") |
| 58 | + # TODO: Apply tilt to physical device |
| 59 | + # set_blinds_tilt(int(value)) |
| 60 | + return True |
| 61 | + else: |
| 62 | + print(f" Invalid tilt value: {value} (must be 0-100)") |
| 63 | + return False |
| 64 | + |
| 65 | + # Handle direction setting |
| 66 | + elif setting_id == "id_direction": |
| 67 | + valid_directions = ["up", "down"] |
| 68 | + if isinstance(value, str) and value.lower() in valid_directions: |
| 69 | + device_settings["direction"] = value.lower() |
| 70 | + print(f" Direction preference set to '{value.lower()}'") |
| 71 | + return True |
| 72 | + else: |
| 73 | + print(f" Invalid direction value: {value} (must be 'up' or 'down')") |
| 74 | + return False |
| 75 | + |
| 76 | + # Handle speed setting |
| 77 | + elif setting_id == "id_speed": |
| 78 | + valid_speeds = ["slow", "normal", "fast"] |
| 79 | + if isinstance(value, str) and value.lower() in valid_speeds: |
| 80 | + device_settings["speed"] = value.lower() |
| 81 | + print(f" Movement speed set to '{value.lower()}'") |
| 82 | + # TODO: Apply speed to physical device |
| 83 | + # set_motor_speed(value.lower()) |
| 84 | + return True |
| 85 | + else: |
| 86 | + print(f" Invalid speed value: {value} (must be 'slow', 'normal', or 'fast')") |
| 87 | + return False |
| 88 | + |
| 89 | + # Handle auto_close setting |
| 90 | + elif setting_id == "id_auto_close": |
| 91 | + if isinstance(value, bool): |
| 92 | + device_settings["auto_close"] = value |
| 93 | + print(f" Auto-close {'enabled' if value else 'disabled'}") |
| 94 | + return True |
| 95 | + else: |
| 96 | + print(f" Invalid auto_close value: {value} (must be boolean)") |
| 97 | + return False |
| 98 | + |
| 99 | + # Handle close_timeout setting |
| 100 | + elif setting_id == "id_close_timeout": |
| 101 | + if isinstance(value, (int, float)) and 60 <= value <= 3600: |
| 102 | + device_settings["close_timeout"] = int(value) |
| 103 | + print(f" Close timeout set to {int(value)} seconds") |
| 104 | + return True |
| 105 | + else: |
| 106 | + print(f" Invalid close_timeout value: {value} (must be 60-3600)") |
| 107 | + return False |
| 108 | + |
| 109 | + else: |
| 110 | + print(f" Unknown setting: {setting_id}") |
| 111 | + return False |
| 112 | + |
| 113 | + |
| 114 | +async def main() -> None: |
| 115 | + # Get SinricPro instance |
| 116 | + sinric_pro = SinricPro.get_instance() |
| 117 | + |
| 118 | + # Create a blinds device |
| 119 | + blinds = SinricProBlinds(DEVICE_ID) |
| 120 | + |
| 121 | + # Register device callbacks |
| 122 | + blinds.on_power_state(on_power_state) |
| 123 | + |
| 124 | + # Register device-level setting callback |
| 125 | + # This handles settings specific to this device |
| 126 | + blinds.on_setting(on_device_setting) |
| 127 | + |
| 128 | + # Add device to SinricPro |
| 129 | + sinric_pro.add(blinds) |
| 130 | + |
| 131 | + # Example function to demonstrate sending device setting events |
| 132 | + async def send_example_setting(): |
| 133 | + """Send an example device setting event after connection.""" |
| 134 | + await asyncio.sleep(5) # Wait for connection to stabilize |
| 135 | + print("\n[Example] Sending device setting event...") |
| 136 | + sent = await blinds.send_setting_event("id_tilt", 75) |
| 137 | + print(f" Device setting event sent: {sent}") |
| 138 | + |
| 139 | + # Configure connection |
| 140 | + config = SinricProConfig(app_key=APP_KEY, app_secret=APP_SECRET) |
| 141 | + |
| 142 | + try: |
| 143 | + print("=" * 60) |
| 144 | + print("SinricPro Device Settings Example") |
| 145 | + print("=" * 60) |
| 146 | + print("\nConnecting to SinricPro...") |
| 147 | + await sinric_pro.begin(config) |
| 148 | + print("Connected!") |
| 149 | + |
| 150 | + print("\n" + "=" * 60) |
| 151 | + print("Device Settings vs Module Settings:") |
| 152 | + print("=" * 60) |
| 153 | + print(" Device Settings: Configuration for THIS specific device") |
| 154 | + print(" - Receive via: device.on_setting(callback)") |
| 155 | + print(" - Send via: device.send_setting_event(setting_id, value)") |
| 156 | + print(" - Examples: Tilt angle, speed, direction, auto-close") |
| 157 | + print(" - Callback receives: (setting_id, value)") |
| 158 | + print("") |
| 159 | + print(" Module Settings: Configuration for the module/board") |
| 160 | + print(" - Receive via: sinric_pro.on_set_setting(callback)") |
| 161 | + print(" - Send via: sinric_pro.send_setting_event(setting_id, value)") |
| 162 | + print(" - Examples: WiFi retry count, log level") |
| 163 | + |
| 164 | + print("\n" + "=" * 60) |
| 165 | + print("Current Device Settings:") |
| 166 | + print("=" * 60) |
| 167 | + for key, value in device_settings.items(): |
| 168 | + print(f" {key}: {value}") |
| 169 | + |
| 170 | + print("\n" + "=" * 60) |
| 171 | + print("Voice Commands:") |
| 172 | + print("=" * 60) |
| 173 | + print(" 'Alexa, turn on [device name]'") |
| 174 | + print(" 'Alexa, set [device name] to 50 percent'") |
| 175 | + print(" (Device settings are configured via SinricPro portal)") |
| 176 | + |
| 177 | + print("\n" + "=" * 60) |
| 178 | + print("Press Ctrl+C to exit") |
| 179 | + print("=" * 60) |
| 180 | + |
| 181 | + # Start the example setting event task |
| 182 | + #asyncio.create_task(send_example_setting()) |
| 183 | + |
| 184 | + while True: |
| 185 | + await asyncio.sleep(1) |
| 186 | + |
| 187 | + except KeyboardInterrupt: |
| 188 | + print("\n\nShutting down...") |
| 189 | + except Exception as e: |
| 190 | + print(f"\nError: {e}") |
| 191 | + import traceback |
| 192 | + traceback.print_exc() |
| 193 | + finally: |
| 194 | + await sinric_pro.stop() |
| 195 | + print("Disconnected.") |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + asyncio.run(main()) |
0 commit comments