Skip to content

Commit

Permalink
Improve the CLI (#555)
Browse files Browse the repository at this point in the history
* Add check humidity option

* Rename type to devtype

* Remove unnecessary try except clause

* Add commands to README.md
  • Loading branch information
felipediel authored Mar 14, 2021
1 parent 9ff6b2d commit 4e8d47a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 38 deletions.
113 changes: 83 additions & 30 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
Command line interface for python-broadlink
===========================================

This is a command line interface for broadlink python library

Tested with BroadLink RMPRO / RM2
This is a command line interface for the python-broadlink API.


Requirements
------------
You should have the broadlink python installed, this can be made in many linux distributions using :
You need to install the module first:
```
sudo pip install broadlink
pip3 install broadlink
```

Installation
-----------
Just copy this files
Download "broadlink_cli" and "broadlink_discovery".


Programs
--------
* broadlink_discovery: Discover Broadlink devices connected to the local network.


* broadlink_discovery
used to run the discovery in the network
this program withh show the command line parameters to be used with
broadlink_cli to select broadlink device

* broadlink_cli
used to send commands and query the broadlink device
* broadlink_cli: Send commands and query the Broadlink device.


device specification formats
Device specification formats
----------------------------

Using separate parameters for each information:
Expand All @@ -48,38 +40,99 @@ Using file with parameters:
```
broadlink_cli --device @BEDROOM.device --temp
```
This is prefered as the configuration is stored in file and you can change
just a file to point to a different hardware
This is prefered as the configuration is stored in a file and you can change
it later to point to a different device.

Sample usage
------------
Example usage
-------------

### Common commands

Learn commands :
#### Join device to the Wi-Fi network
```
broadlink_cli --joinwifi SSID PASSWORD
```

#### Discover devices connected to the local network
```
broadlink_discovery
```

### Universal remotes

#### Learn IR code and show at console
```
# Learn and save to file
broadlink_cli --device @BEDROOM.device --learnfile LG-TV.power
# LEard and show at console
broadlink_cli --device @BEDROOM.device --learn
```

#### Learn RF code and show at console
```
broadlink_cli --device @BEDROOM.device --rfscanlearn
```

#### Learn IR code and save to file
```
broadlink_cli --device @BEDROOM.device --learnfile LG-TV.power
```

#### Learn RF code and save to file
```
broadlink_cli --device @BEDROOM.device --rfscanlearn --learnfile LG-TV.power
```

Send command :
#### Send code
```
broadlink_cli --device @BEDROOM.device --send DATA
```

#### Send code from file
```
broadlink_cli --device @BEDROOM.device --send @LG-TV.power
broadlink_cli --device @BEDROOM.device --send ....datafromlearncommand...
```

Get Temperature :
#### Check temperature
```
broadlink_cli --device @BEDROOM.device --temperature
```

Get Energy Consumption (For a SmartPlug) :
#### Check humidity
```
broadlink_cli --device @BEDROOM.device --energy
broadlink_cli --device @BEDROOM.device --temperature
```

### Smart plugs

#### Turn on
```
broadlink_cli --device @BEDROOM.device --turnon
```

#### Turn off
```
broadlink_cli --device @BEDROOM.device --turnoff
```

Once joined to the Broadlink provisioning Wi-Fi, configure it with your Wi-Fi details:
#### Turn on nightlight
```
broadlink_cli --device @BEDROOM.device --turnnlon
```
broadlink_cli --joinwifi MySSID MyWifiPassword

#### Turn off nightlight
```
broadlink_cli --device @BEDROOM.device --turnnloff
```

#### Check power state
```
broadlink_cli --device @BEDROOM.device --check
```

#### Check nightlight state
```
broadlink_cli --device @BEDROOM.device --checknl
```

#### Check power consumption
```
broadlink_cli --device @BEDROOM.device --energy
```
15 changes: 7 additions & 8 deletions cli/broadlink_cli
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ parser.add_argument("--type", type=auto_int, default=0x2712, help="type of devic
parser.add_argument("--host", help="host address")
parser.add_argument("--mac", help="mac address (hex reverse), as used by python-broadlink library")
parser.add_argument("--temperature", action="store_true", help="request temperature from device")
parser.add_argument("--humidity", action="store_true", help="request humidity from device")
parser.add_argument("--energy", action="store_true", help="request energy consumption from device")
parser.add_argument("--check", action="store_true", help="check current power state")
parser.add_argument("--checknl", action="store_true", help="check current nightlight state")
Expand All @@ -92,16 +93,16 @@ args = parser.parse_args()

if args.device:
values = args.device.split()
type = int(values[0], 0)
devtype = int(values[0], 0)
host = values[1]
mac = bytearray.fromhex(values[2])
elif args.mac:
type = args.type
devtype = args.type
host = args.host
mac = bytearray.fromhex(args.mac)

if args.host or args.device:
dev = broadlink.gendevice(type, (host, 80), mac)
dev = broadlink.gendevice(devtype, (host, 80), mac)
dev.auth()

if args.joinwifi:
Expand All @@ -113,14 +114,12 @@ if args.convert:
print(format_durations(durations))
if args.temperature:
print(dev.check_temperature())
if args.humidity:
print(dev.check_humidity())
if args.energy:
print(dev.get_energy())
if args.sensors:
try:
data = dev.check_sensors()
except:
data = {}
data['temperature'] = dev.check_temperature()
data = dev.check_sensors()
for key in data:
print("{} {}".format(key, data[key]))
if args.send:
Expand Down

0 comments on commit 4e8d47a

Please # to comment.