Last active
November 6, 2024 07:28
-
-
Save jailuthra/bfb8a1e1e31a434494cf4e54b8ae29ee to your computer and use it in GitHub Desktop.
Smarteefi Local (WLAN) Switch Control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import socket | |
| import binascii | |
| import argparse | |
| """This script sends a UDP payload to smarteefi switch, to turn it on/off""" | |
| """Reverse Engg. credits to https://github.com/abhilash0505/homebridge-smarteefi-local""" | |
| def send_payload(ip_address: str, port: int, hex_string: str) -> None: | |
| """Sends a payload to a specified IP address and port over UDP. | |
| Args: | |
| ip_address: The IP address of the target device. | |
| port: The port number to send the data to. | |
| hex_string: The hexadecimal string representing the payload. | |
| """ | |
| # Convert the hex string to binary data | |
| binary_data = binascii.unhexlify(hex_string) | |
| # Create a UDP socket | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| sock.settimeout(1) | |
| # Send the binary data to the specified address and port | |
| sock.sendto(binary_data, (ip_address, port)) | |
| try: | |
| data, addr = sock.recvfrom(1000) | |
| print(f"Got back {len(data)} bytes from {addr}") | |
| except socket.timeout: | |
| print("Socket timeout") | |
| # Close the socket | |
| sock.close() | |
| def prepare_payload(device_id: str, switch: int, state: bool) -> str: | |
| """Prepares a payload for a device. | |
| Args: | |
| device_id: The ID of the device. | |
| switch: The number of the switch to control. | |
| state: Whether to turn the switch on or off. | |
| Returns: | |
| The formatted payload string. | |
| """ | |
| # Convert device ID to hexadecimal | |
| device_id_hex = binascii.hexlify(device_id.encode('utf-8')).decode('utf-8') | |
| # Convert switch number to hexadecimal | |
| switch_hex = binascii.hexlify(bytes([2 ** switch])).decode('utf-8') | |
| # Determine the "turn on" hex value based on the state | |
| turn_on_hex = switch_hex if state else "00" | |
| # Format the payload string | |
| payload = "cc1010000000000000000000000000000000000000000000{}000000000000000000000000{}000000{}000000000000000000000000" | |
| payload = payload.format(device_id_hex, switch_hex, turn_on_hex) | |
| return payload | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Send payload to device") | |
| parser.add_argument("switch_num", type=int, help="Switch number") | |
| parser.add_argument("state", type=int, choices=[0, 1], help="Switch state (0 for off, 1 for on)") | |
| device_id = "seXXXXXXXXXX" # Replace with your device ID | |
| args = parser.parse_args() | |
| ip_address = "192.168.1.X" # Replace with the actual IP address | |
| port = 10201 # Replace with the actual port number | |
| hex_string = prepare_payload(device_id, args.switch_num, args.state) | |
| print(hex_string) | |
| send_payload(ip_address, port, hex_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment