tlyakhov / python-decora_wifi

Python Library for Interacting with Leviton Decora Smart WiFi Switches & Dimmers
MIT License
37 stars 21 forks source link

How do I turn on/off a room? #13

Closed sundown94 closed 4 years ago

sundown94 commented 4 years ago

Hi! I am able to turn switches on or off, one at a time. However, I just recently added six switches to a "room" in the Leviton mobile app and they will all turn on or off if I press the button in the app. How can I do the same thing using this Python code? I thought I could just set the [power] attribute for the room to ON or OFF, but while that did turn the power button on/off in the app, it did not actually turn the switches on or off. Looking further into the "residential_room.py" code, I see that there is a "turn_on" and "turn_off" class method that may do the trick (maybe...not even sure about this), but I don't know how to use them. I tried adding the following code to the "cli_test.py" script, but line # 2 fails with an "Authorization Required" error message:

room = all_residences[0].find_by_id_residential_rooms('62109') #this works great room.turn_on(session) #this gives me an error

Thanks! Steve-

tlyakhov commented 4 years ago

Hey Steve, I haven't tried that particular method, but what you could definitely do is just loop through all the lights in a room and switch them on individually.

tlyakhov commented 4 years ago

Actually now that I look at it, turn_on is a class_method. So try room.turn_on(session, { 'id': room.id }). Some of the API methods work like that, it's kind of weird.

sundown94 commented 4 years ago

Hi Tim! THANK YOU SO MUCH!! (Sorry for the caps...I am just so excited!!) That totally worked!!!! After posting my original message above, last night I did end up using a loop which you have also suggested in your first response. But with your second post about proper usage of the room.turn_on method, I can now submit one command to turn on six switches all at once (which are actually dimmer modules that control 10 candles in my front windows). I never would have thought that I needed to send the room ID attribute again within the parameters, but now I think I understand that this is because turn_on is a class_method and does not automatically get the room ID from the "room." part. In fact, I was now able to add from decora_wifi.models.residential_room import ResidentialRoom and then replace my code with ResidentialRoom.turn_on(session, { 'id': room.id }). This last part didn't really buy me anything other than to help me learn a little more about how this stuff works. And as you can probably tell by now, I am new to Python. :-)
Thank you again...I really appreciate your help!! Steve-