codetheweb / homebridge-tuya-outlet

A plugin for Homebridge for Tuya-based outlets
15 stars 11 forks source link

Plug turns on but not off #1

Closed AALMA closed 6 years ago

AALMA commented 6 years ago

I got this working somewhat for two plugs but when I turn a plug on it immediately shows in Apple's Home app as off even though it is still on, then if I tap on it again it just sends an on command so it stays on. I did find if I double tap on it quickly it will turn it off.

Any idea what I'm missing here? Thanks for a cool project, excited to get my dirt cheap plugs working with homekit.

AALMA commented 6 years ago

I should add that it seems to work flawlessly with Siri voice commands, which is the way I'm probably going to use 99.9% of the time. I think there is just an issue with it not reporting back the current state correctly. Not sure if it's a bug in the plugin or an issue with the Tuya API.

codetheweb commented 6 years ago

Sorry about that, not sure what's going on. Try starting homebridge with DEBUG=* homebridge, and see if you can find anything in the output. If not, post it here.

I did just kind of throw this together, though, so I'm not that surprised it doesn't work 😛 .

pmeisel123 commented 6 years ago

Found a solution.
Apparently the bug is in tuyapi

on my machine I updated requests.json so the "off" "prefix" was the same as the "on" "prefix"

The file for me was located at: /usr/local/lib/node_modules/homebridge-tuya-outlet/node_modules/tuyapi/requests.json

codetheweb commented 6 years ago

@pmeisel123 huh, you shouldn't have to change it. Are you using this with a smart plug or a different type of device?

If you're able and willing, could you send me a Wireshark capture of traffic between your phone and device?

pmeisel123 commented 6 years ago

I'm about to run out the door, but let me give you what info I do have I don't have wireshark installed at the moment, I still have charles set up but I don't see the info you would want there.

I'm using a smart plug (Alexa-Enabled Ora MINISMARTPLUG ). I am also using the Jinvoo smart app which looks like it is just a wrapper for the TuyaSmart app (with different logins). The devices do also work with the Tuya app

Basically I spent a long time trying to figure out why it was failing. At one point I ripped out your _setOn code, and replaced it with the example code from tuyapi (which should just toggle the light switch). It would turn on but not off. I looked at the requests and saw the prefix where different (suffix the same), so decided to try it on a whim. Seems to mostly work. I am still getting some errors (below). I also tweaked the code to ignore them for now, and it seems to work:

{ Error: read ECONNRESET at exports._errnoException (util.js:1018:11) at TCP.onread (net.js:572:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

FYI, I've set up a bunch of homebridge devices before, but I have never really dove into the code behind it. I've also never really played around with nodejs before either so I am a bit of a newbie to this framework (usually I deal with browsers, webservers, and DBs).

codetheweb commented 6 years ago

Alright. If you could open a pull request with your fixed code, that would be great. The ECONNRESET error usually stems from a second connection to a device, as it only can handle one at a time (i.e. the app on your phone cannot be open the same time you're using this).

I'm also going to push an update to tuyapi soon that might fix this.

codetheweb commented 6 years ago

Hey guys, I fixed the bug in TuyAPI with some help from @jepsonrob. Update homebridge-tuya-outlet's dependencies and see if it works better.

pmeisel123 commented 6 years ago

Sorry, I didn't get back faster. I've been tinkering with things on my end as well. Kept thinking I had things working but then the code would randomly crash.

I'm trying to submit this change, but I've only ever used github to pull programs not push.

One of my changes is dealing with the error { Error: read ECONNRESET. I also have an alexa that can control my lights and I am guessing it is stealing the connection from homebridge (or it could be the multiple devices connecting to homebridge).

It's a pretty minor change. All I did was change the connect to per request instead of a constant connection. I also added a timeout for when the connection fails (it would completely freeze homebridge up). With these changes I was able to open the app that controls the lights, and homebridge would just say "No Response". Still get the occasional crash, but it happens less often.

index.js.txt

Also had to make this change

diff homebridge-tuya-outlet/node_modules/@codetheweb/recon/index.js~  homebridge-tuya-outlet/node_modules/@codetheweb/recon/index.js
79c79
<             else self.emit('error', err);
---
>             else self.emit('drop', err);

Oh ps, I was reading up on the tuya api. https://docs.tuya.com/en/cloudapi/appAPI/index.html . It looks like there should be a way to use the api to control the devices, however the documentation isn't great, and the error messages really lacked usefulness (nothing like error codes with no where to look up there meaning, or getting errors in Chinese)

pmeisel123 commented 6 years ago

Missed a change

diff index.js ~/homebridge-tuya-outlet/node_modules/tuyapi/index.js
38c38
<   this.client = recon(this.ip, this.port, {retryErrors: ['ECONNREFUSED', 'ECONNRESET']});
---
>   this.client = recon(this.ip, this.port, {retryErrors: []});
45a46
> 
76a78
> 
129c131
<     waitUntil(500, 40, () => {
---
>     waitUntil(500, 4, () => {
133c135
<         return reject(new Error('timeout'));
---
>         return resolve(new Error('timeout'));
143a146
> 
quetim commented 6 years ago

Hello Guys,

I’m having an issue. I got this cheap smart plug, got all the configuration info with Charles using the Tuya app, set it up on Homebridge and I’m able to see the status of the device (on or off). My problem is that when I try to switch the device (to the on position from off for example) it shows like it’s working but then after about 1 minute it just go back to the previous status (off) and shows an alert saying that this device is not responding. As you can imagine the device does not change status at all. I’ve changed the prefix to match the on and off (as suggested by @pmeisel123 ), as well as to Mach the on and off prefix with the status prefix with no luck (then I reverted them back to their original values). I’ve tried uninstalling the app, and nothing. Let’s see what else can I try when I get little bit more time.

I was also taking a look at Wireshark to try to capture the packages between my phone and the outlet and see if I can get the prefix of the on/off/status to make sure there is no difference between the values on the request.json file, but it’s getting late and I have lots of things to do tomorrow. I’ll see what I can do about it tomorrow.

pmeisel123 commented 6 years ago

@quetim my change to the prefix fixes the issue if turning the switch on works but off fails.
Make sure to close the Tuya app (don't even have it running in the background). If the app is running, then @codetheweb api can not control the device

quetim commented 6 years ago

Hello @pmeisel123, I’ve tried that (closing the application), i’ve also uninstalled the application in order to avoid it trying to contact the device and noting. It just seems it’s not working at all. I’ll try to capture some packages from my phone to the outlet to see if I can figure things out. One question. Which IP do you use? You use the local IP of the device or you use the remote IP that shows on the application itself? So far I’m only using the local IP, but tonight I’ll try to use the remote IP that shows on the app.

Thanks in advance?

pmeisel123 commented 6 years ago

@quetim the ip address should be the local ip address. The machine running the homebridge server must be able to access the ip address for the device

codetheweb commented 6 years ago

Hey guys, I'll try to take a look at this tomorrow. My setup sometimes has issues, but the majority of the time works flawlessly. It might just be a function that I updated in TuyAPI without updating it here. By the way, if your packages are fully updated, you shouldn't have to mess around at all with the prefixes.