Operate smartlife power supply with python (de-IFTTT)

IFTTT finally charged

It seems that it will be charged from 2020/11/1. I've used it on webhooks so far, but it doesn't work. There is no choice but to develop alternatives. When I looked it up, it seems that it can be controlled by python, so I tried it.

I wasn't using it all the time, I just wanted to be able to turn the power on and off, so my motivation was to take the trouble to receive a paid service.

Rough flow

Basically, if you can use tinytuya, you can control it from python. However, in order to control with tinytuya, Device ID (= virtual ID), IP Addr, local key are required. Therefore, tuyapower (Python3) and tuya-cli (node) are required. There are many front stages and it is troublesome.

--Get IP Addr, virtual id with tuyapower --Get a local key with tuyacli (registration to iot.tuya.com required) --Control the target switch with tinytuya

Most of it is still written on tinytuya's github etc. Also called mere Japanese localization.

Tool installation

tinytuya / tuyapower / tuya-cli

# Install required libraries
$ sudo apt-get install python-crypto python-pip		# for RPi, Linux
$ python3 -m pip install pycryptodome    # or pycrypto, pyaes or Crypto
$ python3 -m pip install tinytuya        # or pytuya
$ python3 -m pip install tuyapower       # this tuyapower module
$ npm i @tuyapi/cli -g

Only tuya-cli is npm.

Get the required parameters

Device ID, IP address

$ python3 -m tuyapower
TuyaPower (Tuya compatible smart plug scanner) [0.0.25] tinytuya [1.0.3]

Scanning on UDP ports 6666 and 6667 for devices (15 retries)...

FOUND Device [Valid payload]: 192.168.x.xx
    ID = 02xx11xxbcxxc2xxffxx, product = xxSUxxTWxx8ExxTw, Version = 3.1
    Stats: on=False [Timeout polling device]
FOUND Device [Valid payload]: 192.168.x.xx
    ID = 02xx77xxbcxxc2xx99xx, product = xxSUxxTWxx8ExxTw, Version = 3.1
    Stats: on=False [Timeout polling device]
FOUND Device [Valid payload]: 192.168.1.62
    ID = 03xx01xxdcxx22xxccxx, product = xx0VxxFcxxBTxxWt, Version = 3.1
    Stats: on=True, W=0.1, mA=0.0, V=0.0 [OK]
                    
Scan Complete!  Found 3 devices.

We have 3 smartlife devices, so 3 will be displayed. The important thing is the ID and IP Address. Enter this ID as the tuya-cli virtual ID.

local key Tuya API key / Tuya secret

From iot.tuya.com, go to cloud Development and get the Tuya API and Tuya secret.

image.png

Access ID = Tuya API key Access Secret = Tuya secret

local key

Get it by running tuya-cli wizard. Targets key terms in json format.

$ tuya-cli wizard
? The API key from tuya.com: nnxxxxxxxxxxxxxxxxur
? The API secret from tuya.com 53xxxxxxxxxxxxxxxxxxxxxxxxxxxxa3
? Provide a 'virtual ID' of a device currently registered in the app: 03xxxxxxxxxxxxxxxx08
[
  {
    name: 'Note A',
    id: 'XXXXXXXXXXXXXXXXXXXX',
    key: 'YYYYYYYYYYYYYYYY'
  },
  {
    name: 'Note B',
    id: 'XXXXXXXXXXXXXXXXXXXX',
    key: 'YYYYYYYYYYYYYYYY'
  },
  {
    name: 'Note C',
    id: 'XXXXXXXXXXXXXXXXXXXX',
    key: 'YYYYYYYYYYYYYYYY'
  }
]

Sample code

Well, this is Pakuri. We will use the information that we have struggled with above.

DEVICE_ID_HERE: Device ID, Virtual ID (obtained at tuyapower / tuya-cli) IP_ADDRESS_HERE: IP address (obtained at tuyapower) LOCAL_KEY_HERE: key (obtained at tuya-cli)

import tinytuya

d = tinytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
d.set_version(3.3)
data = d.status()  # NOTE this does NOT require a valid key vor version 3.1

# Show status of first controlled switch on device
print('Dictionary %r' % data)
print('State (bool, true is ON) %r' % data['dps']['1'])  

# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state)

data = d.status()
print('State (bool, true is ON) %r' % data['dps']['1'])  

If you just want to get the status (data = d.status ()), you don't need a local key. When calling d.set_status, an error will be returned if the correct local key is not set. If set correctly, it works as follows. After passing set_status (not switch_status), the energized state has changed from False to True.

$ python3 sample.py 
Dictionary {'devId': '02XXXXXXXXXXXXXXXXeb', 'dps': {'1': False, '2': 0}}
State (bool, true is ON) False
State (bool, true is ON) True

The meross system is controlled by another method. It's easier to control from python.

The end

Recommended Posts

Operate smartlife power supply with python (de-IFTTT)
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
[Note] Operate MongoDB with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Operate a receipt printer with python
Try to operate Facebook with Python
Operate ECHONET Lite appliances with Python
[GCP] Operate Google Cloud Storage with Python
Principal component analysis with Power BI + Python
[Pyto] Operate iPhone Taptic Engine with Python
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Try to operate DB with Python and visualize with d3
How to operate Discord API with Python (bot registration)
[Automation] Operate GitLab with Python to facilitate inquiry management
Let's operate GPIO of Raspberry Pi with Python CGI
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Operate Redmine using Python Redmine
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python