From d45c9d0850554620157f5c5fb775c61272edf3f2 Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel <41558831+felipediel@users.noreply.github.com> Date: Sun, 14 Mar 2021 12:20:25 -0300 Subject: [PATCH] Improve the CLI (#555) * Add check humidity option * Rename type to devtype * Remove unnecessary try except clause * Add commands to README.md --- cli/README.md | 119 +++++++++++++++++++++++++++++++++------------- cli/broadlink_cli | 15 +++--- 2 files changed, 93 insertions(+), 41 deletions(-) diff --git a/cli/README.md b/cli/README.md index 7e229e3..7b40b4c 100644 --- a/cli/README.md +++ b/cli/README.md @@ -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_cli: Send commands and query the Broadlink device. -* 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 - - -device specification formats +Device specification formats ---------------------------- Using separate parameters for each information: @@ -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 +------------- -Learn commands : +### Common 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 +``` -Send command : +#### 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 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 --temperature +``` + +### Smart plugs + +#### Turn on +``` +broadlink_cli --device @BEDROOM.device --turnon +``` + +#### Turn off +``` +broadlink_cli --device @BEDROOM.device --turnoff +``` + +#### Turn on nightlight +``` +broadlink_cli --device @BEDROOM.device --turnnlon +``` + +#### 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 ``` - -Once joined to the Broadlink provisioning Wi-Fi, configure it with your Wi-Fi details: -``` -broadlink_cli --joinwifi MySSID MyWifiPassword -``` diff --git a/cli/broadlink_cli b/cli/broadlink_cli index 31bbb2c..36a83e1 100755 --- a/cli/broadlink_cli +++ b/cli/broadlink_cli @@ -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") @@ -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: @@ -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: