/XiaomiGateway3

Control Zigbee, BLE and Mesh devices from Home Assistant with Xiaomi Gateway 3 (ZNDMWG03LM) on original firmware

Primary LanguagePython

Xiaomi Gateway 3 integration for Home Assistant

hacs_badge Donate Donate

Control Zigbee devices from Home Assistant with Xiaomi Gateway 3 (ZNDMWG03LM) on original firmware.

ATTENTION: The component does not work with new firmware versions. Don't update your gateway! Check supported firmwares.

Gateway support Zigbee 3, Bluetooth Mesh and HomeKit.

This method does not change the device firmware. Gateway continues to work with Mi Home and HomeKit.

Real user config with 3 Gateways

Thanks to @Serrj for instruction how to enable Telnet on this device.

Important: This component does not work with:

  • Xiaomi Gateway 2 (DGNWG02LM, lumi.gateway.v3) - use this component
  • Xiaomi Gateway EU (DGNWG05LM, lumi.gateway.mieu01)

Attention: The component is under active development. Breaking changes may appear.

Table of Contents

FAQ

Q. Can Xiaomi close support for this integration in firmware updates?
A. Yes, they can and are already doing it. But if you have a worked hub and you do not update it, it will continue to work. Component can block fw updates with Lock Firmware switch.

Q. Does this integration support hubs DGNWG02LM, DGNWG05LM, Aqara Hub...?
A. No. The integration only supports ZNDMWG03LM.

Q. Does this integration support Xiaomi Robot Vacuum, Xiaomi Philips Bulb...?
A. No. The integration does not support Xiaomi Wi-Fi devices.

Q. Does the integration work without internet?
A. Partially. The component connects to a hub on the local network. Zigbee devices work without internet. But adding new Zigbee devices to Mi Home requires Internet. Updating BLE device data may not work without Internet.

Q. Does the integration support non Xiaomi Zigbee devices?
A. Yes. There are two ways to connect third party Zigbee devices. The first method allows devices to work in both Mi Home and Hass at the same time. The second method (ZHA) disconnects the hub from Mi Home and only works with Hass. Both methods have a different set of supported devices. There is no exact supported list. Don't expect absolutely every device on the market to be supported in any of these methods.

Q. Will the Zigbee devices continue to work in Mi Home?
A. Yes. If you do not enable ZHA mode, the devices will continue to work in Mi Home. And you can use automation in both Mi Home and Hass.

Q. Do I need to receive a token or enable Telnet manually?
A. No. The token is obtained automatically using the login / password from the Mi Home account. Telnet turns on automatically using token.

Q. Should I open or solder the hub?
A. Depends on the firmware version. Read supported firmwares section.

Q. Should I use ZHA mode?
A. You decide. If all of your Zigbee devices are supported in Mi Home, it is best not to enable ZHA. If you have two hubs - you can use one of them in Mi Home mode, and the second in ZHA mode. Or you can also use the hub in Mi Home mode with Xiaomi devices and a Zigbee USB Dongle for other Zigbee devices.

Q. How many Zigbee devices does the hub support?
A. The hub can connect directly up to 32 battery-powered devices (end devices). And additionaly up to 26 powered devices (routers). Other devices on your network can work through routers. The maximum number of devices is unknown. Official Xiaomi documentation writes about 128 devices.

Q. Does the component support decoupled mode for switches?
A. Yes, but it needs to be turned on in the Mi Home app.

Q. Why does the two-button switch only have one entity action?
A. All button clicks are displayed in the status of that one entity.

Supported Firmwares

Attention: Starting from 2020.10, there is a high chance of buying any version of the gateway with "bad" firmware. Only soldering will be able to open Telnet.

If you have "bad" firmware and don't want to solder - try to use HomeKit Controller component. It has many limitations, but the popular Xiaomi Zigbee devices should work fine. BLE and Mesh devices won't work. 3rd party zigbee devices won't work. And you won't have many advanced features of this component.

