Automation with Alexa using python.
Tags: python, javascript, cpp, alexa
Published: 2019-12-16 09:23:06

Hi, Being an electronics and programming enthusiastic, I always wondered how can I automate my room appliances (not with smart bulbs). There were many different ways to come up with like Bluetooth, LAN, MQTT, etc. Then I thought of automating with Alexa for existing devices. There are 2 ways of doing it. The easy way and the hard way. So, for now, let's talk about the easy way.


There are a lot of automation skills in the Alexa skill store but most of them support smart devices. After doing some research I found a really good skill called [sinric](https://sinric.com) which let's me control existing appliances with some extra circuitry. Thanks to [Aruna Tennakoon](https://github.com/kakopappa) for providing this feature. Setting up this skill is straight forward.

- Signup 
- Get API key
- Add skill to your Alexa
- Use these [examples](https://github.com/kakopappa/sinric)
- That's It.

Sinric is the 1st version with many kinds of device supported. Recently 2nd version of  [sinric](https://sinric.com) is up which is called [sinric pro](https://sinric.pro). We'll discuss the latest version.


let's see how to set up the skill.

# 1. Signup
  - Goto [sinric pro website](https://sinric.pro) and signup.
  - Verify your email.

# 2. Create devices
  - Click on the `Devices` button on the sidebar.
  - ![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/rxtaxdy8m495g2zxha7k.jpeg)
  - Then select `Add Device`.
  - Select which device type you need and let the access key be the default for now.
  - ![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/l1blc1sn5i41koz4qezf.jpeg)
  - That's it.


# 3. Linking sinric pro to Alexa

  - Goto to Alexa's skills & games and search for [sinric pro skill](https://www.amazon.in/HOME-Sinric-Pro/dp/B07ZT5VDT8/ref=sr_1_2?crid=1U4W5ES006ORH&keywords=sinric&qid=1576448010&s=digital-skills&sprefix=s%2Calexa-skills%2C292&sr=1-2).
  - As soon you link the skill you will be redirected to the login page.
  - Login with the credentials which you used signup for [sinric pro website](https://sinric.pro)
  - After successful login just go back to your Alexa app and it'll start discovering the devices which you added in sinric pro account.
  - So linking skill is done.

# 4. Controlling devices with Alexa

  - Here's where the coding part begins.
  - Sinric Pro has supporting libraries for [python](https://github.com/sinricpro/python-sdk), [c++(nodemcu)](https://github.com/sinricpro/esp8266-esp32-sdk), [nodejs](https://github.com/sinricpro/nodejs-sdk).
  - Let's start with python.

# 5. Integration with python

  - **Installing python library for [sinricpro](https://sinric.pro)**
       
        pip install sinricpro
  
  - You can find the source code for the python library [here](https://github.com/sinricpro/python-sdk).
  
  - Just a reminder. This library is only supported for python versions 3.7+.

**Initializing with access key and secret key**

{% gist https://gist.github.com/imdhanush/a00f9c479a3cec9d95f8e8da6e33d77f %}

#### Let's see how to get these keys.
  - Login to sinricpro [here](https://portal.sinric.pro) 
  - Click on [credentials](https://portal.sinric.pro/credential/list) button on the sidebar.
  - There you'll find 2 keys. One is `app key` and another is `secret key`
    basically `app key` is used to provide API service. `secret key` is used to provide security for the information exchanged between devices.

#### Getting device ids.
  - Device IDs are the unique ids that are used to differentiate between devices.
  - As you can see there are 2 more fields `fan` and `light` in the code above.
  - Go to the devices page [here](https://portal.sinric.pro/device/list).
  - Copy the device ID as shown below and paste it in the code.
  ![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nr2a59mfjo3skq8cj37q.jpeg)
  - Done. 

So moving forward, let's see how to connect to sinricpro. Basically there are 2 ways for controlling a device with sinricpro.
 - Action.
 - Event.
 
## Action
  - Action is something that Alexa does for you. Like when say `Alexa, turn on light` or when you control device from website or app.

## Event
  - Event is used to control the device manually. Like when you press a 
button so light should change its state or much more.

**Defining action and event callbacks**

{% gist https://gist.github.com/imdhanush/26407616424ae62f30e346b0af02ec67 %}

**Next step is to initialize the library with callbacks**
{% gist https://gist.github.com/imdhanush/0addfadd61de7643f1ccb25052726458 %}

**Let's see how a complete code looks like.**
{% gist https://gist.github.com/imdhanush/dd48a224ce5ada2cc8791f55fc3b6cee %}

**That's it ;). When you run the code your devices will be online you can control with Alexa or [website portal](portal.sinric.pro) or [app](https://play.google.com/store/apps/details?id=pro.sinric).**


For more examples, click [here](https://github.com/sinricpro/python-examples).
For more help, click [here](https://sinric.pro).

Thank you :).