AMoo-Miki / homebridge-tuya-lan

Homebridge plugin for IoT devices that use Tuya Smart's platform
MIT License
204 stars 52 forks source link

Devices are connected but unresponsive until first toggled inside the Tuya app #130

Closed digitalec closed 4 years ago

digitalec commented 4 years ago

I managed to get homebridge + homebridge-tuya-lan working across two vlans but for some reason I no longer see "Sending first query..." and "Ready to handle..." after it connects to one of the devices. In the Home app, all devices show "No response". If I open the Tuya app and toggle a light, I then see the "Read to handle..." message and the device in the Home app comes to life instantly. I can use all the devices inside the Home app without issue from this point on so long as I don't have to restart homebridge.

My question is, what happens between "Connected to..." and "Sending first query..." that would prevent it from ever sending the first query? I know it's related to my setup but I'm just not sure where to start. Does it wait after it connects for a ping or some other response before it continues to send the first query and ready the device?

$ journalctl -f -u homebridge

Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Starting discovery...
Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] Homebridge is running on port 51826.
Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaDiscovery] Discovery started on port 6666.                                                                                                                                                Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaDiscovery] Discovery started on port 6667.                                                                                                                                                Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaDiscovery] UDP from 10.0.40.35:6667 0x000055aa...0x0000aa55                                                                                                                               Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Discovered Living Room Overhead 2 (76670400dc4f2271c443) identified as RGBTWLight (3.3)
Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaAccessory] Changing ping gap for Living Room Overhead 2 to 9s
Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Connected to Living Room Overhead 2                                                                                                                         Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaDiscovery] UDP from 10.0.40.10:6666 0x000055aa...0x0000aa55
Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Discovered Overhead Light (002004245ccf7f43e31f) identified as TWLight (3.1)
Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Connected to Overhead Light                                                                                                                                 Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaDiscovery] UDP from 10.0.40.41:6667 0x000055aa...0x0000aa55                                                                                                                               Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Discovered Kitchen Overhead Light 3 (821043242cf4323b16d8) identified as RGBTWLight (3.3)
Dec 14 02:20:30 homebridge homebridge[10316]: [TuyaAccessory] Changing ping gap for Kitchen Overhead Light 3 to 9s
Dec 14 02:20:30 homebridge homebridge[10316]: [12/14/2019, 2:20:30 AM] [TuyaLan] Connected to Kitchen Overhead Light 3                                                                                                                       Dec 14 02:20:31 homebridge homebridge[10316]: [TuyaDiscovery] UDP from 10.0.40.31:6666 0x000055aa...0x0000aa55

$ journalctl -f -u homebridge (after toggling a few lights in Tuya)

Dec 14 02:30:33 homebridge homebridge[10316]: [12/14/2019, 2:30:33 AM] [TuyaLan] Ready to handle Office Lamp (Outlet:3.1) with signature {"1":false,"2":0}
Dec 14 02:30:51 homebridge homebridge[10316]: [TuyaAccessory] Heard back from Coffee Bar Lamp with command 8
Dec 14 02:30:51 homebridge homebridge[10316]: [12/14/2019, 2:30:51 AM] [TuyaLan] Ready to handle Coffee Bar Lamp (RGBTWLight:3.3) with signature {"20":true}
Dec 14 02:30:55 homebridge homebridge[10316]: [TuyaAccessory] Heard back from Kitchen Overhead Light 3 with command 8
Dec 14 02:30:55 homebridge homebridge[10316]: [12/14/2019, 2:30:55 AM] [TuyaLan] Ready to handle Kitchen Overhead Light 3 (RGBTWLight:3.3) with signature {"1":true}
Dec 14 02:30:55 homebridge homebridge[10316]: [TuyaAccessory] Color format for Kitchen Overhead Light 3 (3.3) is undetectable.
Dec 14 02:31:03 homebridge homebridge[10316]: [TuyaAccessory] Heard back from Kitchen Overhead Light 3 with command 8
Dec 14 02:31:07 homebridge homebridge[10316]: [TuyaAccessory] Heard back from Coffee Bar Lamp with command 8
Dec 14 02:31:22 homebridge homebridge[10316]: [TuyaAccessory] Heard back from Kitchen Overhead Light 3 with command 8

Network setup: vlan10 - 10.0.10.0 - wireless router vlan20 - 10.0.20.0 - homebridge server, Apple TV, iPhone vlan40 - 10.0.40.0 - homebridge server, Tuya IoT devices

The homebridge server has two interfaces, one connecting to vlan20 and another to vlan40. I have my router (rt-ac68u) setup with an avahi-reflector which I'm not sure is necessary since the server is available on the same vlan as the ATV.

digitalec commented 4 years ago

I was able to narrow it down to this block of code:

https://github.com/AMoo-Miki/homebridge-tuya-lan/blob/f1b4fa9c653c49c4535c4a9af690f481df11f553/lib/TuyaAccessory.js#L200-L205

@AMoo-Miki - is this essentially waiting endlessly for a ping response (cmd 9)?

digitalec commented 4 years ago

After playing around with this for a few days, I was able to (somewhat) get around my “cmd 9” problem by adding an “intro”: false entry to each of my devices (couldn’t find any reference in the code to a context.intro except when it’s checked prior to the code above). I now see “Sending first query” and “Ready to handle” messages but the plugin seems to get ping timeouts on everything soon after. I’m able to ping all devices from the server when this happens so I’m curious if the timeout is too short or if it “forgets” where they are (I have set static IPs on all the Tuya stuff to keep the troubleshooting consistent).

Couple of questions...

  1. What is the “intro” property for?
  2. I’m getting a ton of “UDP message from...” in the logs that I hadn’t gotten in the past. Is this new?
digitalec commented 4 years ago

I'm still curious what some of these status messages mean but I'm going to close this issue.

For anyone else looking to use this on a VLAN, it can be done and it's quite easy once you've got the VLANs setup. Put all your IoT devices and your Homebridge server on one dedicated VLAN (I have a dedicated Homebridge server running as a headless VM). I also enabled the avahi-reflector on my router to allow HomeKit to see the bridge.

My problems stemmed from the fact that I had the server on two VLANs which worked briefly after toggling devices from the Tuya app but would inevitably disconnect. This plugin couldn't find it automatically and would fallback to my static IP in the config even with routing setup on the server.