Instruction for soldering: wiki.

Component can block fw updates with Lock Firmware switch. Mi Home app will continue to offer you update. But won't be able to install it. It should fail at 0%.

Xiaomi Gateway 3 firmware update lock

ZNDMWG03LM (Chinese version, US plug)

  • v1.4.4_0003 - factory firmware, supported, not recomended
  • v1.4.5_0012 - factory firmware, supported, not recomended
  • v1.4.5_0016 - safe to update, supported, not recomended
  • v1.4.6_0012 - safe to update, supported
  • v1.4.6_0030 - was available in summer 2020, now unavailable, supported
  • v1.4.6_0043 - factory firmware from 2020.10, need to solder
  • v1.4.7_0040 - need to solder
  • v1.4.7_0063 - factory firmware from 2020.11, need to solder
  • v1.4.7_0065 - need to solder
  • v1.4.7_0115 - need to solder

ZNDMWG02LM (Euro version, no plug)

  • v1.4.6_0043 - factory firmware, need to solder (telnet has a password)

Supported Zigbee Devices

Tested Devices:

  • Aqara Bulb (ZNLDP12LM)
  • Aqara Button (WXKG11LM)
  • Aqara Cube (MFKZQ01LM)
  • Aqara Curtain (ZNCLDJ11LM)
  • Aqara Curtain B1 (ZNCLDJ12LM)
  • Aqara Door Lock S1 (ZNMS11LM)
  • Aqara Door Lock S2 (ZNMS12LM)
  • Aqara Door Lock S2 Pro (ZNMS12LM)
  • Aqara Door Sensor (MCCGQ11LM)
  • Aqara Double Wall Button (WXKG02LM)
  • Aqara Double Wall Button D1 (WXKG07LM)
  • Aqara Double Wall Switch (QBKG03LM,QBKG12LM)
  • Aqara Double Wall Switch D1 (QBKG22LM,QBKG24LM)
  • Aqara Motion Sensor (RTCGQ11LM)
  • Aqara Opple Four Button (WXCJKG12LM)
  • Aqara Opple MX480 (XDD13LM)
  • Aqara Opple MX650 (XDD12LM)
  • Aqara Opple Six Button (WXCJKG13LM)
  • Aqara Opple Two Button (WXCJKG11LM)
  • Aqara Precision Motion Sensor (RTCGQ13LM)
  • Aqara Relay (LLKZMK11LM)
  • Aqara Relay T1 (SSM-U01,SSM-U02)
  • Aqara Roller Shade (ZNGZDJ11LM)
  • Aqara Shake Button (WXKG12LM)
  • Aqara Single Wall Button (WXKG03LM)
  • Aqara Single Wall Button D1 (WXKG06LM)
  • Aqara Single Wall Switch (QBKG04LM,QBKG11LM)
  • Aqara Single Wall Switch D1 (QBKG21LM,QBKG23LM)
  • Aqara Socket (QBCZ11LM)
  • Aqara TH Sensor (WSDCGQ11LM,WSDCGQ12LM)
  • Aqara Thermostat S2 (KTWKQ03ES)
  • Aqara Triple Wall Switch D1 (QBKG25LM,QBKG26LM)
  • Aqara Vibration Sensor (DJT11LM)
  • Aqara Water Leak Sensor (SJCGQ11LM)
  • Honeywell Gas Sensor (JTQJ-BF-01LM/BW)
  • Honeywell Smoke Sensor (JTYJ-GD-01LM/BW)
  • IKEA Bulb E14 (LED1649C5)
  • IKEA Bulb E14 400 lm (LED1536G5)
  • IKEA Bulb E27 1000 lm (LED1623G12)
  • IKEA Bulb E27 950 lm (LED1546G12)
  • IKEA Bulb E27 980 lm (LED1545G12)
  • IKEA Bulb GU10 400 lm (LED1537R6,LED1650R5)
  • Xiaomi Button (WXKG01LM)
  • Xiaomi Door Sensor (MCCGQ01LM)
  • Xiaomi Light Sensor (GZCGQ01LM)
  • Xiaomi Motion Sensor (RTCGQ01LM)
  • Xiaomi Plug (ZNCZ02LM)
  • Xiaomi Plug EU (ZNCZ04LM)
  • Xiaomi Plug TW (ZNCZ03LM)
  • Xiaomi Plug US (ZNCZ12LM)
  • Xiaomi TH Sensor (WSDCGQ01LM)

