Consistenly assign Broadlink.devices keys with macAddress.toString('hex') #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Cameron,
I'm a heavy user of homebridge-broadlink-rm, with 4 of those devices at home, and I had been struggling with auto device discoverability for a while. Your library always found 3 of my 4 devices, and not necessarily the same 3. Initially, I thought it was the broadlink hardware entering locked mode, but when using python-broadlink I always managed to find the 4 of them. So, I finally spent some time today to dig into it, and it seems that I found out the root cause.
Essentially, there is an expectation on the
onMessage()
handler that the key forBroadlink.devices
will be generated asmacAddress.toString('hex')
, but during theaddDevice()
method we directly assign the buffer value as the key:this.devices[macAddress] = device
That, in itself, seems innocent enough. The check to prevent re-adding existing devices is wasted, since
macAddress.toString('hex') != String(macAddress)
, but the resulting Device object is the same.Now, here's the interesting part,
String(macAddress)
can return the same string, for different buffer values. And that's exactly what was happening in my case. I had 2 devices, with 2 different mac addresses, that resulted in the same value when stringified. This script demonstrates the bug:The PR simply ensures that broadlink.devices are assigned with
macAddress.toString('hex')
. That ensures the serialization is safe, and also prevents from re-adding devices that were already discovered. I've been trying this fix on my homebridge installation and has been working like a charm so far. Would be very grateful if you could merge it in, and include it in a future release ofhomebridge-broadlink-rm
. Let me know if you need any modifications on the PR, add tests, etc.Thank you very much for your software!