- Code: Select all
Script Error schedule "TPLink 04 Check Power State" embedded script error:
Script Error a bytes-like object is required, not 'str'
Script Error Exception Traceback (most recent call shown last):
embedded script, line 59, at top level
TypeError: a bytes-like object is required, not 'str'
Here is the Script in question:
- Code: Select all
#!/usr/bin/env python
#
# TP-Link Wi-Fi Smart Plug Protocol Client
# For use with TP-Link HS-100 or HS-110
#
# by Lubomir Stroetmann
# Copyright 2016 softScheck GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
import socket
import indigo
import json
# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
def encrypt(string):
key = 171
result = "\0\0\0\0"
for i in string:
a = key ^ ord(i)
key = a
result += chr(a)
return result
def decrypt(string):
key = 171
result = ""
for i in string:
a = key ^ ord(i)
key = ord(i)
result += chr(a)
return result
# Set target IP, port and command to send
ip = '192.168.20.144'
port = 9999
cmd = '{"system":{"get_sysinfo":{}}}'
var = indigo.variables[981436479] # Change this to the actual ID of your variable
# Send command and receive reply
try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port))
sock_tcp.send(encrypt(cmd))
data = sock_tcp.recv(2048)
sock_tcp.close()
# indigo.server.log("Sent: ", cmd)
# indigo.server.log("Received: ", decrypt(data[4:]))
jsonData = json.loads(decrypt(data[4:]))
var.value = unicode( jsonData["system"]["get_sysinfo"]["relay_state"])
var.replaceOnServer()
except socket.error:
indigo.server.error("Cound not connect to host " + ip + ":" + str(port))