Supported BLE Devices

Video DEMO

Xiaomi Rubik's Cube

Tested Devices:

  • Aqara Door Lock N100 (ZNMS16LM)
  • Aqara Door Lock N200 (ZNMS17LM)
  • Xiaomi Alarm Clock (CGD1)
  • Xiaomi Door Sensor 2 (MCCGQ02HL)
  • Xiaomi Flower Care (HHCCJCY01)
  • Xiaomi Magic Cube (XMMF01JQD) - don't sends edge info, only direction!
  • Xiaomi Mosquito Repellent (WX08ZM)
  • Xiaomi Motion Sensor 2 (RTCGQ02LM)
  • Xiaomi Night Light 2 (MJYD02YL-A)
  • Xiaomi Qingping TH Sensor (CGG1)
  • Xiaomi Safe Box (BGX-5/X1-3001)
  • Xiaomi TH Clock (LYWSD02MMC)
  • Xiaomi TH Sensor (LYWSDCGQ/01ZM)
  • Xiaomi TH Sensor 2 (LYWSD03MMC)
  • Xiaomi ZenMeasure Clock (MHO-C303)
  • Yeelight Button S1 (YLAI003)

Other BLE devices also maybe supported...

Kettles and scooters are not BLE devices. It is not known whether the gateway can work with them. Currently not supported.

BLE devices and their attributes don't appear immediately! Data collected and stored at the gateway. After rebooting Hass - data restored from the gateway. Rebooting the gateway will clear the saved data!

Supported Bluetooth Mesh Devices

Tested Devices:

  • Xiaomi Mesh Bulb (MJDP09YL)
  • Xiaomi Mesh Downlight (MJTS01YL)
  • Yeelight Mesh Bulb E14 (YLDP09YL)
  • Yeelight Mesh Bulb E27 (YLDP10YL)
  • Yeelight Mesh Bulb M2 (YLDP25YL/YLDP26YL)
  • Yeelight Mesh Downlight (YLSD01YL)
  • Yeelight Mesh Downlight M2 (YLTS02YL/YLTS04YL)
  • Yeelight Mesh Spotlight (YLSD04YL)

Other Mesh devices also maybe supported...

Install

Video DEMO

Xiaomi Gateway 3 control from Home Assistant

You can install component with HACS custom repo (example): AlexxIT/XiaomiGateway3.

Or manually copy xiaomi_gateway3 folder from latest release to custom_components folder in your config folder.

Config

Video DEMO

Mi Cloud authorization in Home Assistant with Xiaomi Gateway 3

With GUI. Configuration > Integration > Xiaomi Gateway 3.

If the integration is not in the list, you need to clear the browser cache.

Zigbee and BLE performance table

  1. To enable stats sensors go to:

    Configuration > Integrations > Xiaomi Gateway 3 > Options > Zigbee and BLE performance data

    Optional you can enable pereodical updates about device parent routers.

    Configuration > Integrations > Xiaomi Gateway 3 > Options > Parent devices in stats: Hourly

  2. Install Flex Table from HACS

  3. Add new Lovelace tab with Panel Mode

  4. Add new Lovelace card:

