1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-14 03:20:14 +01:00
python-broadlink/README.md

248 lines
6.1 KiB
Markdown
Raw Normal View History

2021-10-17 14:20:33 +02:00
# python-broadlink
2016-09-15 17:06:26 +02:00
2021-10-17 14:20:33 +02:00
A Python module and CLI for controlling Broadlink devices locally. The following devices are supported:
- **Universal remotes**: RM home, RM mini 3, RM plus, RM pro, RM pro+, RM4 mini, RM4 pro, RM4C mini, RM4S, RM4 TV mate
2021-10-17 14:20:33 +02:00
- **Smart plugs**: SP mini, SP mini 3, SP mini+, SP1, SP2, SP2-BR, SP2-CL, SP2-IN, SP2-UK, SP3, SP3-EU, SP3S-EU, SP3S-US, SP4L-AU, SP4L-EU, SP4L-UK, SP4M, SP4M-US, Ankuoo NEO, Ankuoo NEO PRO, Efergy Ego, BG AHC/U-01
- **Switches**: MCB1, SC1, SCB1E, SCB2
- **Outlets**: BG 800, BG 900
2021-03-11 11:36:20 +01:00
- **Power strips**: MP1-1K3S2U, MP1-1K4S, MP2
- **Environment sensors**: A1
2021-10-17 14:20:33 +02:00
- **Alarm kits**: S1C, S2KIT
- **Light bulbs**: LB1, LB26 R1, LB27 R1, SB800TD
2021-03-11 11:36:20 +01:00
- **Curtain motors**: Dooya DT360E-45/20
- **Thermostats**: Hysen HY02B05H
- **Hubs**: S3
2021-10-17 14:20:33 +02:00
## Installation
2016-09-15 17:06:26 +02:00
2021-10-17 14:20:33 +02:00
Use pip3 to install the latest version of this module.
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
pip3 install broadlink
```
2021-10-17 14:20:33 +02:00
## Basic functions
First, open Python 3 and import this module.
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
python3
```
```python3
2016-09-15 17:06:26 +02:00
import broadlink
```
2021-10-17 14:20:33 +02:00
Now let's try some functions...
2021-10-17 14:20:33 +02:00
### Setup
2021-10-17 14:20:33 +02:00
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.
2021-10-17 14:20:33 +02:00
1. Put the device into AP Mode.
- Long press the reset button until the blue LED is blinking quickly.
- Long press again until blue LED is blinking slowly.
- Manually connect to the WiFi SSID named BroadlinkProv.
2. Connect the device to your local network with the setup function.
```python3
broadlink.setup('myssid', 'mynetworkpass', 3)
```
2021-10-17 14:20:33 +02:00
Security mode options are (0 = none, 1 = WEP, 2 = WPA1, 3 = WPA2, 4 = WPA1/2)
#### Advanced options
You may need to specify a broadcast address if setup is not working.
```python3
broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255')
```
2021-10-17 14:20:33 +02:00
### Discovery
Use this function to discover devices:
```python3
devices = broadlink.discover()
```
2021-10-17 14:20:33 +02:00
#### Advanced options
You may need to specify `local_ip_address` or `discover_ip_address` if discovery does not return any devices.
2021-10-18 19:05:39 +02:00
Using the IP address of your local machine:
2021-10-17 14:20:33 +02:00
```python3
2021-10-18 19:05:39 +02:00
devices = broadlink.discover(local_ip_address='192.168.0.100')
```
2021-10-18 19:05:39 +02:00
Using the broadcast address of your subnet:
2021-10-17 14:20:33 +02:00
```python3
2021-10-18 19:05:39 +02:00
devices = broadlink.discover(discover_ip_address='192.168.0.255')
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
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:
```python3
2021-10-18 19:05:39 +02:00
device = broadlink.hello('192.168.0.16')
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
If you are a perfomance freak, use `broadlink.xdiscover()` to create devices instantly:
```python3
for device in broadlink.xdiscover():
print(device) # Example action. Do whatever you want here.
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
### Authentication
After discovering the device, call the `auth()` method to obtain the authentication key required for further communication:
```python3
device.auth()
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
The next steps depend on the type of device you want to control.
## Universal remotes
### Learning IR codes
Learning IR codes takes place in three steps.
1. Enter learning mode:
```python3
device.enter_learning()
2017-11-25 21:38:57 +01:00
```
2021-10-17 14:20:33 +02:00
2. When the LED blinks, point the remote at the Broadlink device and press the button you want to learn.
3. Get the IR packet.
```python3
packet = device.check_data()
2017-11-25 21:38:57 +01:00
```
2021-10-17 14:20:33 +02:00
### Learning RF codes
Learning RF codes takes place in six steps.
2021-10-17 14:20:33 +02:00
1. Sweep the frequency:
```python3
device.sweep_frequency()
```
2021-10-17 14:20:33 +02:00
2. When the LED blinks, point the remote at the Broadlink device for the first time and long press the button you want to learn.
3. Check if the frequency was successfully identified:
```python3
ok = device.check_frequency()
if ok:
print('Frequency found!')
```
4. Enter learning mode:
2021-10-17 14:20:33 +02:00
```python3
device.find_rf_packet()
2017-11-25 21:38:57 +01:00
```
5. When the LED blinks, point the remote at the Broadlink device for the second time and short press the button you want to learn.
6. Get the RF packet:
2021-10-17 14:20:33 +02:00
```python3
packet = device.check_data()
2017-11-25 21:38:57 +01:00
```
#### Notes
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()`.
2021-10-17 14:20:33 +02:00
### Canceling learning
2017-11-25 21:38:57 +01:00
2021-10-17 14:20:33 +02:00
You can exit the learning mode in the middle of the process by calling this method:
```python3
device.cancel_sweep_frequency()
2016-09-15 17:06:26 +02:00
```
2021-10-17 14:20:33 +02:00
### Sending IR/RF packets
```python3
device.send_data(packet)
```
2021-10-17 14:20:33 +02:00
### Fetching sensor data
```python3
data = device.check_sensors()
```
2021-10-17 14:20:33 +02:00
## Switches
2021-10-17 14:20:33 +02:00
### Setting power state
```python3
device.set_power(True)
device.set_power(False)
```
2021-10-17 14:20:33 +02:00
### Checking power state
```python3
state = device.check_power()
```
2021-10-17 14:20:33 +02:00
### Checking energy consumption
```python3
state = device.get_energy()
2018-04-19 22:45:50 +02:00
```
2021-10-17 14:20:33 +02:00
## Power strips
2021-10-17 14:20:33 +02:00
### Setting power state
```python3
device.set_power(1, True) # Example socket. It could be 2 or 3.
device.set_power(1, False)
```
2021-10-17 14:20:33 +02:00
### Checking power state
```python3
state = device.check_power()
```
2021-10-17 14:20:33 +02:00
## Light bulbs
### Fetching data
```python3
state = device.get_state()
```
2021-10-17 14:20:33 +02:00
### Setting state attributes
```python3
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)
```
2021-10-17 14:20:33 +02:00
## Environment sensors
### Fetching sensor data
```python3
data = device.check_sensors()
```
## Hubs
### Discovering subdevices
```python3
device.get_subdevices()
```
### Fetching data
Use the DID obtained from get_subdevices() for the input parameter to query specific sub-device.
```python3
device.get_state(did="00000000000000000000a043b0d06963")
```
### Setting state attributes
The parameters depend on the type of subdevice that is being controlled. In this example, we are controlling LC-1 switches:
#### Turn on
```python3
device.set_state(did="00000000000000000000a043b0d0783a", pwr=1)
device.set_state(did="00000000000000000000a043b0d0783a", pwr1=1)
device.set_state(did="00000000000000000000a043b0d0783a", pwr2=1)
```
#### Turn off
```python3
device.set_state(did="00000000000000000000a043b0d0783a", pwr=0)
device.set_state(did="00000000000000000000a043b0d0783a", pwr1=0)
device.set_state(did="00000000000000000000a043b0d0783a", pwr2=0)
```