A Python module and CLI for controlling Broadlink devices locally. The following devices are supported:
Use pip3 to install the latest version of this module.
pip3 install broadlink
First, open Python 3 and import this module.
python3
import broadlink
Now let's try some functions...
In order to control the device, you need to connect it to your local network. If you have already configured the device with the Broadlink app, this step is not necessary.
broadlink.setup('myssid', 'mynetworkpass', 3)
Security mode options are (0 = none, 1 = WEP, 2 = WPA1, 3 = WPA2, 4 = WPA1/2)
You may need to specify a broadcast address if setup is not working.
broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255')
Use this function to discover devices:
devices = broadlink.discover()
You may need to specify local_ip_address
or discover_ip_address
if discovery does not return any devices.
Using the IP address of your local machine:
devices = broadlink.discover(local_ip_address='192.168.0.100')
Using the broadcast address of your subnet:
devices = broadlink.discover(discover_ip_address='192.168.0.255')
If the device is locked, it may not be discoverable with broadcast. In such cases, you can use the unicast version broadlink.hello()
for direct discovery:
device = broadlink.hello('192.168.0.16')
If you are a perfomance freak, use broadlink.xdiscover()
to create devices instantly:
for device in broadlink.xdiscover():
print(device) # Example action. Do whatever you want here.
After discovering the device, call the auth()
method to obtain the authentication key required for further communication:
device.auth()
The next steps depend on the type of device you want to control.
Learning IR codes takes place in three steps.
device.enter_learning()
packet = device.check_data()
Learning RF codes takes place in six steps.
device.sweep_frequency()
ok = device.check_frequency()
if ok:
print('Frequency found!')
device.find_rf_packet()
packet = device.check_data()
Universal remotes with product id 0x2712 use the same method for learning IR and RF codes. They don't need to sweep frequency. Just call device.enter_learning()
and device.check_data()
.
You can exit the learning mode in the middle of the process by calling this method:
device.cancel_sweep_frequency()
device.send_data(packet)
data = device.check_sensors()
device.set_power(True)
device.set_power(False)
state = device.check_power()
state = device.get_energy()
device.set_power(1, True) # Example socket. It could be 2 or 3.
device.set_power(1, False)
state = device.check_power()
state = device.get_state()
devices[0].set_state(pwr=0)
devices[0].set_state(pwr=1)
devices[0].set_state(brightness=75)
devices[0].set_state(bulb_colormode=0)
devices[0].set_state(blue=255)
devices[0].set_state(red=0)
devices[0].set_state(green=128)
devices[0].set_state(bulb_colormode=1)
data = device.check_sensors()
device.get_subdevices()
Use the DID obtained from get_subdevices() for the input parameter to query specific sub-device.
device.get_state(did="00000000000000000000a043b0d06963")
The parameters depend on the type of subdevice that is being controlled. In this example, we are controlling LC-1 switches:
device.set_state(did="00000000000000000000a043b0d0783a", pwr=1)
device.set_state(did="00000000000000000000a043b0d0783a", pwr1=1)
device.set_state(did="00000000000000000000a043b0d0783a", pwr2=1)
device.set_state(did="00000000000000000000a043b0d0783a", pwr=0)
device.set_state(did="00000000000000000000a043b0d0783a", pwr1=0)
device.set_state(did="00000000000000000000a043b0d0783a", pwr2=0)