How it works:

  • for each Zigbee and BLE device, a sensor will be created with the time of receiving the last message from this sensor
  • there will also be a lot of useful information in the sensor attributes
  • for the Gateway, the sensor state shows the uptime of the gateway connection, so you can check the stability of your Wi-Fi
  • the uptime in gateway sensor attributes means time after reboot gateway
  • the msg_missed may not always show correct data if you reboot the gate or device
  • dash in the type means that the device is not directly connected to the hub
  • the parent can be updated within a few hours

Advanced config

Support custom occupancy timeout for motion sensor and invert state for door sensor (for DIY purposes).

Config through built-in customizing UI or YAML.

Xiaomi Gateway 3 occupancy timeout settings in Home Assistant

It's important to add these lines to your configuration.yaml. Otherwise, changes to the UI will not be read when you restart Home Assistant.

homeassistant:
  customize: !include customize.yaml

To enable customizing UI, you need to enable Advanced Mode in your user profile.

Occupancy timeout for moving sensor.

  • a simple timer starts every time a person moves
  • the progressive timer starts with a new value with each new movement of the person, the more you move - the longer the timer
  • fast back timer starts with doubled value if the person moves immediately after the timer is off
# /config/customize.yaml
binary_sensor.0x158d0003456789_motion:
  occupancy_timeout: 180  # simple mode
binary_sensor.0x158d0003456788_motion:
  occupancy_timeout: -120  # fast back mode
binary_sensor.0x158d0003456787_motion:
  occupancy_timeout: [-120, 240, 300]  # progressive timer
binary_sensor.0x158d0003456786_motion:
  occupancy_timeout: 1  # for hacked 5 sec sensors

Invert state for contact sensor.

# /config/customize.yaml
binary_sensor.0x158d0003456789_contact:
  invert_state: 1  # any non-empty value will reverse the logic

Ignore offline device status.

# /config/customize.yaml
switch.0x158d0003456789_switch:
  ignore_offline: 1  # any non-empty value

Add and remove Zigbee devices

To enter the pairing mode, turn on the switch Xiaomi Gateway 3 Pair. Pairing lasts 60 seconds.

After successfully adding the device, the Gateway will sound two long beeps.

If the addition was unsuccessful, for example, an unsupported device, the Gateway will sound three short beeps.

To delete a device from Hass and from Gateway - you need to rename device to delete. Just the device, not its objects!

Add third-party Zigbee devices

Video DEMO

Control non Xiaomi zigbee devices from Xiaomi Gateway 3

Attention 1: Only devices similar to Xiaomi devices will work!

Attention 2: After the first pairing, Mi Home remembers the selected device model. And with the next pairings, it will show old interface, even if you change the model. Hass will take the new device model on the next pairings.

To add a custom device, you need to call the service remote.send_command with params:

entity_id: remote.0x680ae2fffe123456_pair  # change to your Gateway remote
command: pair
device: ikea.light.led1623g12  # change to your device model

You need to choose the most similar Xiaomi model for your device from this file.

For example, for a lamp or dimmer - choose an IKEA lamp ikea.light.led1623g12.

Sometimes it doesn't work the first time and you need to try pairing again.

The devices added in this way will work even after the Gateway is restarted. They will continue to work without Hass. And they can be used in Mi Home automations.

You can discuss the feature here.

Zigbee Home Automation Mode

Video DEMO

Zigbee Home Automation (ZHA) with Xiaomi Gateway 3 on original firmware without soldering

Zigbee Home Automation (ZHA) is a standard Home Assistant component for managing Zigbee devices. It works with various radio modules such as CC2531, Conbee II, Tasmoted Sonoff ZBBridge and others.

Important: ZHA component is in early development stage. Don't expect it to work well with all devices.

Attention: ZHA mode cannot work simultaneously with Mi Home!

When you turn on ZHA mode - zigbee devices in Mi Home will stop working. Bluetooth devices will continue to work.

To switch the mode go to:

Configuration > Integrations > Xiaomi Gateway 3 > Options > Mode

