From e2ab21f502ca85d2c078bb48cc0f06c8f75d3fc2 Mon Sep 17 00:00:00 2001 From: Dan Van Derveer Date: Fri, 2 Oct 2026 08:09:28 -0400 Subject: [PATCH 1/2] Provide CLI tools as console_scripts entry points Move broadlink_cli and broadlink_discovery into broadlink.cli with main() functions so they install via pip or uv tool install. The scripts in cli/ remain as thin wrappers. --- broadlink/cli/__init__.py | 0 broadlink/cli/broadlink_cli.py | 225 +++++++++++++++++++++++++++ broadlink/cli/broadlink_discovery.py | 35 +++++ cli/README.md | 13 +- cli/broadlink_cli | 222 +------------------------- cli/broadlink_discovery | 31 +--- setup.py | 7 +- 7 files changed, 281 insertions(+), 252 deletions(-) create mode 100644 broadlink/cli/__init__.py create mode 100644 broadlink/cli/broadlink_cli.py create mode 100644 broadlink/cli/broadlink_discovery.py diff --git a/broadlink/cli/__init__.py b/broadlink/cli/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/broadlink/cli/broadlink_cli.py b/broadlink/cli/broadlink_cli.py new file mode 100644 index 00000000..740f170b --- /dev/null +++ b/broadlink/cli/broadlink_cli.py @@ -0,0 +1,225 @@ +import argparse +import base64 +import time +from typing import List + +import broadlink +from broadlink.const import DEFAULT_PORT +from broadlink.exceptions import ReadError, StorageError +from broadlink.remote import data_to_pulses, pulses_to_data + +TIMEOUT = 30 + + +def auto_int(x): + return int(x, 0) + + +def format_pulses(pulses: List[int]) -> str: + """Format pulses.""" + return " ".join( + f"+{pulse}" if i % 2 == 0 else f"-{pulse}" + for i, pulse in enumerate(pulses) + ) + + +def parse_pulses(data: List[str]) -> List[int]: + """Parse pulses.""" + return [abs(int(s)) for s in data] + + +def main(): + parser = argparse.ArgumentParser(fromfile_prefix_chars='@') + parser.add_argument("--device", help="device definition as 'type host mac'") + parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device") + 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") + parser.add_argument("--turnon", action="store_true", help="turn on device") + parser.add_argument("--turnoff", action="store_true", help="turn off device") + parser.add_argument("--turnnlon", action="store_true", help="turn on nightlight on the device") + parser.add_argument("--turnnloff", action="store_true", help="turn off nightlight on the device") + parser.add_argument("--switch", action="store_true", help="switch state from on to off and off to on") + parser.add_argument("--send", action="store_true", help="send command") + parser.add_argument("--sensors", action="store_true", help="check all sensors") + parser.add_argument("--learn", action="store_true", help="learn command") + parser.add_argument("--rflearn", action="store_true", help="rf scan learning") + parser.add_argument("--frequency", type=float, help="specify radiofrequency for learning") + parser.add_argument("--learnfile", help="save learned command to a specified file") + parser.add_argument("--durations", action="store_true", + help="use durations in micro seconds instead of the Broadlink format") + parser.add_argument("--convert", action="store_true", help="convert input data to durations") + parser.add_argument("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with") + parser.add_argument("data", nargs='*', help="Data to send or convert") + args = parser.parse_args() + + if args.device: + values = args.device.split() + devtype = int(values[0], 0) + host = values[1] + mac = bytearray.fromhex(values[2]) + elif args.mac: + devtype = args.type + host = args.host + mac = bytearray.fromhex(args.mac) + + if args.host or args.device: + dev = broadlink.gendevice(devtype, (host, DEFAULT_PORT), mac) + dev.auth() + + if args.joinwifi: + broadlink.setup(args.joinwifi[0], args.joinwifi[1], 4) + + if args.convert: + data = bytearray.fromhex(''.join(args.data)) + pulses = data_to_pulses(data) + print(format_pulses(pulses)) + if args.temperature: + print(dev.check_temperature()) + if args.humidity: + print(dev.check_humidity()) + if args.energy: + print(dev.get_energy()) + if args.sensors: + data = dev.check_sensors() + for key in data: + print("{} {}".format(key, data[key])) + if args.send: + data = ( + pulses_to_data(parse_pulses(args.data)) + if args.durations + else bytes.fromhex(''.join(args.data)) + ) + dev.send_data(data) + if args.learn or (args.learnfile and not args.rflearn): + dev.enter_learning() + print("Learning...") + start = time.time() + while time.time() - start < TIMEOUT: + time.sleep(1) + try: + data = dev.check_data() + except (ReadError, StorageError): + continue + else: + break + else: + print("No data received...") + exit(1) + + print("Packet found!") + raw_fmt = data.hex() + base64_fmt = base64.b64encode(data).decode('ascii') + pulse_fmt = format_pulses(data_to_pulses(data)) + + print("Raw:", raw_fmt) + print("Base64:", base64_fmt) + print("Pulses:", pulse_fmt) + + if args.learnfile: + print("Saving to {}".format(args.learnfile)) + with open(args.learnfile, "w") as text_file: + text_file.write(pulse_fmt if args.durations else raw_fmt) + if args.check: + if dev.check_power(): + print('* ON *') + else: + print('* OFF *') + if args.checknl: + if dev.check_nightlight(): + print('* ON *') + else: + print('* OFF *') + if args.turnon: + dev.set_power(True) + if dev.check_power(): + print('== Turned * ON * ==') + else: + print('!! Still OFF !!') + if args.turnoff: + dev.set_power(False) + if dev.check_power(): + print('!! Still ON !!') + else: + print('== Turned * OFF * ==') + if args.turnnlon: + dev.set_nightlight(True) + if dev.check_nightlight(): + print('== Turned * ON * ==') + else: + print('!! Still OFF !!') + if args.turnnloff: + dev.set_nightlight(False) + if dev.check_nightlight(): + print('!! Still ON !!') + else: + print('== Turned * OFF * ==') + if args.switch: + if dev.check_power(): + dev.set_power(False) + print('* Switch to OFF *') + else: + dev.set_power(True) + print('* Switch to ON *') + if args.rflearn: + if args.frequency: + frequency = args.frequency + print("Press the button you want to learn, a short press...") + else: + dev.sweep_frequency() + print("Detecting radiofrequency, press and hold the button to learn...") + + start = time.time() + while time.time() - start < TIMEOUT: + time.sleep(1) + locked, frequency = dev.check_frequency() + if locked: + break + else: + print("Radiofrequency not found") + dev.cancel_sweep_frequency() + exit(1) + + print("Radiofrequency detected: {}MHz".format(frequency)) + print("You can now let go of the button") + + input("Press enter to continue...") + + print("Press the button again, now a short press.") + + dev.find_rf_packet(frequency) + + start = time.time() + while time.time() - start < TIMEOUT: + time.sleep(1) + try: + data = dev.check_data() + except (ReadError, StorageError): + continue + else: + break + else: + print("No data received...") + exit(1) + + print("Packet found!") + raw_fmt = data.hex() + base64_fmt = base64.b64encode(data).decode('ascii') + pulse_fmt = format_pulses(data_to_pulses(data)) + + print("Raw:", raw_fmt) + print("Base64:", base64_fmt) + print("Pulses:", pulse_fmt) + + if args.learnfile: + print("Saving to {}".format(args.learnfile)) + with open(args.learnfile, "w") as text_file: + text_file.write(pulse_fmt if args.durations else raw_fmt) + + +if __name__ == "__main__": + main() diff --git a/broadlink/cli/broadlink_discovery.py b/broadlink/cli/broadlink_discovery.py new file mode 100644 index 00000000..8b7ba8d9 --- /dev/null +++ b/broadlink/cli/broadlink_discovery.py @@ -0,0 +1,35 @@ +import argparse + +import broadlink +from broadlink.const import DEFAULT_BCAST_ADDR, DEFAULT_TIMEOUT +from broadlink.exceptions import StorageError + + +def main(): + parser = argparse.ArgumentParser(fromfile_prefix_chars='@') + parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="timeout to wait for receiving discovery responses") + parser.add_argument("--ip", default=None, help="ip address to use in the discovery") + parser.add_argument("--dst-ip", default=DEFAULT_BCAST_ADDR, help="destination ip address to use in the discovery") + args = parser.parse_args() + + print("Discovering...") + devices = broadlink.discover(timeout=args.timeout, local_ip_address=args.ip, discover_ip_address=args.dst_ip) + for device in devices: + if device.auth(): + print("###########################################") + print(device.type) + print("# broadlink_cli --type {} --host {} --mac {}".format(hex(device.devtype), device.host[0], + ''.join(format(x, '02x') for x in device.mac))) + print("Device file data (to be used with --device @filename in broadlink_cli) : ") + print("{} {} {}".format(hex(device.devtype), device.host[0], ''.join(format(x, '02x') for x in device.mac))) + try: + print("temperature = {}".format(device.check_temperature())) + except (AttributeError, StorageError): + pass + print("") + else: + print("Error authenticating with device : {}".format(device.host)) + + +if __name__ == "__main__": + main() diff --git a/cli/README.md b/cli/README.md index b7e48dc9..9bc27e88 100644 --- a/cli/README.md +++ b/cli/README.md @@ -6,14 +6,19 @@ This is a command line interface for the python-broadlink API. Requirements ------------ -You need to install the module first: +Installation +------------ +Install the module, which also provides the `broadlink_cli` and `broadlink_discovery` commands. + +With pip: ``` pip3 install broadlink ``` -Installation ------------ -Download "broadlink_cli" and "broadlink_discovery". +With uv, as isolated command line tools: +``` +uv tool install broadlink +``` Programs diff --git a/cli/broadlink_cli b/cli/broadlink_cli index 7913e332..eddf13e5 100755 --- a/cli/broadlink_cli +++ b/cli/broadlink_cli @@ -1,221 +1,5 @@ #!/usr/bin/env python3 -import argparse -import base64 -import time -from typing import List +from broadlink.cli.broadlink_cli import main -import broadlink -from broadlink.const import DEFAULT_PORT -from broadlink.exceptions import ReadError, StorageError -from broadlink.remote import data_to_pulses, pulses_to_data - -TIMEOUT = 30 - - -def auto_int(x): - return int(x, 0) - - -def format_pulses(pulses: List[int]) -> str: - """Format pulses.""" - return " ".join( - f"+{pulse}" if i % 2 == 0 else f"-{pulse}" - for i, pulse in enumerate(pulses) - ) - - -def parse_pulses(data: List[str]) -> List[int]: - """Parse pulses.""" - return [abs(int(s)) for s in data] - - -parser = argparse.ArgumentParser(fromfile_prefix_chars='@') -parser.add_argument("--device", help="device definition as 'type host mac'") -parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device") -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") -parser.add_argument("--turnon", action="store_true", help="turn on device") -parser.add_argument("--turnoff", action="store_true", help="turn off device") -parser.add_argument("--turnnlon", action="store_true", help="turn on nightlight on the device") -parser.add_argument("--turnnloff", action="store_true", help="turn off nightlight on the device") -parser.add_argument("--switch", action="store_true", help="switch state from on to off and off to on") -parser.add_argument("--send", action="store_true", help="send command") -parser.add_argument("--sensors", action="store_true", help="check all sensors") -parser.add_argument("--learn", action="store_true", help="learn command") -parser.add_argument("--rflearn", action="store_true", help="rf scan learning") -parser.add_argument("--frequency", type=float, help="specify radiofrequency for learning") -parser.add_argument("--learnfile", help="save learned command to a specified file") -parser.add_argument("--durations", action="store_true", - help="use durations in micro seconds instead of the Broadlink format") -parser.add_argument("--convert", action="store_true", help="convert input data to durations") -parser.add_argument("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with") -parser.add_argument("data", nargs='*', help="Data to send or convert") -args = parser.parse_args() - -if args.device: - values = args.device.split() - devtype = int(values[0], 0) - host = values[1] - mac = bytearray.fromhex(values[2]) -elif args.mac: - devtype = args.type - host = args.host - mac = bytearray.fromhex(args.mac) - -if args.host or args.device: - dev = broadlink.gendevice(devtype, (host, DEFAULT_PORT), mac) - dev.auth() - -if args.joinwifi: - broadlink.setup(args.joinwifi[0], args.joinwifi[1], 4) - -if args.convert: - data = bytearray.fromhex(''.join(args.data)) - pulses = data_to_pulses(data) - print(format_pulses(pulses)) -if args.temperature: - print(dev.check_temperature()) -if args.humidity: - print(dev.check_humidity()) -if args.energy: - print(dev.get_energy()) -if args.sensors: - data = dev.check_sensors() - for key in data: - print("{} {}".format(key, data[key])) -if args.send: - data = ( - pulses_to_data(parse_pulses(args.data)) - if args.durations - else bytes.fromhex(''.join(args.data)) - ) - dev.send_data(data) -if args.learn or (args.learnfile and not args.rflearn): - dev.enter_learning() - print("Learning...") - start = time.time() - while time.time() - start < TIMEOUT: - time.sleep(1) - try: - data = dev.check_data() - except (ReadError, StorageError): - continue - else: - break - else: - print("No data received...") - exit(1) - - print("Packet found!") - raw_fmt = data.hex() - base64_fmt = base64.b64encode(data).decode('ascii') - pulse_fmt = format_pulses(data_to_pulses(data)) - - print("Raw:", raw_fmt) - print("Base64:", base64_fmt) - print("Pulses:", pulse_fmt) - - if args.learnfile: - print("Saving to {}".format(args.learnfile)) - with open(args.learnfile, "w") as text_file: - text_file.write(pulse_fmt if args.durations else raw_fmt) -if args.check: - if dev.check_power(): - print('* ON *') - else: - print('* OFF *') -if args.checknl: - if dev.check_nightlight(): - print('* ON *') - else: - print('* OFF *') -if args.turnon: - dev.set_power(True) - if dev.check_power(): - print('== Turned * ON * ==') - else: - print('!! Still OFF !!') -if args.turnoff: - dev.set_power(False) - if dev.check_power(): - print('!! Still ON !!') - else: - print('== Turned * OFF * ==') -if args.turnnlon: - dev.set_nightlight(True) - if dev.check_nightlight(): - print('== Turned * ON * ==') - else: - print('!! Still OFF !!') -if args.turnnloff: - dev.set_nightlight(False) - if dev.check_nightlight(): - print('!! Still ON !!') - else: - print('== Turned * OFF * ==') -if args.switch: - if dev.check_power(): - dev.set_power(False) - print('* Switch to OFF *') - else: - dev.set_power(True) - print('* Switch to ON *') -if args.rflearn: - if args.frequency: - frequency = args.frequency - print("Press the button you want to learn, a short press...") - else: - dev.sweep_frequency() - print("Detecting radiofrequency, press and hold the button to learn...") - - start = time.time() - while time.time() - start < TIMEOUT: - time.sleep(1) - locked, frequency = dev.check_frequency() - if locked: - break - else: - print("Radiofrequency not found") - dev.cancel_sweep_frequency() - exit(1) - - print("Radiofrequency detected: {}MHz".format(frequency)) - print("You can now let go of the button") - - input("Press enter to continue...") - - print("Press the button again, now a short press.") - - dev.find_rf_packet(frequency) - - start = time.time() - while time.time() - start < TIMEOUT: - time.sleep(1) - try: - data = dev.check_data() - except (ReadError, StorageError): - continue - else: - break - else: - print("No data received...") - exit(1) - - print("Packet found!") - raw_fmt = data.hex() - base64_fmt = base64.b64encode(data).decode('ascii') - pulse_fmt = format_pulses(data_to_pulses(data)) - - print("Raw:", raw_fmt) - print("Base64:", base64_fmt) - print("Pulses:", pulse_fmt) - - if args.learnfile: - print("Saving to {}".format(args.learnfile)) - with open(args.learnfile, "w") as text_file: - text_file.write(pulse_fmt if args.durations else raw_fmt) +if __name__ == "__main__": + main() diff --git a/cli/broadlink_discovery b/cli/broadlink_discovery index 477e1bd7..a3320781 100755 --- a/cli/broadlink_discovery +++ b/cli/broadlink_discovery @@ -1,30 +1,5 @@ #!/usr/bin/env python3 -import argparse +from broadlink.cli.broadlink_discovery import main -import broadlink -from broadlink.const import DEFAULT_BCAST_ADDR, DEFAULT_TIMEOUT -from broadlink.exceptions import StorageError - -parser = argparse.ArgumentParser(fromfile_prefix_chars='@') -parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="timeout to wait for receiving discovery responses") -parser.add_argument("--ip", default=None, help="ip address to use in the discovery") -parser.add_argument("--dst-ip", default=DEFAULT_BCAST_ADDR, help="destination ip address to use in the discovery") -args = parser.parse_args() - -print("Discovering...") -devices = broadlink.discover(timeout=args.timeout, local_ip_address=args.ip, discover_ip_address=args.dst_ip) -for device in devices: - if device.auth(): - print("###########################################") - print(device.type) - print("# broadlink_cli --type {} --host {} --mac {}".format(hex(device.devtype), device.host[0], - ''.join(format(x, '02x') for x in device.mac))) - print("Device file data (to be used with --device @filename in broadlink_cli) : ") - print("{} {} {}".format(hex(device.devtype), device.host[0], ''.join(format(x, '02x') for x in device.mac))) - try: - print("temperature = {}".format(device.check_temperature())) - except (AttributeError, StorageError): - pass - print("") - else: - print("Error authenticating with device : {}".format(device.host)) +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 0426f148..c3abc63f 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,12 @@ author_email="mjg59@srcf.ucam.org", url="http://github.com/mjg59/python-broadlink", packages=find_packages(), - scripts=[], + entry_points={ + "console_scripts": [ + "broadlink_cli=broadlink.cli.broadlink_cli:main", + "broadlink_discovery=broadlink.cli.broadlink_discovery:main", + ], + }, install_requires=["cryptography>=3.2"], description="Python API for controlling Broadlink devices", classifiers=[ From 3f9f3c1113596fae29254afd7b5482fa71b76e1f Mon Sep 17 00:00:00 2001 From: Dan Van Derveer Date: Sun, 4 Oct 2026 13:00:22 -0400 Subject: [PATCH 2/2] Format CLI code with black --- broadlink/cli/broadlink_cli.py | 102 +++++++++++++++++---------- broadlink/cli/broadlink_discovery.py | 40 ++++++++--- setup.py | 3 +- 3 files changed, 99 insertions(+), 46 deletions(-) diff --git a/broadlink/cli/broadlink_cli.py b/broadlink/cli/broadlink_cli.py index 740f170b..d73a885b 100644 --- a/broadlink/cli/broadlink_cli.py +++ b/broadlink/cli/broadlink_cli.py @@ -18,8 +18,7 @@ def auto_int(x): def format_pulses(pulses: List[int]) -> str: """Format pulses.""" return " ".join( - f"+{pulse}" if i % 2 == 0 else f"-{pulse}" - for i, pulse in enumerate(pulses) + f"+{pulse}" if i % 2 == 0 else f"-{pulse}" for i, pulse in enumerate(pulses) ) @@ -29,32 +28,63 @@ def parse_pulses(data: List[str]) -> List[int]: def main(): - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') + parser = argparse.ArgumentParser(fromfile_prefix_chars="@") parser.add_argument("--device", help="device definition as 'type host mac'") parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device") 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") + 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" + ) parser.add_argument("--turnon", action="store_true", help="turn on device") parser.add_argument("--turnoff", action="store_true", help="turn off device") - parser.add_argument("--turnnlon", action="store_true", help="turn on nightlight on the device") - parser.add_argument("--turnnloff", action="store_true", help="turn off nightlight on the device") - parser.add_argument("--switch", action="store_true", help="switch state from on to off and off to on") + parser.add_argument( + "--turnnlon", action="store_true", help="turn on nightlight on the device" + ) + parser.add_argument( + "--turnnloff", action="store_true", help="turn off nightlight on the device" + ) + parser.add_argument( + "--switch", + action="store_true", + help="switch state from on to off and off to on", + ) parser.add_argument("--send", action="store_true", help="send command") parser.add_argument("--sensors", action="store_true", help="check all sensors") parser.add_argument("--learn", action="store_true", help="learn command") parser.add_argument("--rflearn", action="store_true", help="rf scan learning") - parser.add_argument("--frequency", type=float, help="specify radiofrequency for learning") + parser.add_argument( + "--frequency", type=float, help="specify radiofrequency for learning" + ) parser.add_argument("--learnfile", help="save learned command to a specified file") - parser.add_argument("--durations", action="store_true", - help="use durations in micro seconds instead of the Broadlink format") - parser.add_argument("--convert", action="store_true", help="convert input data to durations") - parser.add_argument("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with") - parser.add_argument("data", nargs='*', help="Data to send or convert") + parser.add_argument( + "--durations", + action="store_true", + help="use durations in micro seconds instead of the Broadlink format", + ) + parser.add_argument( + "--convert", action="store_true", help="convert input data to durations" + ) + parser.add_argument( + "--joinwifi", + nargs=2, + help="Args are SSID PASSPHRASE to configure Broadlink device with", + ) + parser.add_argument("data", nargs="*", help="Data to send or convert") args = parser.parse_args() if args.device: @@ -75,7 +105,7 @@ def main(): broadlink.setup(args.joinwifi[0], args.joinwifi[1], 4) if args.convert: - data = bytearray.fromhex(''.join(args.data)) + data = bytearray.fromhex("".join(args.data)) pulses = data_to_pulses(data) print(format_pulses(pulses)) if args.temperature: @@ -92,7 +122,7 @@ def main(): data = ( pulses_to_data(parse_pulses(args.data)) if args.durations - else bytes.fromhex(''.join(args.data)) + else bytes.fromhex("".join(args.data)) ) dev.send_data(data) if args.learn or (args.learnfile and not args.rflearn): @@ -113,7 +143,7 @@ def main(): print("Packet found!") raw_fmt = data.hex() - base64_fmt = base64.b64encode(data).decode('ascii') + base64_fmt = base64.b64encode(data).decode("ascii") pulse_fmt = format_pulses(data_to_pulses(data)) print("Raw:", raw_fmt) @@ -126,45 +156,45 @@ def main(): text_file.write(pulse_fmt if args.durations else raw_fmt) if args.check: if dev.check_power(): - print('* ON *') + print("* ON *") else: - print('* OFF *') + print("* OFF *") if args.checknl: if dev.check_nightlight(): - print('* ON *') + print("* ON *") else: - print('* OFF *') + print("* OFF *") if args.turnon: dev.set_power(True) if dev.check_power(): - print('== Turned * ON * ==') + print("== Turned * ON * ==") else: - print('!! Still OFF !!') + print("!! Still OFF !!") if args.turnoff: dev.set_power(False) if dev.check_power(): - print('!! Still ON !!') + print("!! Still ON !!") else: - print('== Turned * OFF * ==') + print("== Turned * OFF * ==") if args.turnnlon: dev.set_nightlight(True) if dev.check_nightlight(): - print('== Turned * ON * ==') + print("== Turned * ON * ==") else: - print('!! Still OFF !!') + print("!! Still OFF !!") if args.turnnloff: dev.set_nightlight(False) if dev.check_nightlight(): - print('!! Still ON !!') + print("!! Still ON !!") else: - print('== Turned * OFF * ==') + print("== Turned * OFF * ==") if args.switch: if dev.check_power(): dev.set_power(False) - print('* Switch to OFF *') + print("* Switch to OFF *") else: dev.set_power(True) - print('* Switch to ON *') + print("* Switch to ON *") if args.rflearn: if args.frequency: frequency = args.frequency @@ -208,7 +238,7 @@ def main(): print("Packet found!") raw_fmt = data.hex() - base64_fmt = base64.b64encode(data).decode('ascii') + base64_fmt = base64.b64encode(data).decode("ascii") pulse_fmt = format_pulses(data_to_pulses(data)) print("Raw:", raw_fmt) diff --git a/broadlink/cli/broadlink_discovery.py b/broadlink/cli/broadlink_discovery.py index 8b7ba8d9..f9268742 100644 --- a/broadlink/cli/broadlink_discovery.py +++ b/broadlink/cli/broadlink_discovery.py @@ -6,22 +6,46 @@ def main(): - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="timeout to wait for receiving discovery responses") + parser = argparse.ArgumentParser(fromfile_prefix_chars="@") + parser.add_argument( + "--timeout", + type=int, + default=DEFAULT_TIMEOUT, + help="timeout to wait for receiving discovery responses", + ) parser.add_argument("--ip", default=None, help="ip address to use in the discovery") - parser.add_argument("--dst-ip", default=DEFAULT_BCAST_ADDR, help="destination ip address to use in the discovery") + parser.add_argument( + "--dst-ip", + default=DEFAULT_BCAST_ADDR, + help="destination ip address to use in the discovery", + ) args = parser.parse_args() print("Discovering...") - devices = broadlink.discover(timeout=args.timeout, local_ip_address=args.ip, discover_ip_address=args.dst_ip) + devices = broadlink.discover( + timeout=args.timeout, local_ip_address=args.ip, discover_ip_address=args.dst_ip + ) for device in devices: if device.auth(): print("###########################################") print(device.type) - print("# broadlink_cli --type {} --host {} --mac {}".format(hex(device.devtype), device.host[0], - ''.join(format(x, '02x') for x in device.mac))) - print("Device file data (to be used with --device @filename in broadlink_cli) : ") - print("{} {} {}".format(hex(device.devtype), device.host[0], ''.join(format(x, '02x') for x in device.mac))) + print( + "# broadlink_cli --type {} --host {} --mac {}".format( + hex(device.devtype), + device.host[0], + "".join(format(x, "02x") for x in device.mac), + ) + ) + print( + "Device file data (to be used with --device @filename in broadlink_cli) : " + ) + print( + "{} {} {}".format( + hex(device.devtype), + device.host[0], + "".join(format(x, "02x") for x in device.mac), + ) + ) try: print("temperature = {}".format(device.check_temperature())) except (AttributeError, StorageError): diff --git a/setup.py b/setup.py index c3abc63f..a3f8cd61 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,7 @@ from setuptools import setup, find_packages - -version = '0.19.0' +version = "0.19.0" setup( name="broadlink",