Giter VIP home page Giter VIP logo

google-assistant-smart-home's Introduction

google-assistant-smart-home

Simple Python framework to control your DIY smart home devices using Google Assistant

Requirements

  • Web server (you can use some single-board computer like Raspberry Pi)
  • Domain name
  • SSL certificate for HTTPS (you can use free certificate from Let's Entrypt)
  • This project is based on Flask, so you need Python 3.x.x and Flask to be installed

How to install

  • Checkout project somewhere on your server
  • Edit google_home.wsgi and change path to project directory
  • Deploy project to your web server using WSGI, don't forget to allow authorization header
  • Go to https://console.actions.google.com/ and create new project, select "smart home" type
  • Open "Develop"->"Invocation" and type name of your project
  • Open "Develop"->"Actions" and type fulfillment URL: https://your-domain-name/
  • Open "Develop"->"Account linking" and type

Client ID: some ID, just remember it for now

Client secret: some password, remember it too and keep it secret

Authorization URL: https://your-domain-name/auth/

Token URL: https://your-domain-name/token/

  • Open https://console.cloud.google.com -> "APIs & Services" -> "ENABLE APIS AND SERVICES" -> "HomeGraph API" -> "Manage" -> "Credentials" -> "Credentials in APIs & Services" -> "CREATE CREDENTIALS" -> "API Key", store it somewhere
  • Edit config.py and fill CLIENT_ID, CLIENT_SECRET and API_KEY with your credentials, also change USERS_DIRECTORY, TOKENS_DIRECTORY, and DEVICES_DIRECTORY to your users, tokens and devices paths
  • It's recommended to chmod go-rwx tokens users

How to use

  • Create file username.json in users directory and write json config for user with password and list of available devices:
{
    "password": "test",
    "devices": [
        "pc"
    ]
}

Example for simple on-off device:

{
    "type": "action.devices.types.SWITCH",
    "traits": [
        "action.devices.traits.OnOff"
    ],
    "name": {
        "name": "PC",
        "defaultNames": [
          "PC",
          "Computer"
        ],
        "nicknames": [
          "PC",
          "Computer"
        ]
    },
    "willReportState": false,
    "roomHint": "My room",
    "deviceInfo": {
        "manufacturer": "Cluster",
        "model": "1",
        "hwVersion": "1",
        "swVersion": "1"
    }
}
  • Create file device-name.py in devices directory and write python script with two methods: device-name_query(custom_data) and device-name_command(custom_data, command, params)

Example script to turn on/off PC:

import subprocess

def pc_query(custom_data):
    p = subprocess.run(["ping", "-c", "1", "192.168.0.2"], stdout=subprocess.PIPE)
    state = p.returncode == 0
    return {"on": state, "online": True}

def pc_action(custom_data, command, params):
    if command == "action.devices.commands.OnOff":
        if params['on']:
            subprocess.run(["wakeonlan", "-i", "192.168.0.255", "00:11:22:33:44:55"])
        else:
            subprocess.run(["sh", "-c", "echo shutdown -h | ssh [email protected]"])
        return {"status": "SUCCESS", "states": {"on": params['on'], "online": True}}
    else:
        return {"status": "ERROR"}

Query fuction must return device status object, and action function must return action result. Please read traits documentation for more info: https://developers.google.com/assistant/smarthome/traits.

  • Open Google Home app on your phone and link it with your project
  • Done! You can control your devices using voice commands or Google Home app
  • Run sync.py script when you need to update devices list

google-assistant-smart-home's People

Contributors

clusterm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.