Zigbee devices will not migrate from Mi Home to ZHA. You will need to pair them again with ZHA.

You can change the operating mode at any time. Your gateway firmware does not change! Just reboot the gateway and it is back in stock.

Thanks to @zvldz for help with socat.

Handle Button Actions

Video DEMO

Handling Zigbee buttons with Xiaomi Gateway 3 in Home Assistant

Buttons, vibration sensor, cube, locks and other - create an action entity. The entity changes its state for a split second and returns to an empty state. The attributes contain useful data, they are not cleared after the event is triggered.

Depending on the button model, its state may be:

  • single button: single, double, triple, quadruple, many, hold, release, shake
  • double button: button_1_single, button_2_single, button_both_single, etc.
  • triple button: button_1_single, button_12_single, button_23_single, etc.

Your button may not have all of these options! Check available values in action-sensor attributes when you interact with button.

automation:
- alias: Turn off all lights
  trigger:
  - platform: state
    entity_id: sensor.0x158d0002fa99fd_action  # change to your button
    to: button_1_single  # change to your button state
  action:
  - service: light.turn_off
    entity_id: all
  mode: single

Handle BLE Locks

BLE locks have an action entity, just like buttons.

The state changes to door, lock, fingerprint, armed when an event occurs. Details of the event are in the entity attributes.

action: fingerprint

  • key_id - Key ID in full hex format
  • action_id: 0, message: Match successful
  • action_id: 1, message: Match failed
  • action_id: 2, message: Timeout
  • action_id: 3, message: Low quality
  • action_id: 4, message: Insufficient area
  • action_id: 5, message: Skin is too dry
  • action_id: 5, message: Skin is too wet

action: door

  • action_id: 0, message: Door is open
  • action_id: 1, message: Door is closed
  • action_id: 2, message: Timeout is not closed
  • action_id: 3, message: Knock on the door
  • action_id: 4, message: Breaking the door
  • action_id: 5, message: Door is stuck

action: lock

  • key_id - Key ID in short decimal format
  • action_id: 0, message: Unlock outside the door
  • action_id: 1, message: Lock
  • action_id: 2, message: Turn on anti-lock
  • action_id: 3, message: Turn off anti-lock
  • action_id: 4, message: Unlock inside the door
  • action_id: 5, message: Lock inside the door
  • action_id: 6, message: Turn on child lock
  • action_id: 7, message: Turn off child lock
  • method_id: 0, method: bluetooth
  • method_id: 1, method: password
  • method_id: 2, method: biological
  • method_id: 3, method: key
  • method_id: 4, method: turntable
  • method_id: 5, method: nfc
  • method_id: 6, method: one-time password
  • method_id: 7, method: two-step verification
  • method_id: 8, method: coercion
  • method_id: 10, method: manual
  • method_id: 11, method: automatic
  • key_id: 0xc0de0000, error: Frequent unlocking with incorrect password
  • key_id: 0xc0de0001, error: Frequent unlocking with wrong fingerprints
  • key_id: 0xc0de0002, error: Operation timeout (password input timeout)
  • key_id: 0xc0de0003, error: Lock picking
  • key_id: 0xc0de0004, error: Reset button is pressed
  • key_id: 0xc0de0005, error: The wrong key is frequently unlocked
  • key_id: 0xc0de0006, error: Foreign body in the keyhole
  • key_id: 0xc0de0007, error: The key has not been taken out
  • key_id: 0xc0de0008, error: Error NFC frequently unlocks
  • key_id: 0xc0de0009, error: Timeout is not locked as required
  • key_id: 0xc0de000a, error: Failure to unlock frequently in multiple ways
  • key_id: 0xc0de000b, error: Unlocking the face frequently fails
  • key_id: 0xc0de000c, error: Failure to unlock the vein frequently
  • key_id: 0xc0de000d, error: Hijacking alarm
  • key_id: 0xc0de000e, error: Unlock inside the door after arming
  • key_id: 0xc0de000f, error: Palmprints frequently fail to unlock
  • key_id: 0xc0de0010, error: The safe was moved
  • key_id: 0xc0de1000, error: The battery level is less than 10%
  • key_id: 0xc0de1001, error: The battery is less than 5%
  • key_id: 0xc0de1002, error: The fingerprint sensor is abnormal
  • key_id: 0xc0de1003, error: The accessory battery is low
  • key_id: 0xc0de1004, error: Mechanical failure

