shauntarves / wyze-sdk

A modern Python client for controlling Wyze devices.
The Unlicense
309 stars 50 forks source link

Make this run faster? #65

Closed duckredbeard closed 2 years ago

duckredbeard commented 2 years ago

I am trying to replace the contact switches with magnetic reed switches connected to my Raspberry Pi. I am having some delay in getting the lights to come on when the door is opened. It takes about 7 seconds for the lights to respond when I open or close the door. I'm thinking lines 24-26 and 48-50 should be somewhere else (bulbS1 etc. assignments), so that their state is already known before the change in state is to be made. Like, instead of checking them when the door opens, already know what they are (on or off).

from gpiozero import Button, LED from signal import pause from time import sleep from datetime import datetime from datetime import timedelta import requests

from wyze_sdk import Client from wyze_sdk.errors import WyzeApiError client = Client(email="d***@gmail.com", password="****")

garagepassage = Button(20) # Input from garage passage door stairspassage = Button(18) # Input from door at top of stairs

now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Door Started at ", current_time) r = requests.post("https://autoremotejoaomgcd.appspot.com/sendnotification?key=APA9SecretAutoRemoteCodeo8V75nUXCU8solLv-Y2mbzGOzkybqKGibberishO2Ody2SsXjiC&title=Just%20Saying&text=Switch%20monitoring%20has%20begun")

def open():

now = datetime.now()

#current_time = now.strftime("%H:%M:%S")
#print("Door open at " + current_time)
bulbS1 = client.bulbs.info(device_mac='2CAA8E5B0550')
bulbS2 = client.bulbs.info(device_mac='2CAA8E5B13B1')
plugSLED = client.plugs.info(device_mac='7C78B2717462')
if bulbS1.is_on:
    client.bulbs.turn_off(device_mac=bulbS1.mac, device_model=bulbS1.product.model, after=timedelta(hours=.5))
else:
    client.bulbs.turn_on(device_mac=bulbS1.mac, device_model=bulbS1.product.model)
bulbS1 = client.bulbs.info(device_mac=bulbS1.mac)
if bulbS2.is_on:
    client.bulbs.turn_off(device_mac=bulbS2.mac, device_model=bulbS2.product.model, after=timedelta(hours=.5))
else:
    client.bulbs.turn_on(device_mac=bulbS2.mac, device_model=bulbS2.product.model)
bulbS2 = client.bulbs.info(device_mac=bulbS2.mac)
if plugSLED.is_on:
    client.plugs.turn_off(device_mac=plugSLED.mac, device_model=plugSLED.product.model, after=timedelta(hours=.5))
else:
    client.plugs.turn_on(device_mac=plugSLED.mac, device_model=plugSLED.product.model)
print("on")

def closed():

now = datetime.now()

#current_time = now.strftime("%H:%M:%S")
#print("Door closed at " + current_time)
bulbS1 = client.bulbs.info(device_mac='2CAA8E5B0550')
bulbS2 = client.bulbs.info(device_mac='2CAA8E5B13B1')
plugSLED = client.plugs.info(device_mac='7C78B2717462')
if bulbS1.is_on:
    client.bulbs.turn_off(device_mac=bulbS1.mac, device_model=bulbS1.product.model, after=timedelta(hours=.05))
if bulbS2.is_on:
    client.bulbs.turn_off(device_mac=bulbS2.mac, device_model=bulbS2.product.model, after=timedelta(hours=.05))
if plugSLED.is_on:
    client.plugs.turn_off(device_mac=plugSLED.mac, device_model=plugSLED.product.model, after=timedelta(hours=.05))
print("off")

stairspassage.when_pressed = closed stairspassage.when_released = open garagepassage.when_pressed = closed garagepassage.when_released = open

pause()

TheGreatSkeeve commented 2 years ago

I could be wrong, but in my experience it looks like what's holding you up is creating all the Wyze objects (bulbS1 = client.bulbs.info(device_mac='2CAA8E5B0550')) inside your functions. I've been coding a Discord bot that does various Wyze bulb things, and initializing all the 30+ bulbs is what takes the longest.

You could make a loading function that you call when the program starts, that initializes all of those and then appends them to a list, you can also just keep the existing names. Really, you could just move all the lines creating Wyze objects outside of a function so they're run by default first.

I was just fiddling with this, and I set it up haphazardly to work with threading. I'll post it here in case it helps someone-

`from wyze_sdk import Client import threading

global client client = Client(email='[email]', password='password')

# Make empty list for house # Room names cooresponding with house list house_text = ["lab","bed","foyer","kitchen", "library"] # Empty variable for all the lights house = [[],[],[],[],[]]

# Initializes a bulb def getLight(mac,num): global house house[num].append(client.bulbs.info(device_mac=mac))

# Manually added MACs in lists for rooms, because lazy # Create a thread for creating each bulb object # These objects all dumps to the house list def load(): lights_lab = ['MAC','MAC','MAC'] lights_bed = ['MAC','MAC','MAC'] lights_foyer = ['MAC','MAC','MAC','MAC','MAC','MAC'] lights_kitchen = ['MAC','MAC','MAC'] light_library = ['MAC'] list = [lights_lab,lights_bed,lights_foyer,lights_kitchen,light_library] for i in range(0,len(list)): for j in range(0,len(list[i])): new_thread = threading.Thread(target=getLight, args=[list[i][j],i]) new_thread.start() new_thread.join()`

duckredbeard commented 2 years ago

Thanks for your reply. I found that moving the "bulbS1 = client.bulbs.info(device_mac='2CAA8E5B0550')" and other assignments to the top of the code (with the passage door button assignments) to be the fix. The lights now turn on almost instantly. Previously, they took 5-8 seconds. Last step is to add a PIR to the code so that motion in the stairway keeps the lights on.

shauntarves commented 2 years ago

You guys are awesome. It makes me so happy to know people found this useful. I wish I had more time to keep tinkering with it. I'm trying to keep up with al the devices and requests.