Skip to content

Instantly share code, notes, and snippets.

@dennisheitmann
Last active February 14, 2022 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisheitmann/dce4ce91064c169b8e45826251321888 to your computer and use it in GitHub Desktop.
Save dennisheitmann/dce4ce91064c169b8e45826251321888 to your computer and use it in GitHub Desktop.
Tuya Power Socket: Get Voltage and Switch Power (on/off)
#!/usr/bin/python3
#
# MIT License
#
# Copyright (c) 2021 Dennis Heitmann
import sys
import json
import hmac
import hashlib
import requests
import time
t = str(int(time.time()*1000))
base = 'https://openapi.tuyaeu.com'
device = 'SECRET'
client_id = 'SECRET'
secret = 'SECRET'
def makeSign(msg):
sign = hmac.new(key = bytes(secret, 'latin-1'), msg = msg, digestmod = hashlib.sha256).hexdigest().upper()
return sign
def getAccessToken():
sign = makeSign(bytes(client_id + t, 'latin-1'))
headers = {
'sign_method': 'HMAC-SHA256',
'client_id': client_id,
't': t,
'mode': 'cors',
'Content-Type': 'application/json',
'sign': sign,
'secret': secret,
}
url = '/v1.0/token?grant_type=1'
tuyaResponse = requests.get(base + url, headers = headers)
access_token = tuyaResponse.json()['result'].get('access_token')
return access_token
def getDeviceStatus(device):
access_token = getAccessToken()
sign = makeSign(bytes(client_id + access_token + t, 'latin-1'))
headers = {
'sign_method': 'HMAC-SHA256',
'client_id': client_id,
't': t,
'mode': 'cors',
'Content-Type': 'application/json',
'sign': sign,
'access_token': access_token,
}
url = '/v1.0/devices/' + device + '/status'
tuyaResponse = requests.get(base + url, headers = headers)
tuyaStatus = tuyaResponse.json()['result']
return tuyaStatus
def switchDevice(state):
if state == 0: state = False
if state == "off": state = False
if state == "OFF": state = False
if state == 1: state = True
if state == "on": state = True
if state == "ON": state = True
if type(state) != bool:
print('Wrong input (0/1, on/off)')
return 1
command = {"commands":[{"code": "switch_1", "value": state},]}
access_token = getAccessToken()
sign = makeSign(bytes(client_id + access_token + t, 'latin-1'))
headers = {
'sign_method': 'HMAC-SHA256',
'client_id': client_id,
't': t,
'mode': 'cors',
'Content-Type': 'application/json',
'sign': sign,
'access_token': access_token,
}
url = '/v1.0/devices/' + device + '/commands'
tuyaResponse = requests.post(base + url, headers = headers, data = json.dumps(command))
tuyaStatus = tuyaResponse.json()
return tuyaStatus
status = getDeviceStatus(device)
voltage = status[-1].get('value')/10
print(voltage)
if len(sys.argv) > 1:
print(switchDevice(sys.argv[1]))
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment