Created
June 12, 2021 04:42
-
-
Save tannercollin/4d408bcad87c90f925b2fbc682ed925d to your computer and use it in GitHub Desktop.
A script to add new Lutron Caseta devices using pylutron-caseta
This file contains hidden or 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
# A script to add new Lutron Caseta devices using pylutron-caseta | |
# by Tanner Collin, Apache Licensed | |
import asyncio | |
import json | |
import pylutron_caseta | |
from pylutron_caseta.smartbridge import Smartbridge | |
pylutron_caseta.smartbridge.REQUEST_TIMEOUT = 15.0 | |
device_heard = None | |
async def add_device(): | |
global device_heard | |
print('connecting to bridge...') | |
bridge = Smartbridge.create_tls( | |
'192.168.69.100', 'caseta.key', 'caseta.crt', 'caseta-bridge.crt' | |
) | |
await bridge.connect() | |
print('entering association mode...') | |
res = await bridge._request( | |
'UpdateRequest', | |
'/system/status', | |
{ | |
'SystemStatus': { | |
'InAssociationMode': True | |
} | |
} | |
) | |
print('association mode response:', 'OK' if res.Body['SystemStatus']['InAssociationMode'] else 'ERROR') | |
def recv_heard(res): | |
global device_heard | |
print('device heard:', res) | |
device_heard = res.Body['DeviceStatus']['DeviceHeard'] | |
res = await bridge._subscribe( | |
'/device/status/deviceheard', | |
recv_heard | |
) | |
print('subscribe deviceheard response: OK') | |
print('==== Please hold the bottom button for 10 seconds...') | |
while not device_heard: | |
await asyncio.sleep(0.1) | |
serial = device_heard['SerialNumber'] | |
print('Saw device:', serial) | |
name = input('==== What do you want to name it? ') | |
print('adding device...') | |
res = await bridge._request( | |
'CreateRequest', | |
'/device', | |
{ | |
'Device': { | |
'Name': name, | |
'SerialNumber': serial, | |
'AssociatedArea': { | |
'href': '/area/2' | |
} | |
} | |
} | |
) | |
zone = res.Body['Device']['LocalZones'][0]['href'] | |
print('add device response: OK, zone:', zone) | |
print('updating devices...') | |
await bridge._load_devices() | |
print('done updating') | |
print('exiting association mode...') | |
res = await bridge._request( | |
'UpdateRequest', | |
'/system/status', | |
{ | |
'SystemStatus': { | |
'InAssociationMode': False | |
} | |
} | |
) | |
print('association mode response:', 'OK' if not res.Body['SystemStatus']['InAssociationMode'] else 'ERROR') | |
print('finished.') | |
await bridge.close() | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(add_device()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment