Skip to content

Commit

Permalink
Add multiple device select example
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Oct 19, 2024
1 parent 11f43cc commit 438d88c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ turn_on('Dining Room')
## Multi-Threaded Example

[threading.py](threading.py) - Example that uses python threading to connect to multiple devices and listen for updates.

## Multiple Device Select Example

[multi-select.py](multi-select.py) - Example that uses python select() to connect to multiple devices and listen for updates simultaneously. By using select(), the program avoids having to create separate threads for each device and can efficiently handle multiple device connections in a single loop.
103 changes: 103 additions & 0 deletions examples/multi-select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# TinyTuya Example
# -*- coding: utf-8 -*-
"""
TinyTuya - Multi-select Example
This demonstrates how to use pythons socket select() to listen for status updates
from multiple Tuya devices.
Setup:
Set the config for each device and the script will open a socket connection for
each device to listen for status updates.
Author: Jason A. Cox
For more information see https://github.com/jasonacox/tinytuya
"""
import select
import time
import tinytuya

# Define the devices to control
config = {
"TuyaDevices": [
{
"Address": "192.168.1.10",
"Device ID": "00112233445566778899",
"Local Key": "1234567890123abc",
"Version": "3.3",
},
{
"Address": "192.168.1.11",
"Device ID": "10112233445566778899",
"Local Key": "1234567890123abc",
"Version": "3.3",
},
{
"Address": "192.168.1.12",
"Device ID": "20112233445566778899",
"Local Key": "1234567890123abc",
"Version": "3.3",
},
{
"Address": "192.168.1.13",
"Device ID": "30112233445566778899",
"Local Key": "1234567890123abc",
"Version": "3.3",
}
]
}

# Create array, devices, that is an array of tinytuya.Device objects
devices = []
for i in config["TuyaDevices"]:
print(f"Connecting to {i['Device ID']} at {i['Address']} with key {i['Local Key']}")
d = tinytuya.Device(i["Device ID"], i["Address"], i["Local Key"], version=i["Version"])
devices.append(d) # Add the device to the devices array

def getDeviceStatuses():
global devices
global statuses

# Enable persistent socket connection for each device
for device in devices:
device.set_socketPersistent(True)
# Call status() once to get the initial status and connect
initial_status = device.status()
device_id = device.id
index = devices.index(device)
print(f"INITIAL status from {device_id}: {initial_status}")
statuses[index] = {"id": device_id, "status": initial_status["dps"]}

# Create a list of sockets to monitor
sockets = [device.socket for device in devices]

# Infinite loop to listen for status updates using select
while True:
# Use select to wait for any of the device sockets to have data
readable, _, _ = select.select(sockets, [], [], 0.1)

# Process each socket with incoming data
for sock in readable:
# Find the corresponding device for this socket
device = next(device for device in devices if device.socket == sock)
updated_status = device.receive()

if updated_status:
print(f"UPDATE status from {device.id}: {updated_status}")
index = devices.index(device)
# We may only get one DPS, so just update that one item
if "dps" in updated_status:
for key in updated_status["dps"]:
statuses[index]["status"][key] = updated_status["dps"][key]
print(f" - Updated status for {device.id} DPS {key} to {updated_status['dps'][key]}")

# Add a small delay (optional) to prevent tight looping
time.sleep(0.1)

# Example usage
statuses = [None] * len(devices) # Initialize statuses list to hold results for each device

# Start the status listener
getDeviceStatuses()

0 comments on commit 438d88c

Please sign in to comment.