Search code examples
pythongoogle-assistant-sdk

Google Assistant API, controlling a light switch connected to Google Home


I have machine A that just cranks out .png files. It gets synced to machine B and I view it on machine B.

Sometimes machine A crashes for whatever reason and stops doing the scheduled jobs, which means then files on machine B will be old.

I want machine B to run a script to see if the file is older than 1 day, and if it is, then reset the power switch on machine A, so that it can be cold booted. The switch is connected to Google Home but understand I have to use the Assistant API.

I have installed the google-assistant-sdk[samples] package. Can someone show me some code on how to query and return all devices then flip the switch on and off on that device?


Solution

  • Unfortunately, many smart home companies are building products for consumers, not developers. Google's SDK is letting developers stream consumer voice requests to their servers and turning that into actions. Gosund, similarly, is only interested in API access for Amazon and Google. They're API is probably not documented for public use.

    To answer your specific question, if you want to use the Google Assistant SDK, you would name your switch something like "Server A Switch" and record a short clip of you saying "Turn off Server A Switch" and "Turn on Server A Switch" and send those two google. The way google matches the requests with your particular account is through OAuth2 tokens, which google will give you in exchange for valid sign in credentials.

    If Gosund works with Google Assistant, it has a standard OAuth2 server endpoint as well as a Google Assistant compliant API endpoint. I only recommend this if you want to have some fun reverse engineering it.

    In your Google Assistant app, if you try adding the Gosund integration, the first screen popup is the url endpoint where you can exchange valid Gosund account credentials for a one-time code which you can then exchange for OAuth2 access and refresh tokens. With the access token you can theoretically control your switch. The commands you'll want to send are standardized by Google. However, you'll have to figure out where to send them. The best bet here is probably to email their developers.

    Are you familiar with OAuth2? If not, I don't recommend doing any of the above.

    Your other option is to prevent Server A from hardware crashes. This is what I recommend as the least amount of work. You should start with a server that never crashes, keep it that way and add stuff on top of it. If you only have two servers, they should be able to maintain many months of uptime. Run your scheduled jobs using cron or systemctl and have a watchdog that restarts the job when it detects an error. If your job is crashing the server maybe put it in a VM like docker or something, which gives you neat auto-restart capabilities off the bat.

    Another hacky thing you can do is schedule your gosund plug to turn off and on once a day through their consumer UI or app, or at whatever frequency you feel like is most optimal.