### setup usage examples Source: https://github.com/mjg59/python-broadlink Configures a device to join a Wi-Fi network. The first call uses the default broadcast address; the second specifies a custom IP. Fire-and-forget UDP send. ```python import broadlink broadlink.setup('myssid', 'mynetworkpass', 3) broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255') ``` -------------------------------- ### Basic discover() usage example Source: https://github.com/mjg59/python-broadlink Simple example that imports broadlink, calls discover() with default parameters, authenticates each found device, and prints its host and MAC address. ```python import broadlink devices = broadlink.discover() for device in devices: device.auth() print(device.host, device.mac.hex()) ``` -------------------------------- ### xdiscover() usage example Source: https://github.com/mjg59/python-broadlink Example that iterates over the generator returned by xdiscover() and prints each device as it responds, allowing processing of devices in real time. ```python import broadlink for device in broadlink.xdiscover(): print(device) # process devices as they respond ``` -------------------------------- ### hello usage example Source: https://github.com/mjg59/python-broadlink Unicast discovery against a single host. Locked devices may not answer broadcasts. ```python import broadlink device = broadlink.hello('192.168.0.16') ``` -------------------------------- ### Example SUPPORTED_TYPES entry for sp2 Source: https://github.com/mjg59/python-broadlink Example entry showing the nested mapping for the sp2 device class: product ID to (model, manufacturer) tuple. ```python sp2: { 0x2720: ("SP mini", "Broadlink"), 0x7530: ("SP2", "Broadlink (OEM)"), ... } ``` -------------------------------- ### gendevice usage example Source: https://github.com/mjg59/python-broadlink Creates a device with gendevice and authenticates it. The device type 0x2720 corresponds to an SP mini. ```python import broadlink dev = broadlink.gendevice(0x2720, ("192.168.0.16", 80), bytes.fromhex("aabbccddeeff")) dev.auth() ``` -------------------------------- ### Setup with broadcast address Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Specify a broadcast address if setup is not working. ```python3 broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255') ``` -------------------------------- ### discover() with custom parameters and error handling Source: https://github.com/mjg59/python-broadlink Example showing how to pass custom timeout, local_ip_address, and discover_ip_address to discover(). Uses DEFAULT_TIMEOUT from broadlink.const and catches exceptions during discovery. ```python from broadlink.const import DEFAULT_TIMEOUT try: devices = broadlink.discover( timeout=20, local_ip_address="192.168.0.100", discover_ip_address="192.168.0.255", ) print("Found %d devices" % len(devices)) except Exception as err: print("Discovery failed:", err) ``` -------------------------------- ### wser usage example Source: https://github.com/mjg59/python-broadlink Example calls to open, get position, and set position. ```python print(dev.open()) print(dev.get_position()) dev.set_position(80) ``` -------------------------------- ### broadlink_discovery example invocations Source: https://github.com/mjg59/python-broadlink Example commands for running discovery with default settings and with a custom timeout and destination IP. ```bash broadlink_discovery broadlink_discovery --timeout 30 --dst-ip 192.168.0.255 ``` -------------------------------- ### sp3 usage example Source: https://github.com/mjg59/python-broadlink Example of using sp3 methods: set_power, set_nightlight, check_power, and check_nightlight. ```python dev.set_power(True) dev.set_nightlight(True) print(dev.check_power(), dev.check_nightlight()) ``` -------------------------------- ### broadlink_cli examples Source: https://github.com/mjg59/python-broadlink Examples covering learning IR codes, sending raw hex or pulse durations, controlling a plug, configuring Wi-Fi, and converting raw packets to pulse durations. ```bash # Learn an IR code and print raw/base64/pulses broadlink_cli --device @BEDROOM.device --learn ``` ```bash # Send a previously learned code (raw hex) broadlink_cli --device @BEDROOM.device --send 260048000... ``` ```bash # Send pulses in microseconds broadlink_cli --device "0x2712 192.168.0.16 aabbccddeeff" --durations --send +8920 -4450 +592 -556 ``` ```bash # Control a plug broadlink_cli --type 0x2720 --host 192.168.0.16 --mac aabbccddeeff --turnon ``` ```bash # Configure a device's Wi-Fi (AP mode) with WPA2 broadlink_cli --joinwifi MySSID MyPassword ``` ```bash # Convert a raw packet to pulse durations broadlink_cli --device @BEDROOM.device --convert 26004800... ``` -------------------------------- ### Install broadlink with pip3 Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Install the latest version of the module using pip3. ```bash pip3 install broadlink ``` -------------------------------- ### sp1 set_power usage example Source: https://github.com/mjg59/python-broadlink Example call to set_power on an sp1 device, turning it on. ```python dev.set_power(True) ``` -------------------------------- ### set_state usage example Source: https://github.com/mjg59/python-broadlink Example call to set_state with pwr, ntlight, and ntlbrightness parameters. ```python dev.set_state(pwr=1, ntlight=1, ntlbrightness=50) ``` -------------------------------- ### sp2s get_energy usage example Source: https://github.com/mjg59/python-broadlink Example of calling get_energy on an sp2s device to get instantaneous power draw in watts. ```python watts = dev.get_energy() ``` -------------------------------- ### Instant discovery with broadlink.xdiscover Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Create devices instantly using xdiscover(). The example action prints each device. ```python3 for device in broadlink.xdiscover(): print(device) # Example action. Do whatever you want here. ``` -------------------------------- ### get_state usage example Source: https://github.com/mjg59/python-broadlink Example of retrieving and printing the power state from the device state dict. ```python state = dev.get_state() print(state["pwr"]) ``` -------------------------------- ### hello usage example Source: https://github.com/mjg59/python-broadlink Shows how to create a device with broadlink.hello and call the hello method. ```python dev = broadlink.hello("192.168.0.16") dev.hello() ``` -------------------------------- ### get_subdevices usage example Source: https://github.com/mjg59/python-broadlink Example of calling get_subdevices and printing each sub-device's DID. ```python subdevices = dev.get_subdevices() for sub in subdevices: print(sub["did"]) ``` -------------------------------- ### setup function definition Source: https://github.com/mjg59/python-broadlink Defines the `setup` function to configure a new Broadlink device via AP mode. Sends a 136-byte UDP packet with SSID/password/security mode to `ip_address:80`. Only tested with Broadlink RM3 Mini (Blackbean). ```python def setup( ssid: str, password: str, security_mode: int, ip_address: str = DEFAULT_BCAST_ADDR, ) -> None: """Set up a new Broadlink device via AP mode.""" ``` -------------------------------- ### broadlink.setup Source: https://github.com/mjg59/python-broadlink Function to set up a device. Re-exported from the broadlink package. ```APIDOC ## broadlink.setup ### Description Sets up a device. ### Method Function ### Signature `setup(...)` ``` -------------------------------- ### get_state usage example Source: https://github.com/mjg59/python-broadlink Example of calling get_state with a specific sub-device DID. ```python state = dev.get_state(did="00000000000000000000a043b0d06963") ``` -------------------------------- ### setup(ssid, password, security_mode, ip_address) Source: https://github.com/mjg59/python-broadlink Configures an unconfigured Broadlink device in AP mode to join a Wi-Fi network. Sends a UDP packet with the network credentials to the specified broadcast address. ```APIDOC ## `setup` ### Description Set up a new Broadlink device via AP mode. ### Method Function call ### Signature ```python def setup( ssid: str, password: str, security_mode: int, ip_address: str = DEFAULT_BCAST_ADDR, ) -> None: ``` ### Parameters - **ssid** (str) - Required - Wi-Fi network name - **password** (str) - Required - Wi-Fi password - **security_mode** (int) - Required - 0 = none, 1 = WEP, 2 = WPA1, 3 = WPA2, 4 = WPA1/2 - **ip_address** (str) - Optional - Destination broadcast address (default: `"255.255.255.255"`) ### Returns `None` (fire-and-forget UDP send) ### Request Example ```python import broadlink broadlink.setup('myssid', 'mynetworkpass', 3) broadlink.setup('myssid', 'mynetworkpass', 3, ip_address='192.168.0.255') ``` ``` -------------------------------- ### set_state usage example with pwr and pwr1/pwr2 Source: https://github.com/mjg59/python-broadlink Example of setting state with master power and per-socket states. ```python dev.set_state(did="00000000000000000000a043b0d0783a", pwr=1, pwr1=1, pwr2=1) ``` -------------------------------- ### set_mode usage example Source: https://github.com/mjg59/python-broadlink Example setting manual mode with loop_mode 2. ```python dev.set_mode(auto_mode=0, loop_mode=2) ``` -------------------------------- ### enter_learning usage example Source: https://github.com/mjg59/python-broadlink Example of entering learning mode and then checking for the captured code. ```python dev.enter_learning() packet = dev.check_data() ``` -------------------------------- ### hello error handling example Source: https://github.com/mjg59/python-broadlink Demonstrates catching DataValidationError when hello fails due to MAC or product ID mismatch. ```python from broadlink.exceptions import DataValidationError try: dev.hello() except DataValidationError as err: print(err) # [Errno -2040] Device information is not intact: The MAC address is different: ... ``` -------------------------------- ### switch_to_manual and set_temp usage example Source: https://github.com/mjg59/python-broadlink Example calling dev.switch_to_manual() then dev.set_temp(21.5). ```python dev.switch_to_manual() dev.set_temp(21.5) ``` -------------------------------- ### set_state usage examples Source: https://github.com/mjg59/python-broadlink Shows how to set power, brightness, RGB color, and white color temperature using set_state(). ```python dev.set_state(pwr=1, brightness=75) dev.set_state(bulb_colormode=0, red=255, green=128, blue=0) # RGB dev.set_state(bulb_colormode=1, colortemp=2700) # white ``` -------------------------------- ### mp1.set_power usage example Source: https://github.com/mjg59/python-broadlink Example calls to set_power on an mp1 device, turning socket 1 on and socket 2 off. ```python dev.set_power(1, True) # socket 1 on dev.set_power(2, False) # socket 2 off ``` -------------------------------- ### set_state usage example Source: https://github.com/mjg59/python-broadlink Example call to set_state with a cooling mode configuration. Uses hvac.Mode.COOL, hvac.Speed.AUTO, hvac.Preset.NORMAL, hvac.SwHoriz.ON, and hvac.SwVert.POS2. Requires importing broadlink.hvac. ```python from broadlink import hvac dev.set_state( power=True, target_temp=23.0, mode=hvac.Mode.COOL, speed=hvac.Speed.AUTO, preset=hvac.Preset.NORMAL, swing_h=hvac.SwHoriz.ON, swing_v=hvac.SwVert.POS2, sleep=False, ifeel=False, display=True, health=True, clean=False, mildew=False, ) ``` -------------------------------- ### set_lock usage example Source: https://github.com/mjg59/python-broadlink Shows how to lock the device. ```python dev.set_lock(True) ``` -------------------------------- ### get_fwversion usage example Source: https://github.com/mjg59/python-broadlink Shows how to retrieve the firmware version from a device. ```python version = dev.get_fwversion() ``` -------------------------------- ### Enter IR learning mode Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Starts IR learning mode on the Broadlink device. The LED blinks to indicate the device is ready to learn a code. ```python3 device.enter_learning() ``` -------------------------------- ### set_name usage example Source: https://github.com/mjg59/python-broadlink Shows how to set the device name. ```python dev.set_name("Living room") ``` -------------------------------- ### bg1 set_state usage example Source: https://github.com/mjg59/python-broadlink Example of calling set_state() on a bg1 device to set outlet 1 on and outlet 2 off. ```python dev.set_state(pwr1=1, pwr2=0) ``` -------------------------------- ### set_power usage examples Source: https://github.com/mjg59/python-broadlink Two example calls: dev.set_power(1) turns on, dev.set_power(0, remote_lock=1) turns off and locks buttons. ```python dev.set_power(1) # on dev.set_power(0, remote_lock=1) # off + lock buttons ``` -------------------------------- ### send_request usage example Source: https://github.com/mjg59/python-broadlink Example call to send_request with a command byte sequence. ```python payload = dev.send_request([0x01, 0x03, 0x00, 0x00, 0x00, 0x08]) ``` -------------------------------- ### broadlink.rmmini Source: https://github.com/mjg59/python-broadlink Class for RM Mini device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rmmini ### Description Class representing an RM Mini device. ### Method Class ### Signature `rmmini(...)` ``` -------------------------------- ### sp2 check_power usage example Source: https://github.com/mjg59/python-broadlink Example call to check_power on an sp2 device, storing the returned boolean state. ```python state = dev.check_power() ``` -------------------------------- ### check_sensors_raw usage example Source: https://github.com/mjg59/python-broadlink Example of calling check_sensors_raw() to get raw integer sensor levels. ```python raw = dev.check_sensors_raw() # raw integer levels ``` -------------------------------- ### sp4b get_state usage example Source: https://github.com/mjg59/python-broadlink Example of calling get_state() on an sp4b device, returning normalized sensor values in A, V, W, and kW·h. ```python state = dev.get_state() # {'pwr': 1, 'volt': 230.4, 'current': 0.32, ...} ``` -------------------------------- ### get_state usage example Source: https://github.com/mjg59/python-broadlink Shows how to call get_state() and access the pwr and brightness keys from the returned dict. ```python state = dev.get_state() print(state["pwr"], state["brightness"]) ``` -------------------------------- ### mp1.check_power usage example Source: https://github.com/mjg59/python-broadlink Example usage of check_power on an mp1 device, checking if socket 1 is on and printing a message. ```python state = dev.check_power() if state["s1"]: print("Socket 1 is on") ``` -------------------------------- ### set_state usage example Source: https://github.com/mjg59/python-broadlink Example call to set_state that turns on the device, sets brightness to 50, and sets bulb_colormode to 2. ```python dev.set_state(pwr=1, brightness=50, bulb_colormode=2) ``` -------------------------------- ### ping usage example Source: https://github.com/mjg59/python-broadlink Example of calling ping() with a device IP. The ping must be resent every 2 minutes when the cloud is unreachable. ```python from broadlink.device import ping ping("192.168.0.16") # must be resent every 2 minutes when cloud is unreachable ``` -------------------------------- ### scan usage example Source: https://github.com/mjg59/python-broadlink Example of calling scan() with a 5-second timeout and iterating over the yielded HelloResponse tuples, printing devtype as hex, host, mac as hex, name, and is_locked. ```python from broadlink.device import scan for devtype, host, mac, name, is_locked in scan(timeout=5): print(hex(devtype), host, mac.hex(), name, is_locked) ``` -------------------------------- ### get_state usage example Source: https://github.com/mjg59/python-broadlink Example call to get_state that retrieves the current state and prints the mode and target temperature. ```python state = dev.get_state() print(state["mode"], state["target_temp"]) ``` -------------------------------- ### hello with error handling Source: https://github.com/mjg59/python-broadlink Calls `hello` with a timeout and catches `NetworkTimeoutError`. Prints the error message including errno -4000. ```python from broadlink import hello from broadlink.exceptions import NetworkTimeoutError try: dev = hello('192.168.0.16', timeout=3) except NetworkTimeoutError as err: print("No response:", err) # [Errno -4000] Network timeout: No response received within 3s ``` -------------------------------- ### auth usage example Source: https://github.com/mjg59/python-broadlink Calls auth() on a device instance. ```python device.auth() ``` -------------------------------- ### ehc31.set_state usage example Source: https://github.com/mjg59/python-broadlink Example call to set_state on an ehc31 device, turning on outlets 1 and 3 and disabling the child lock. ```python dev.set_state(pwr1=1, pwr3=1, childlock=0) ``` -------------------------------- ### broadlink.discover Source: https://github.com/mjg59/python-broadlink Function to discover devices. Re-exported from the broadlink package. ```APIDOC ## broadlink.discover ### Description Discovers devices. ### Method Function ### Signature `discover(...)` ``` -------------------------------- ### broadlink.e Source: https://github.com/mjg59/python-broadlink Module alias for broadlink.exceptions, e.g. broadlink.e.NetworkTimeoutError. ```APIDOC ## broadlink.e ### Description Alias for the broadlink.exceptions module. ### Type Module ``` -------------------------------- ### broadlink.rmminib Source: https://github.com/mjg59/python-broadlink Class for RM Mini B device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rmminib ### Description Class representing an RM Mini B device. ### Method Class ### Signature `rmminib(...)` ``` -------------------------------- ### Turn on smart plug Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Turn on a smart plug. ```bash broadlink_cli --device @BEDROOM.device --turnon ``` -------------------------------- ### Datetime.now usage example Source: https://github.com/mjg59/python-broadlink Gets the current timestamp; used internally by the scan() function. ```python ts = Datetime.now() # used internally by scan() ``` -------------------------------- ### broadlink.wser Source: https://github.com/mjg59/python-broadlink Class for WSER device. Re-exported from the broadlink package. ```APIDOC ## broadlink.wser ### Description Class representing a WSER device. ### Method Class ### Signature `wser(...)` ``` -------------------------------- ### broadlink.sp3 Source: https://github.com/mjg59/python-broadlink Class for SP3 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp3 ### Description Class representing an SP3 device. ### Method Class ### Signature `sp3(...)` ``` -------------------------------- ### get_type usage example Source: https://github.com/mjg59/python-broadlink Shows how to print the device type, e.g., RMMINI. ```python print(dev.get_type()) # e.g. RMMINI ``` -------------------------------- ### s3.get_subdevices Source: https://github.com/mjg59/python-broadlink Gets subdevices of an S3 device. Accepts a step parameter. ```APIDOC ## s3.get_subdevices ### Description Gets subdevices of an S3 device. Accepts a step parameter. ### Method Method call ### Parameters #### Arguments - **step** (int) - Optional - Step size (default 5) ``` -------------------------------- ### broadlink.Device Source: https://github.com/mjg59/python-broadlink Function to create a device instance. Re-exported from the broadlink package. ```APIDOC ## broadlink.Device ### Description Creates a device instance. ### Method Function ### Signature `Device(...)` ``` -------------------------------- ### broadlink.rm Source: https://github.com/mjg59/python-broadlink Class for RM device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rm ### Description Class representing an RM device. ### Method Class ### Signature `rm(...)` ``` -------------------------------- ### broadlink.gendevice Source: https://github.com/mjg59/python-broadlink Function to generate a device instance. Re-exported from the broadlink package. ```APIDOC ## broadlink.gendevice ### Description Generates a device instance. ### Method Function ### Signature `gendevice(...)` ``` -------------------------------- ### broadlink.sp2 Source: https://github.com/mjg59/python-broadlink Class for SP2 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp2 ### Description Class representing an SP2 device. ### Method Class ### Signature `sp2(...)` ``` -------------------------------- ### send_packet usage example Source: https://github.com/mjg59/python-broadlink Sends a state command (0x6A) with a 16-byte payload and decrypts the response starting at offset 0x38. The response includes the 0x38-byte header. ```python resp = dev.send_packet(0x6A, bytearray([0x01]) + bytearray(15)) payload = dev.decrypt(resp[0x38:]) ``` -------------------------------- ### broadlink.sp1 Source: https://github.com/mjg59/python-broadlink Class for SP1 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp1 ### Description Class representing an SP1 device. ### Method Class ### Signature `sp1(...)` ``` -------------------------------- ### broadlink.sp4 Source: https://github.com/mjg59/python-broadlink Class for SP4 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp4 ### Description Class representing an SP4 device. ### Method Class ### Signature `sp4(...)` ``` -------------------------------- ### broadlink.rm4mini Source: https://github.com/mjg59/python-broadlink Class for RM4 Mini device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rm4mini ### Description Class representing an RM4 Mini device. ### Method Class ### Signature `rm4mini(...)` ``` -------------------------------- ### broadlink.rmpro Source: https://github.com/mjg59/python-broadlink Class for RM Pro device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rmpro ### Description Class representing an RM Pro device. ### Method Class ### Signature `rmpro(...)` ``` -------------------------------- ### broadlink.sp3s Source: https://github.com/mjg59/python-broadlink Class for SP3S device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp3s ### Description Class representing an SP3S device. ### Method Class ### Signature `sp3s(...)` ``` -------------------------------- ### broadlink.xdiscover Source: https://github.com/mjg59/python-broadlink Function to discover devices (extended). Re-exported from the broadlink package. ```APIDOC ## broadlink.xdiscover ### Description Extended device discovery. ### Method Function ### Signature `xdiscover(...)` ``` -------------------------------- ### broadlink.lb2 Source: https://github.com/mjg59/python-broadlink Class for LB2 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.lb2 ### Description Class representing an LB2 device. ### Method Class ### Signature `lb2(...)` ``` -------------------------------- ### set_temp usage example Source: https://github.com/mjg59/python-broadlink Example call to set_temp with 21.0°C. ```python dev.set_temp(21.0) ``` -------------------------------- ### broadlink.hello Source: https://github.com/mjg59/python-broadlink Function to send a hello message. Re-exported from the broadlink package. ```APIDOC ## broadlink.hello ### Description Sends a hello message. ### Method Function ### Signature `hello(...)` ``` -------------------------------- ### broadlink.lb1 Source: https://github.com/mjg59/python-broadlink Class for LB1 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.lb1 ### Description Class representing an LB1 device. ### Method Class ### Signature `lb1(...)` ``` -------------------------------- ### broadlink.mp1 Source: https://github.com/mjg59/python-broadlink Class for MP1 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.mp1 ### Description Class representing an MP1 device. ### Method Class ### Signature `mp1(...)` ``` -------------------------------- ### broadlink.a1 Source: https://github.com/mjg59/python-broadlink Class for A1 device. Re-exported from the broadlink package. ```APIDOC ## broadlink.a1 ### Description Class representing an A1 device. ### Method Class ### Signature `a1(...)` ``` -------------------------------- ### set_time usage example Source: https://github.com/mjg59/python-broadlink Example call setting Friday 18:30:00. ```python dev.set_time(18, 30, 0, 5) # Friday 18:30:00 ``` -------------------------------- ### Turn on nightlight Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Turn on the nightlight of a smart plug. ```bash broadlink_cli --device @BEDROOM.device --turnnlon ``` -------------------------------- ### Learn RF code and show at console Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Learn an RF code and display it at the console. ```bash broadlink_cli --device @BEDROOM.device --rfscanlearn ``` -------------------------------- ### get_full_status usage example Source: https://github.com/mjg59/python-broadlink Example retrieving and printing status fields. ```python status = dev.get_full_status() print(status["room_temp"], status["power"], status["weekday"]) ``` -------------------------------- ### Open Python 3 and import broadlink Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Start an interactive Python 3 session and import the broadlink module. ```bash python3 ``` ```python3 import broadlink ``` -------------------------------- ### send_data usage example Source: https://github.com/mjg59/python-broadlink Example of sending a data packet to the device. ```python dev.send_data(data) ``` -------------------------------- ### enter_learning() Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Puts the device into learning mode to capture IR or RF codes. The LED will blink when ready. ```APIDOC ## enter_learning() ### Description Puts the device into learning mode to capture IR or RF codes. The LED will blink when ready. ### Method Device method ### Endpoint `device.enter_learning()` ### Parameters None ### Response None ### Request Example ```python3 device.enter_learning() ``` ``` -------------------------------- ### broadlink.hysen Source: https://github.com/mjg59/python-broadlink Class for Hysen device. Re-exported from the broadlink package. ```APIDOC ## broadlink.hysen ### Description Class representing a Hysen device. ### Method Class ### Signature `hysen(...)` ``` -------------------------------- ### Specify device using file Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Use a file containing device parameters with the @ prefix. This is preferred because the configuration is stored in a file and can be changed later to point to a different device. ```bash broadlink_cli --device @BEDROOM.device --temp ``` -------------------------------- ### Setup device with broadlink.setup Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Connect a device in AP mode to your local network. Security mode options: 0 = none, 1 = WEP, 2 = WPA1, 3 = WPA2, 4 = WPA1/2. ```python3 broadlink.setup('myssid', 'mynetworkpass', 3) ``` -------------------------------- ### Learn IR code and show at console Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Learn an IR code and display it at the console. ```bash broadlink_cli --device @BEDROOM.device --learn ``` -------------------------------- ### Enter RF learning mode Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Starts the RF learning mode after the frequency sweep. The LED blinks to indicate the device is ready for the second remote button press. ```python3 device.find_rf_packet() ``` -------------------------------- ### broadlink.rm4pro Source: https://github.com/mjg59/python-broadlink Class for RM4 Pro device. Re-exported from the broadlink package. ```APIDOC ## broadlink.rm4pro ### Description Class representing an RM4 Pro device. ### Method Class ### Signature `rm4pro(...)` ``` -------------------------------- ### get_temp and get_external_temp usage example Source: https://github.com/mjg59/python-broadlink Example printing both room and external temperatures. ```python print(dev.get_temp(), dev.get_external_temp()) ``` -------------------------------- ### SUPPORTED_TYPES mapping Source: https://github.com/mjg59/python-broadlink Module-level dict mapping each device class to a nested mapping of product IDs. Used by gendevice() for dispatch. ```python SUPPORTED_TYPES = { sp1: {...}, sp2: {...}, sp2s: {...}, sp3: {...}, sp3s: {...}, sp4: {...}, sp4b: {...}, rmmini: {...}, rmpro: {...}, rmminib: {...}, rm4mini: {...}, rm4pro: {...}, a1: {...}, a2: {...}, mp1: {...}, mp1s: {...}, lb1: {...}, lb2: {...}, S1C: {...}, s3: {...}, hvac: {...}, hysen: {...}, dooya: {...}, dooya2: {...}, wser: {...}, bg1: {...}, ehc31: {...}, } ``` -------------------------------- ### hello(ip_address, port, timeout) Source: https://github.com/mjg59/python-broadlink Performs unicast discovery against a single host, useful for locked devices that may not respond to broadcasts. Returns a Device object or raises a NetworkTimeoutError if no response is received within the timeout. ```APIDOC ## `hello` ### Description Direct device discovery. Useful if the device is locked. ### Method Function call ### Signature ```python def hello( ip_address: str, port: int = DEFAULT_PORT, timeout: int = DEFAULT_TIMEOUT, ) -> Device: ``` ### Parameters - **ip_address** (str) - Required - Device IP address - **port** (int) - Optional - Device port (default: `80`) - **timeout** (int) - Optional - Seconds to wait (default: `10`) ### Returns `Device` ### Raises `broadlink.exceptions.NetworkTimeoutError` (errno `-4000`) if no response within `timeout`. ### Request Example ```python import broadlink device = broadlink.hello('192.168.0.16') ``` ```python from broadlink import hello from broadlink.exceptions import NetworkTimeoutError try: dev = hello('192.168.0.16', timeout=3) except NetworkTimeoutError as err: print("No response:", err) # [Errno -4000] Network timeout: No response received within 3s ``` ``` -------------------------------- ### Learn RF code and save to file Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Learn an RF code and save it to a file specified with --learnfile. ```bash broadlink_cli --device @BEDROOM.device --rfscanlearn --learnfile LG-TV.power ``` -------------------------------- ### dooya usage example Source: https://github.com/mjg59/python-broadlink Example calls to open, close, and stop the curtain. ```python dev.open() dev.close() dev.stop() ``` -------------------------------- ### CRC16.calculate usage example Source: https://github.com/mjg59/python-broadlink Shows how to calculate a CRC for a byte sequence using the default polynomial and init value, then print the result in hex. ```python from broadlink.helpers import CRC16 crc = CRC16.calculate([0x01, 0x03, 0x00, 0x00, 0x00, 0x08]) print(hex(crc)) ``` -------------------------------- ### get_state usage example Source: https://github.com/mjg59/python-broadlink Shows how to call get_state on a device instance. The returned dictionary contains voltage in volts, current in amperes, power in watts, and total consumption in kilowatt-hours. ```python state = dev.get_state() # {'volt': 230.1, 'current': 0.34, 'power': 78.2, 'totalconsum': 1234.5} ``` -------------------------------- ### discover(timeout, local_ip_address, discover_ip_address, discover_ip_port) Source: https://github.com/mjg59/python-broadlink Broadcasts a UDP hello packet and collects all responding devices until the timeout expires. Returns a list of Device instances, one per unique response, deduplicated by host, MAC, and type. ```APIDOC ## discover(timeout, local_ip_address, discover_ip_address, discover_ip_port) ### Description Broadcasts a UDP hello packet and collects all responding devices until the timeout expires. Returns a list of Device instances, one per unique response, deduplicated by host, MAC, and type. ### Parameters - **timeout** (int) - Optional - Seconds to wait for responses. Default: `10` (`DEFAULT_TIMEOUT`) - **local_ip_address** (Optional[str]) - Optional - Local interface IP to bind (useful on multi-homed hosts). Default: `None` - **discover_ip_address** (str) - Optional - Broadcast/unicast destination. Default: `"255.255.255.255"` - **discover_ip_port** (int) - Optional - Destination port. Default: `80` (`DEFAULT_PORT`) ### Returns `List[Device]` — one device instance per unique response (deduplicated by host + MAC + type). ### Request Example ```python import broadlink devices = broadlink.discover() for device in devices: device.auth() print(device.host, device.mac.hex()) ``` ```python from broadlink.const import DEFAULT_TIMEOUT try: devices = broadlink.discover( timeout=20, local_ip_address="192.168.0.100", discover_ip_address="192.168.0.255", ) print("Found %d devices" % len(devices)) except Exception as err: print("Discovery failed:", err) ``` ``` -------------------------------- ### CRC16.get_table usage example Source: https://github.com/mjg59/python-broadlink Shows how to obtain a CRC table for a polynomial using the get_table class method. ```python from broadlink.helpers import CRC16 table = CRC16.get_table(0xA001) ``` -------------------------------- ### get_ac_info usage example Source: https://github.com/mjg59/python-broadlink Example call to get_ac_info returning a dictionary with power and ambient_temp keys. ```python info = dev.get_ac_info() # {'power': 1, 'ambient_temp': 24.3} ``` -------------------------------- ### Check power consumption Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Check the power consumption of a smart plug. ```bash broadlink_cli --device @BEDROOM.device --energy ``` -------------------------------- ### update usage example Source: https://github.com/mjg59/python-broadlink Example of calling update() and printing the device name and lock status. ```python dev.update() print(dev.name, dev.is_locked) ``` -------------------------------- ### xdiscover(timeout, local_ip_address, discover_ip_address, discover_ip_port) Source: https://github.com/mjg59/python-broadlink Same parameters as discover(), but returns a generator that yields each device as soon as its response arrives. This allows processing devices incrementally as they respond. ```APIDOC ## xdiscover(timeout, local_ip_address, discover_ip_address, discover_ip_port) ### Description Same parameters as discover(), but returns a generator that yields each device as soon as its response arrives. This allows processing devices incrementally as they respond. ### Parameters - **timeout** (int) - Optional - Seconds to wait for responses. Default: `10` (`DEFAULT_TIMEOUT`) - **local_ip_address** (Optional[str]) - Optional - Local interface IP to bind (useful on multi-homed hosts). Default: `None` - **discover_ip_address** (str) - Optional - Broadcast/unicast destination. Default: `"255.255.255.255"` - **discover_ip_port** (int) - Optional - Destination port. Default: `80` (`DEFAULT_PORT`) ### Returns `Generator[Device, None, None]`. ### Request Example ```python import broadlink for device in broadlink.xdiscover(): print(device) # process devices as they respond ``` ``` -------------------------------- ### xdiscover() function signature Source: https://github.com/mjg59/python-broadlink Function signature for broadlink.xdiscover() which has the same parameters as discover() but returns a generator that yields devices instantly as they respond. ```python def xdiscover( timeout: int = DEFAULT_TIMEOUT, local_ip_address: Optional[str] = None, discover_ip_address: str = DEFAULT_BCAST_ADDR, discover_ip_port: int = DEFAULT_PORT, ) -> Generator[Device, None, None]: """Discover devices connected to the local network. This function returns a generator that yields devices instantly. """ ``` -------------------------------- ### Datetime.pack usage example Source: https://github.com/mjg59/python-broadlink Packs the current UTC time into a protocol timestamp. Requires importing datetime and the Datetime class from broadlink.protocol. ```python import datetime as dt from broadlink.protocol import Datetime packed = Datetime.pack(dt.datetime.now(dt.timezone.utc)) ``` -------------------------------- ### broadlink.setup Source: https://github.com/mjg59/python-broadlink Configures a device to connect to a Wi-Fi network. Requires SSID, password, and security mode. ```APIDOC ## broadlink.setup ### Description Configures a device to connect to a Wi-Fi network. Requires SSID, password, and security mode. ### Method Function call ### Parameters #### Arguments - **ssid** (str) - Required - Wi-Fi network name - **password** (str) - Required - Wi-Fi password - **security_mode** (int) - Required - Security mode (0=None, 1=WEP, 2=WPA1, 3=WPA2, 4=WPA1/2) #### Keyword Arguments - **ip_address** (str) - Optional - Broadcast IP address (default "255.255.255.255") ``` -------------------------------- ### Learn IR code and save to file Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Learn an IR code and save it to a file specified with --learnfile. ```bash broadlink_cli --device @BEDROOM.device --learnfile LG-TV.power ``` -------------------------------- ### Device instantiation with keyword arguments and auth Source: https://github.com/mjg59/python-broadlink Creates a Device instance using keyword arguments, including a hex string for mac, and then calls auth() to authenticate. ```python from broadlink import Device dev = Device( host=("192.168.0.16", 80), mac="aabbccddeeff", # hex string accepted devtype=0x2720, timeout=5, name="Kitchen plug", model="SP mini", manufacturer="Broadlink", is_locked=False, ) dev.auth() ``` -------------------------------- ### mp1.set_power_mask usage example Source: https://github.com/mjg59/python-broadlink Example call to set_power_mask on an mp1 device, turning on sockets 1 and 3 (bitmask 0b0101). ```python dev.set_power_mask(0b0101, True) # sockets 1 and 3 on ``` -------------------------------- ### check_sensors_raw usage example for a2 Source: https://github.com/mjg59/python-broadlink Example of calling check_sensors_raw() on an a2 device and printing pm2_5 and temperature readings. ```python raw = dev.check_sensors_raw() print(raw["pm2_5"], raw["temperature"]) ``` -------------------------------- ### check_sensors usage example Source: https://github.com/mjg59/python-broadlink Example of calling check_sensors() on a device, returning a dictionary with temperature, humidity, and qualitative levels. ```python data = dev.check_sensors() # {'temperature': 22.5, 'humidity': 41.0, 'light': 'normal', # 'air_quality': 'good', 'noise': 'quiet'} ``` -------------------------------- ### set_schedule usage example Source: https://github.com/mjg59/python-broadlink Example call with weekday list of 6 identical dicts and weekend list of 2 dicts. ```python dev.set_schedule( weekday=[{"start_hour": 17, "start_minute": 30, "temp": 22}] * 6, weekend=[{"start_hour": 9, "start_minute": 0, "temp": 20}, {"start_hour": 17, "start_minute": 0, "temp": 22}], ) ``` -------------------------------- ### check_data usage example with error handling Source: https://github.com/mjg59/python-broadlink Example of calling check_data() and handling the ReadError exception when no code has been captured yet. ```python from broadlink.exceptions import ReadError, StorageError try: packet = dev.check_data() except ReadError: print("No code captured yet") ``` -------------------------------- ### Discover devices on local network Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Discover Broadlink devices connected to the local network. ```bash broadlink_discovery ``` -------------------------------- ### CRC16 algorithm examples Source: https://github.com/mjg59/python-broadlink Demonstrates one-shot calculation with default ANSI polynomial and standard init value, and chaining over multiple segments using a custom init value. ```python from broadlink.helpers import CRC16 # One-shot: default ANSI polynomial, standard init value crc = CRC16.calculate(bytes([0x01, 0x03, 0x00, 0x00, 0x00, 0x08])) # Custom init value (e.g. chaining over multiple segments) crc = CRC16.calculate(first_segment, init_value=0xFFFF) crc = CRC16.calculate(second_segment, init_value=crc) ``` -------------------------------- ### set_advanced usage example Source: https://github.com/mjg59/python-broadlink Example call to set_advanced with loop_mode=2, sensor=0, osv=42, dif=2, svh=35, svl=5, adj=-0.5, fre=0, poweron=0. Returns None. ```python dev.set_advanced(loop_mode=2, sensor=0, osv=42, dif=2, svh=35, svl=5, adj=-0.5, fre=0, poweron=0) ``` -------------------------------- ### Discover devices with broadlink.discover Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Discover devices on the local network. ```python3 devices = broadlink.discover() ``` -------------------------------- ### Check nightlight state Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Check the nightlight state of a smart plug. ```bash broadlink_cli --device @BEDROOM.device --checknl ``` -------------------------------- ### Direct discovery with broadlink.hello Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Use unicast discovery for locked devices that may not respond to broadcast. ```python3 device = broadlink.hello('192.168.0.16') ``` -------------------------------- ### broadlink.DEFAULT_TIMEOUT Source: https://github.com/mjg59/python-broadlink Constant for the default timeout. Re-exported from broadlink/const.py. ```APIDOC ## broadlink.DEFAULT_TIMEOUT ### Description Default timeout constant. ### Type Constant ``` -------------------------------- ### Specify device with single argument Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Pass all device information as a single argument in the format 'type host mac'. ```bash broadlink_cli --device "0x2712 1.1.1.1 aaaaaaaaaa" --temp ``` -------------------------------- ### broadlink.DEFAULT_PORT Source: https://github.com/mjg59/python-broadlink Constant for the default port. Re-exported from broadlink/const.py. ```APIDOC ## broadlink.DEFAULT_PORT ### Description Default port constant. ### Type Constant ``` -------------------------------- ### broadlink.DEFAULT_BCAST_ADDR Source: https://github.com/mjg59/python-broadlink Constant for the default broadcast address. Re-exported from broadlink/const.py. ```APIDOC ## broadlink.DEFAULT_BCAST_ADDR ### Description Default broadcast address constant. ### Type Constant ``` -------------------------------- ### Turn On Hub Subdevice (LC-1 Switch) Source: https://github.com/mjg59/python-broadlink/blob/master/README.md Use set_state with pwr, pwr1, and pwr2 set to 1 to turn on an LC-1 switch sub-device. ```python3 device.set_state(did="00000000000000000000a043b0d0783a", pwr=1) device.set_state(did="00000000000000000000a043b0d0783a", pwr1=1) device.set_state(did="00000000000000000000a043b0d0783a", pwr2=1) ``` -------------------------------- ### broadlink.sp4b Source: https://github.com/mjg59/python-broadlink Class for SP4B device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp4b ### Description Class representing an SP4B device. ### Method Class ### Signature `sp4b(...)` ``` -------------------------------- ### hello function definition Source: https://github.com/mjg59/python-broadlink Defines the `hello` function for direct device discovery. Useful if the device is locked. Raises `broadlink.exceptions.NetworkTimeoutError` (errno -4000) if no response within `timeout`. ```python def hello( ip_address: str, port: int = DEFAULT_PORT, timeout: int = DEFAULT_TIMEOUT, ) -> Device: """Direct device discovery. Useful if the device is locked. """ ``` -------------------------------- ### broadlink.sp2s Source: https://github.com/mjg59/python-broadlink Class for SP2S device. Re-exported from the broadlink package. ```APIDOC ## broadlink.sp2s ### Description Class representing an SP2S device. ### Method Class ### Signature `sp2s(...)` ``` -------------------------------- ### Check power state Source: https://github.com/mjg59/python-broadlink/blob/master/cli/README.md Check the power state of a smart plug. ```bash broadlink_cli --device @BEDROOM.device --check ``` -------------------------------- ### Using rmminib device Source: https://github.com/mjg59/python-broadlink Example usage of the rmminib device: create device, authenticate, enter learning mode, check data, and send data. ```python dev = broadlink.gendevice(0x5F36, host, mac) # RM mini 3 (new firmware) dev.auth() dev.enter_learning() packet = dev.check_data() dev.send_data(packet) ``` -------------------------------- ### broadlink.mp1s Source: https://github.com/mjg59/python-broadlink Class for MP1S device. Re-exported from the broadlink package. ```APIDOC ## broadlink.mp1s ### Description Class representing an MP1S device. ### Method Class ### Signature `mp1s(...)` ```