aarons22 / homebridge-bond

Homebridge plugin for Bond
MIT License
63 stars 31 forks source link

404 and TypeError during startup from request to devices/__ #213

Closed diffness closed 2 years ago

diffness commented 2 years ago

Describe the bug When homebridge-bond is starting up, it makes a request to devices/__, and the following errors occur:

Request (18607b96f1c18000) [get http://<ip>/v2/devices/__]
A request error occurred: [status] 404 [statusText] Not Found
Error getting devices: TypeError: Cannot set properties of undefined (setting 'id')

As far as I can tell, this doesn't seem to affect the functionality of existing devices (my ceiling fan works in HomeKit), but as pointed out by @XueningXu it prevents new devices from being added.

Making the same request manually via curl also 404s:

$ curl -i http://<ip>/v2/devices/__ -H "BOND-Token: <token>"
HTTP/1.1 404 Not Found
BOND-Flags: 4
Content-Length: 0

I think this is related to the v3 firmware "local hash" (although I'm 99% sure I was getting a 404 error even before upgrading to v3, but don't have the logs to prove it).

The solution is hopefully as simple as adjusting the filter in getDeviceIds and getCommandIds from BondApi.ts to remove both _ and __; something along the lines of:

  public getDeviceIds(): Promise<string[]> {
    const req = this.request(HTTPMethod.GET, this.uri.deviceIds());
    return req.then(json =>
      Object.keys(json).filter(x => {
        // Ignore anything that is an empty string or '_'
-        return x.length > 0 && x !== '_';
+        return x.length > 0 && !/^_{1,2}$/.test(x);
      }),
    );
  }

Information (please complete the following information):

Logs

Homebridge debug mode logs: ``` [11/07/2022, 13:43:08] Loaded plugin: homebridge-bond@3.2.9 [11/07/2022, 13:43:08] Registering platform 'homebridge-bond.Bond' [11/07/2022, 13:43:15] [Bond] Initializing Bond platform... [11/07/2022, 13:43:15] [Bond] Initializing child bridge 0E:0F:69:2E:38:AA [11/07/2022, 13:43:19] [homebridge-bond] Launched child bridge with PID 25521 [11/07/2022, 13:43:20] Registering platform 'homebridge-bond.Bond' [11/07/2022, 13:43:20] [homebridge-bond] Loaded homebridge-bond v3.2.9 child bridge successfully [11/07/2022, 13:43:20] [homebridge-bond] Config: { "bonds": [ { "ip_address": "", "token": "" } ], "include_dimmer": false, "include_toggle_state": false, "fan_speed_values": false, "platform": "Bond" } [11/07/2022, 13:43:20] Publishing bridge accessory (name: homebridge-bond, publishInfo: {{ username: '', port: , pincode: '***-**-***', category: 2, bind: undefined, mdns: undefined, addIdentifyingMaterial: true, advertiser: 'avahi' }). [11/07/2022, 13:43:20] [homebridge-bond] Request (18607b96f17e4000) [get http:///v2/devices] [11/07/2022, 13:43:20] [homebridge-bond] Request (18607b96f1814000) [get http:///v2/sys/version] [11/07/2022, 13:43:20] Homebridge v1.5.0 (HAP v0.10.2) (homebridge-bond) is running on port 48314. [11/07/2022, 13:43:20] [homebridge-bond] Response (18607b96f17e4000) [get http:///v2/devices] - {"_":"e2331cd9","__":"00000000","2825a63b":{"_":"312fd678"}} [11/07/2022, 13:43:21] [homebridge-bond] Response (18607b96f1814000) [get http:///v2/sys/version] - {"target":"zermatt","fw_ver":"v3.3.7-beta","fw_date":"Fri Jul 8 17:33:49 UTC 2022","uptime_s":253289,"boot_patch":"not first boot","make":"Olibra","model":"BD-1000","branding_profile":"OLIBRA_BD1000","bondid":"","upgrade_http":true,"api":2,"_":"1f2d6050","__":"1f2d6050"} [11/07/2022, 13:43:21] [homebridge-bond] ****** Bond Info ******* bondId: FW: v3.3.7-beta API: v2 Make: Olibra Model: BD-1000 ************************ [11/07/2022, 13:43:21] [homebridge-bond] 1 cached accessories were loaded [11/07/2022, 13:43:21] [homebridge-bond] Getting devices for this Bond ()... [11/07/2022, 13:43:21] [homebridge-bond] 2 devices were found on this Bond (). [11/07/2022, 13:43:21] [homebridge-bond] Attempting to add 1 devices that were not previously added. [11/07/2022, 13:43:21] [homebridge-bond] Request (18607b96f1c18000) [get http:///v2/devices/__] [11/07/2022, 13:43:21] [homebridge-bond] UDP message sent to :30007 [11/07/2022, 13:43:21] [homebridge-bond] A request error occurred: [status] 404 [statusText] Not Found [11/07/2022, 13:43:21] [homebridge-bond] Error getting devices: TypeError: Cannot set properties of undefined (setting 'id') [11/07/2022, 13:43:21] [homebridge-bond] UDP Message received from :30007 - {"B":"","d":0,"v":"v3.3.7-beta"} [11/07/2022, 13:43:21] [homebridge-bond] Configuring Accessory: Bedroom Ceiling Fan [11/07/2022, 13:43:21] [homebridge-bond] [Ceiling Fan] actions: OEMRandomToggle,SetSpeed,OEMTimer,StartDimmer,TurnOff,ToggleLight,Stop,TogglePower,TurnOn,IncreaseSpeed,DecreaseSpeed,TurnLightOn,TurnLightOff [11/07/2022, 13:43:21] [homebridge-bond] Request (18607b96f2268000) [get http:///v2/devices/2825a63b/state] [11/07/2022, 13:43:21] [homebridge-bond] Response (18607b96f2268000) [get http:///v2/devices/2825a63b/state] - {"power":0,"speed":1,"light":0,"_":"86b7414a","__":"86b7414a"} [11/07/2022, 13:44:21] [homebridge-bond] UDP message sent to :30007 [11/07/2022, 13:44:21] [homebridge-bond] UDP Message received from :30007 - {"B":"","d":0,"v":"v3.3.7-beta"} ```
XueningXu commented 2 years ago

Thank you so much for the timely solution !!! :grinning: I just got my Bond today and I spent hours but cannot successfully configure it on Homebridge due to this error.

By the way, the same change is also needed in getCommandIds from BondApi.ts to make this plugin fully functional.

 private getCommandIds(id: string): Promise<string[]> {
    const req = this.request(HTTPMethod.GET, this.uri.commands(id));
    return req.then(json =>
      Object.keys(json).filter(x => {
        // Ignore anything that is an empty string or '_'
-        return x.length > 0 && x !== '_';  
+        return x.length > 0 && !/^_{1,2}$/.test(x); 
      }),
    );
  }
diffness commented 2 years ago

Thanks @XueningXu, good catch. I’ve created a PR that hopefully @aarons22 will be able to merge soon.

cwfeldmann commented 2 years ago

The replacement of the line fixed the issue for me as well - Thanks y'all!

I did have some confusion finding the file. On my Mac running Homebridge the file was titled BondApi.js - not .ts as stated in earlier posts. You will need to turn off hidden files to find it in _/usr/local/lib/nodemodules/homebridge-bond/dist/BondApi.js

I performed a find and replace function with the red and green lines and that worked out great.