Write me if the values are wrong somewhere. I translated from Chinese documentation.

Example of several automations:

automation:
- alias: Doorbell
  trigger:
    platform: state
    entity_id: sensor.ble_1010274797_action
    to: door
  condition:
    condition: template
    value_template: "{{ trigger.to_state.attributes['action_id'] == 3 }}"
  action:
    service: persistent_notification.create
    data_template:
      title: Doorbell
      message: The doorbell is ringing

- alias: Lock Error
  trigger:
    platform: state
    entity_id: sensor.ble_1010274797_action
    to: lock
  condition:
    condition: template
    value_template: "{{ trigger.to_state.attributes['error'] }}"
  action:
    service: persistent_notification.create
    data_template:
      title: Lock ERROR
      message: "{{ trigger.to_state.attributes['error'] }}"

- alias: Open lock
  trigger:
    platform: state
    entity_id: sensor.ble_1010274797_action
    to: lock
  condition:
    condition: template
    value_template: "{{ trigger.to_state.attributes['action_id'] == 0 }}"
  action:
    service: persistent_notification.create
    data_template:
      title: Lock is open
      message: |
        Opening method: {{ trigger.to_state.attributes['method'] }}
        User ID: {{ trigger.to_state.attributes['key_id'] }}

Obtain Mi Home device token

Video DEMO

Mi Cloud authorization in Home Assistant with Xiaomi Gateway 3

You can use this integration to get a token for any of your Xiaomi devices. You don't need to have Xiaomi Gateway 3. Just install and add the integration, enter the username / password from your Mi Home account. And use the integration settings to view your account's device tokens.

Disable Buzzer

If you have a hacked motion sensor, the gateway will beep periodically.

The gateway has an application that handle the button, LED and beeper. This option can turn off this application.

Attention: I don't know what else this app does and will the gateway work fine without it.

To disable buzzer:

Configuration > Integrations > Xiaomi Gateway 3 > Options > Disable buzzer

Advanced commands

Reboot Gateway

script:
  reboot_gateway:
    sequence:
    - service: remote.send_command
      entity_id: remote.0x0123456789abcdef_pair  # change to your gateway
      data:
        command: reboot

Attention: I don’t know if it’s safe to change the channel and power of the gateway. Use at your own risk.

The information in the attributes of the pair object is updated within a minute!

Change Zigbee channel

command: channel 15  # I saw values: 11, 15, 20, 25

Change Zigbee TX power

command: power 7  # I saw values: 0, 7, 30

How it works

The component enables Telnet on Gateway via Miio protocol. Only this Gateway supports this command. Do not try to execute it on other Xiaomi/Aqara Gateways.

The component starts the MQTT Server on the public port of the Gateway. All the logic in the Gateway runs on top of the built-in MQTT Server. By default, access to it is closed from the outside.

ATTENTION: Telnet and MQTT work without a password! Do not use this method on public networks.

After rebooting the device, all changes will be reset. The component will launch Telnet and public MQTT every time it detects that they are disabled.

Debug mode

Component support debug mode. Shows only component logs. The link to the logs is always random and will apear in Notifications.

Configuration > Integrations > Xiaomi Gateway 3 > Options > Debug

You can filter data in the logs, enable auto refresh (in seconds) and tail last lines.

http://192.168.1.123:8123/c4e99cfc-0c83-4a39-b7f0-278b0e719bd1?q=ble_event&r=2&t=100