unparagoned / njsTuya

Openhab interface for Tuya home automation devices sold under various names
27 stars 11 forks source link

Multi-outlet Devices #18

Closed edwolfe3 closed 4 years ago

edwolfe3 commented 5 years ago

I have Smart Life wall outlets that have 2 outlets each. They are labeled as "[device_id]_1" and [device_id]_2. If I want to turn both on (I'm using openHAB), I send the command "[device_id] ON" and that works fine. However, when the status is obtained, I send the command "[device_id] STATUS and it returns all my devices. That is because it doesn't associate that only treat them as a group and never individually. I have actually modified the njstuya.js file to support this scenario. I only modified the runCloud function as that is the only one I'm using, but I replaced the line that reads

const status = ((tuyaID.length > 0) && deviceStates[tuyaID]) || deviceStates;

to the following:

   var status;
    if (tuyaID.length > 0) {
      status = deviceStates[tuyaID];
      if (!status) {
        status = deviceStates[tuyaID + "_1"];
        if (!status) status = "undefined"; //deviceStates;
      }
    }

Can you considered incorporating a solution to handle this case?

unparagoned commented 5 years ago

First I'm pleased to see people getting cloud control working. On first glance I'm not too sure what the problem is. Doesn't running the following work? [device_id]_1 STATUS

edwolfe3 commented 5 years ago

Yes, but the outlets allow you to issue ON/OFF commands by saying [device_id]_1 (or _2) ON to control each separately; or, I can turn them both ON/OFF by saying [device_id] ON. Since I always control this outlet as a group, I have it defined in openHAB (using an exec binding) as a group. Therefore if I want to get the status of it, I would like to be able to issue the command with just [device_id] STATUS to check if it is on of off. In other words, I treat a double outlet device as a singe device using just the device id. Is that making more sense?

edwolfe3 commented 5 years ago

FYI: If you are able to support group id in the code sometime that would be great. In the mean, to avoid issues when upgrading, I've gone ahead and removed the modified code (shown above) and I'm no using a python script to deal with it. I call it from the openHAB exec binding. Below is my script if you are interested:

#!/usr/bin/python

import subprocess
import json
import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-id", help="the deviced id to perform action on")
parser.add_argument("-gr", "--group",  help="if specified instead of -id, the device id is a group")
parser.add_argument("-ac", "--action", help="turn the device on", required=True)
parser.add_argument("-db", "--debug",  help="if specified, debug output will be generated")
args = parser.parse_args()

cmd = "/usr/bin/node /etc/openhab2/scripts/node_modules/njstuya/njstuya.js -mode cloud -id " # 06200045dc4f2236e565 STATE

def getProcessOutput(cmd):
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()
    data, err = process.communicate()
    if process.returncode is 0:
        return data.strip()
    else:
        print ("Error:", err)

    return ""

action = args.action

requestedId = None
if args.id is not None:
    requestedId = args.id
else:
    requestedId = args.group
    if args.action == "STATUS":
      requestedId += "_1"

fullCommand = ""
if args.debug is not None:
    fullCommand = "DEBUG=* "

fullCommand += cmd + requestedId + " " + action
data = getProcessOutput(fullCommand)

print(data)
unparagoned commented 5 years ago

OK, I was a bit confused before I assumed you meant group as in an openhab group over multiple devices. But it looks like you mean a group as in a devices with multiple switches. I don't have any devices like that so support is very limited and hard to support, if you let me know what devices you have I'll see if I can buy it, and add proper support.