Created
November 7, 2024 16:02
-
-
Save Darkflib/065ded2bc8d0b54e98b6027819c6b138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pysmartthings | |
import asyncio | |
import logging | |
import os | |
import dotenv | |
import aiohttp | |
dotenv.load_dotenv() | |
# Enable default logging | |
logging.basicConfig(level=logging.DEBUG) | |
STTOKEN = os.getenv("ST_TOKEN") | |
# Log the token for debugging | |
logging.info(f"Token: {STTOKEN}") | |
async def main(): | |
# Create the API object | |
async with aiohttp.ClientSession() as session: | |
api = pysmartthings.SmartThings(session, STTOKEN) | |
locations = await api.locations() | |
print(len(locations)) | |
for location in locations: | |
print(location.name) | |
print(location.location_id) | |
devices = await api.devices() | |
print(len(devices)) | |
bulbs = [] | |
for device in devices: | |
print(f"Device Name: {device.name}") | |
print(f"Device Label: {device.label}") | |
print(f"Device ID: {device.device_id}") | |
#print(f"Device Type Name: {device.device_type_name}") | |
print(f"Capabilities: {device.capabilities}") | |
#print(f"Status: {device.status}") | |
print(f"Location ID: {device.location_id}") | |
#print(f"Room ID: {device.room_id}") | |
#print(f"Device Type ID: {device.device_type_id}") | |
# and a blank line | |
print() | |
# if the device is a only a bulb, turn it on (we don't want to switch a mains switch) | |
if "switch" in device.capabilities and device.name.startswith("rgbw"): | |
bulbs.append(device) | |
# get the device status | |
await device.status.refresh() | |
print(f"Status Values: {device.status.values}") | |
print(f"Switch Status: {device.status.switch}") | |
print(f"Level: {device.status.level}") | |
print(f"Color: {device.status.color}") | |
#print(f"Turning on {device.name}") | |
#await device.switch_on() | |
#await device.set_level(20) | |
#await asyncio.sleep(5) | |
#await device.set_level(50) | |
#await asyncio.sleep(5) | |
#await device.set_level(20) | |
#await asyncio.sleep(5) | |
#await device.switch_off() | |
#print(f"Turned off {device.name}") | |
#await device.set_color_temperature(2700) | |
#await device.set_level(20) | |
print() | |
"""Set color for multiple bulbs at once.""" | |
tasks = [] | |
hue = 100 | |
saturation = 100 | |
level = 20 | |
for bulb in bulbs: | |
if "colorControl" in bulb.capabilities: | |
# Create tasks for setting hue, saturation, level, and turning on each bulb | |
tasks.append(bulb.set_hue(hue)) | |
tasks.append(bulb.set_saturation(saturation)) | |
tasks.append(bulb.set_level(level)) | |
tasks.append(bulb.switch_on()) | |
await asyncio.gather(*tasks) | |
# Run the main function | |
asyncio.run(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment