davorf / BlackBeanControl

BlackBeanControl - Broadlink RM 3 Mini (aka Black Bean) control script
230 stars 55 forks source link

Cannot send a pre-learned command on one device to another device #9

Closed walkention closed 7 years ago

walkention commented 7 years ago

It seems as though if I have learned a command on one device it can't be sent to another device. It doesn't put the other device in learning mode and no error is thrown.

If I use the python-broadlink package directly I can learn a command on one and send it to both devices.

Here's the sample code I'm using to learn a command using python-broadlink:

#!/usr/bin/python

import broadlink
import time

print "discover"
devices = broadlink.discover(timeout=5)
device = devices[1]
device.auth()
print "enter_learning"
device.enter_learning()
print "sleep"
time.sleep(5)
print "check data"
ir_packet = device.check_data()
print str(ir_packet).encode('hex')

I'm taking the output from a learned command and putting it into this script to send it to both devices:

#!/usr/bin/python

import broadlink
import time

hex_ir_packet = "260070004f13141327141314141328131314141328131314131414131500035f4e14141328121513131428131314131427141413131414131400035e4f14141328131413141328131413141327141413141315121500035f4e141413271315131413281314131413271414131314141314000d050000000000000000"
ir_packet = hex_ir_packet.decode('hex')

print "discover"
devices = broadlink.discover(timeout=5)
device = devices[0]
device.auth()
print "Send command to device 1"
device.send_data(ir_packet)
print "wait"
time.sleep(2)
device = devices[1]
device.auth()
print "Send command to device 2"
device.send_data(ir_packet)

It looks as though in your package the learned command has four bytes of zero padding added, is decrypted using the device auth key, and then stored in the BlackBeanControl.ini file. Perhaps the decrypting the packet with the device key makes it unable to be re-encrypted with a different device key.

This functionality may be by design to prevent a command learned on once device to be sent to another, but it might be nice to broadcast the same command to a different device/room by learning it once on another device.

I forked this packaged and was going to try and rewrite the code for learning and sending commands. I would try to write the commands as encoded hex and make a way to migrate codes from the old to the new format so they don't have to be re-learned again.

Let me know what you think.

Thanks for building this package! It's working great with my Alexa integration code that I am planning on releasing here soon! :-)

davorf commented 7 years ago

Hello!

Since I own only one RM3 Mini, I wasn't able to test it properly. Regardless, I don't think encryption has anything to do with it, since the encryption Key and IV are hard-coded inside broadlink-python package. They are the same for every device. I'll take a look at this, probably tonight, and report back my findings.

Best regards, Davor

walkention commented 7 years ago

I had thought that too until I started poking around in the python-broadlink code. In that code it looks like there is an initial key used for auth and then that key is reset from the payload data returned in auth() here. That data then overwrites the self.key on line 190. If each device returns a device specific key after auth() that would impact using the saved data on another device from the BlackBeanControl.ini.

In this issue it is discussed how the data returned seems to be the raw IR data with some header info attached. It might be helpful to have the raw IR data stored in the BlackBeanControl.ini so folks could pull shared codes online without having to learn them, assuming the header info can be figured out.

I re-wrote how codes were stored in your script to save the IR data in raw form and created a re-key utility to convert commands to raw storage. It works with my two RM3's pretty well. Let me know if you want me to issue a pull request for this update.

Thanks!

davorf commented 7 years ago

Hello!

You can issue a pull request. I'll just review your naming and README.md (for consistency) and remove any commented parts of the code. Thank you for the contribution.

Best regards, Davor

davorf commented 7 years ago

Hello!

Thank you for your contribution once again. I'll review and accept your pull request as soon as I can - probably tonight, or tomorrow.

Best regards, Davor

walkention commented 7 years ago

Davor,

No rush. Thanks for reviewing it and giving your feedback! If you want to make any changes or additions or want to discuss anything just let me know.

Thanks! Aydaen

davorf commented 7 years ago

Hello!

I'm not sure if I merged this correctly (haven't used git as source control before, especially merging pull requests part), but I think it should work. I've made another pull request by mistake, so, I would really appreciate if you could check if the code works as it should.

Best regards, Davor

walkention commented 7 years ago

Hey!

I just pulled down the latest code in your master branch and everything works that was in the pull request! I think it's set! Thanks for processing that pull request and all the work you did on this project!

Aydaen