Wifi smart plugs

I don't know if anyone else has tried these but I recently obtained a couple of wifi smart plugs from amazon:

https://www.amazon.co.uk/gp/product/B07CVJYV3G

There are quite a few around using various brand names and based on the ESP8266 wifi chip I think. While the RF plugs that I use are generally pretty reliable these give another option and it would seem could be integrated with the HAH using python code from here:

https://github.com/clach04/python-tuya

Member for

13 years 4 months

kema01

7 years 10 months ago

Allan I have just purchased a wifi smart plug and am quite impressed with its speed to respond "Alexa" commands.

I wish I had the skills to write the software for the Home Automation Hub and incorporate/integrate these devices into the HAH platform.

Member for

15 years 1 month

allanayr

7 years 10 months ago

I'm not sure that my software skills are quite up to it either!

You need to install the python-tuya stuff from here:

https://github.com/clach04/python-tuya

and also the xaplib.py stuff from here:

http://www.dbzoo.com/livebox/hah_xap_python

You're welcome to have a go with what I have so far.

I've abandoned the lua script and just run this python script on my raspi on the same network as my livebox.

 

#!/usr/bin/python
from subprocess import *
import pytuya
import socket
import sys
import os
from xaplib import Xap
plugID = ['00','Xxxxx' ,'Xxxxx' ,'Xxxxx' ]
plugIP = ['00', 'Xxxxx', 'Xxxxx', 'Xxxxx']
plugKey = ['00','Xxxxx' ,'Xxxxx' ,'Xxxxx' ]
def hubTraffic(xap):
    xapmessage, port = xap.receive()
   
    parsed = xapmessage.split('\n')
    #print(parsed[6])
    if parsed[5] == 'class=xAPBSC.cmd':
        for _ in range(3):
            try:

                      if parsed[6].find('tuya') > 0:
                        state = parsed[12][-2:]
                        #print(state)
                        if state == 'on':
                           state = ' 1'
                        else:
                           state = ' 0'
                        #print(state)
                        tgt = parsed[6][-1:]
                        #print(tgt)
                        tgt = tgt+state
                        #print(tgt)
                        switchDevice(tgt)
                        #print(xapmessage)
                        break
            except socket.error, msg:
                        print('Socket Error')
                        socket.close()
                        exitcode = Popen('bscmsg -t dbzoo.livebox.twitter -x Tuya_plug_failure -s on',shell=True, stdout=PIPE).stdout
           
                        sys.exit()
           

       
       
def switchDevice(patt):
    data = patt
    t = int(data[:1])
    d = pytuya.OutletDevice(plugID[t], plugIP[t], plugKey[t])
    mess = 'Switching plug '+data[:1]
    print(mess)
    if int(data[-1:]) == 1:
        print('on')
        ndata = d.set_status(True) # This requires a valid key
    else:
        print('off')
        ndata = d.set_status(False) # This requires a valid key
   
Xap("FF000F00","dbzoo.livebox.tuyamanager").run(hubTraffic)

 

Obviously you need to put in your plugID, plugIP and plugKey in place of the Xxxxx fields. (leave the 00's in place at index 0)

 

With this script running I issus a "bscmsg -t dbzoo.livebox.tuya:plug.(1|2|3) -s (on|off)"

I haven't created these as bsc endpoints (although I suppose I could) I just rely on the script to pick up the xap message and react.

It switches the plugs pretty instantaneously when it works. As I said in my original posting there is a reliability issue with devices only able to maintain one connection at a time so if I am issuing frequent switching commands or if someone opens the app the script breaks :-(

 

So feel free to have a go and let me know how you get on.

Member for

15 years 1 month

allanayr

7 years 9 months ago

of the script. It's been running on a spare raspi for about a week now without breaking so it seems more stable and useable than the last one.

It attempts to reconnect if it fails to send the signal.

Attachments
tuyacon.txt (2.3 KB)

Member for

13 years 4 months

kema01

7 years 9 months ago

Hi Allan many thanks for your instructions.