miio.miioprotocol module

miIO protocol implementation.

This module contains the implementation of routines to send handshakes, send commands and discover devices (MiIOProtocol).

class miio.miioprotocol.MiIOProtocol(ip: Optional[str] = None, token: Optional[str] = None, start_id: int = 0, debug: int = 0, lazy_discover: bool = True, timeout: int = 5)[source]

Bases: object

static discover(addr: Optional[str] = None, timeout: int = 5) Any[source]

Scan for devices in the network. This method is used to discover supported devices by sending a handshake message to the broadcast address on port 54321. If the target IP address is given, the handshake will be send as an unicast packet.

Parameters:

addr (str) – Target IP address

send(command: str, parameters: Optional[Any] = None, retry_count: int = 3, *, extra_parameters: Optional[Dict] = None) Any[source]

Build and send the given command. Note that this will implicitly call send_handshake() to do a handshake, and will re-try in case of errors while incrementing the _id by 100.

Parameters:
  • command (str) – Command to send

  • parameters (dict) – Parameters to send, or an empty list

  • retry_count – How many times to retry in case of failure, how many handshakes to send

  • extra_parameters (dict) – Extra top-level parameters

Raises:

DeviceException – if an error has occurred during communication.

send_handshake(*, retry_count=3) <Struct>[source]

Send a handshake to the device.

This returns some information, such as device type and serial, as well as device’s timestamp in response.

The handshake must also be done regularly to enable communication with the device.

Raises:

DeviceException – if the device could not be discovered after retries.

property raw_id