codetheweb / homebridge-tuya-outlet

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

Error when trying to add a second accessory #4

Closed oscarcamachom closed 6 years ago

oscarcamachom commented 6 years ago

Hi,

I just installed this plugin to my homebridge running on macOS, I use the BrightFun app to control my Tuya Outlets and I took the localkey from the 1.abj file on the cache folder for the Android app. I have two outlets and both work with this plugin just fine, but only if I add one at a time, as soon as I place both on the config.json file I get the following error on the console when trying to start homebridge:

events.js:137
      throw er; // Unhandled 'error' event
      ^

Error: bind EADDRINUSE 0.0.0.0:6666
    at Object._errnoException (util.js:1003:13)
    at _exceptionWithHostPort (util.js:1024:20)
    at _handle.lookup (dgram.js:266:18)
    at process._tickCallback (internal/process/next_tick.js:152:19)
    at Function.Module.runMain (module.js:703:11)
    at startup (bootstrap_node.js:193:16)
    at bootstrap_node.js:617:3

This is the configuration I have on my config.json file (obfuscated devId and localKey):

{
    "bridge": {
        "name": "Homebridge",
        "username": "66:66:66:66:66:66",
        "pin": "666-66-666"
    },

    "description": "Oscar's HomeBridge",
    "accessories": [
        {
          "accessory": "TuyaOutlet",
          "name": "Extractor Closet",
          "devId": "00200836600666666666",
          "localKey": "400e1c6666666666"
        },
        {
          "accessory": "TuyaOutlet",
          "name": "Lampara Sala",
          "devId": "00200836600666666667",
          "localKey": "9357f06666666666"
        }
    ]
}

I'm new to homebridge so I hope I'm not screwing it, all my other devices work just fine.

codetheweb commented 6 years ago

Yeah, the issue is that adding two accessories with type TuyaOutlet results in TuyaOutlet being initialized twice. Then when resolveIds() (what translates the ID into an IP) is called on the second instance, it returns with an error because there's already a listener on port 6666 added by the first instance. I'll have to rework the plugin so that it uses Homebridge's Platform API rather than the one-off Accessory, which isn't exactly straightforward.
I could add a temporary fix that will allow your configuration to work if you pass in IPs for both devices.

oscarcamachom commented 6 years ago

Thanks for the quick reply! The temporary fix you mention would work just fine for me, I have all my home with reserved DHCP, and a lot of other plugins requires the IP of the devices anyway.

By the way, is there anyway to also control the USB port of the outlets with this feature rather than only the regular power socket? The app I'm using has this feature, I could share you a packet capture from when I use that action.

codetheweb commented 6 years ago

Actually, I guess I already added the ablility to pass in static IPs. 🙄 Just modify your configuration so it looks like this:

{
    "bridge": {
        "name": "Homebridge",
        "username": "66:66:66:66:66:66",
        "pin": "666-66-666"
    },

    "description": "Oscar's HomeBridge",
    "accessories": [
        {
          "accessory": "TuyaOutlet",
          "name": "Extractor Closet",
          "ip": "192.168.0.xxx",
          "devId": "00200836600666666666",
          "localKey": "400e1c6666666666"
        },
        {
          "accessory": "TuyaOutlet",
          "name": "Lampara Sala",
          "ip": "192.168.0.xxx",
          "devId": "00200836600666666667",
          "localKey": "9357f06666666666"
        }
    ]
}

By the way, is there anyway to also control the USB port of the outlets with this feature rather than only the regular power socket?

TuyAPI, the lower-level package that this project uses, has support for that. I'll think about adding it in a future release.

codetheweb commented 6 years ago

Didn't mean to close this.

awakenrz commented 6 years ago

I have a PR https://github.com/codetheweb/homebridge-tuya-outlet/pull/7.

codetheweb commented 6 years ago

Hey @oscarc23 I've published a new package that fixes many issues, including making Homebridge crash if your device isn't plugged in. It also includes an easier setup process.

Please move to homebridge-tuya instead of continuing to use this package.