Python Forum
Refresh token for Wyze SDK - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Refresh token for Wyze SDK (/thread-37233.html)



Refresh token for Wyze SDK - duckredbeard - May-16-2022

I have added a few Wyze smart bulbs to my home security system, running them on their own program but using door sensors from the security program. Everything works when I start the program, but after a few days the token expires and I get errors. I want the token to refresh maybe once per day as a precaution. In the github thread (found here) for this Wyze SDK, a post contained the suggestion that I add a loop to my code. According to that thread, the loop would look like this:

client = Client(email=os.getenv('WYZE_EMAIL'), password=os.getenv('WYZE_PASS'))
 -- Loop here --
     <check sensor status>
     response=client.refresh_token()
     client._token = response["data"]["access_token"]
     client._refresh_token = response["data"]["refresh_token"]
But I'm not sure where to put it in my code (sensitive stuff removed). How do I add a daily or every few hours recurring refresh command?

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="[email protected]", password="ShutYaMouf")

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=APA91bHuMGY0IPSomeSecretStuffHereygUkO2Ody2SsXjiC&title=Just%20Saying&text=Switch%20monitoring%20has%20begun")

bulbS1 = client.bulbs.info(device_mac='LKFI*&DFYF')
bulbS2 = client.bulbs.info(device_mac='HFKHDKFh686F')



def open():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Door open at " + current_time)
    client.bulbs.turn_on(device_mac=bulbS1.mac, device_model=bulbS1.product.model)
    client.bulbs.turn_on(device_mac=bulbS2.mac, device_model=bulbS2.product.model)
    client.bulbs.turn_off(device_mac=bulbS1.mac, device_model=bulbS1.product.model, after=timedelta(hours=.5))    
    client.bulbs.turn_off(device_mac=bulbS2.mac, device_model=bulbS2.product.model, after=timedelta(hours=.5))
    print("on")
    


def closed():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Door closed at " + current_time)
    client.bulbs.turn_off(device_mac=bulbS1.mac, device_model=bulbS1.product.model, after=timedelta(hours=.1))
    client.bulbs.turn_off(device_mac=bulbS2.mac, device_model=bulbS2.product.model, after=timedelta(hours=.1))
    print("off")
        
stairspassage.when_pressed = closed
stairspassage.when_released = open
garagepassage.when_pressed = closed
garagepassage.when_released = open

pause()