Skip to content

Connecting

Niklas Linz edited this page Oct 3, 2026 · 1 revision

Connecting

Everything the client needs to reach a unit goes into a DeviceConfig.

DeviceConfig cfg = DeviceConfig.builder()
        .host("192.168.1.50")
        .deviceId(151732605161920L)
        .protocol(ProtocolVersion.V3)
        .token(System.getenv("MIDEA_TOKEN"))
        .key(System.getenv("MIDEA_KEY"))
        .promptTone(false)                 // no beep on every command
        .build();

try (MideaAirConditioner ac = new MideaAirConditioner(cfg)) {
    System.out.println(ac.refresh());
}

If you have a discovery result and a key, this is shorter:

DeviceConfig cfg = DeviceConfig.builder(deviceInfo).credentials(key).build();

Builder options

Option Default Meaning
host(String) required IP address or hostname
deviceId(long) required id from discovery
port(int) 6444 TCP port
protocol(ProtocolVersion) V3 V2 or V3. V1 is rejected.
token(String), key(String) hex strings, required for V3
credentials(DeviceKey) token and key in one call
promptTone(boolean) true the unit beeps when it receives a command, like with the remote
connectTimeout(Duration) 10 s TCP connect including the V3 handshake
responseTimeout(Duration) 5 s wait for the answer to one query or command
powerAnalysisMethod(int) 1 how the unit encodes energy figures, see below
temperatureRange(min, max) from the unit override the allowed set point range

build() throws IllegalArgumentException if host or id is missing, if V3 is selected without token and key, or if you pick V1.

Energy encoding

Units don't agree on how they encode energy and power. If the kWh or W figures look absurd, try another value:

Value Encoding
1 BCD (default)
2 binary, energy in 0.1 kWh
3 mixed, each byte 0 to 99
12 binary
101 BCD energy, binary power

These are the same values as Home Assistant's power_analysis_method option.

Response timeout

On the first refresh(), every optional query the unit ignores costs two response timeouts. Keep this short unless the unit is really slow to answer. See How it behaves.

Connection lifecycle

  • The client connects on first use. connect() is optional; call it if you want wrong credentials to fail early.
  • If the connection breaks during a call, the client reconnects once and retries before throwing.
  • close() stops polling and closes the socket. The client is AutoCloseable.
  • disconnect() closes the socket but keeps the client usable; the next call reconnects.
  • Use one client per unit. Many Wi-Fi modules accept only one TCP connection at a time.

Next: Reading status

Clone this wiki locally