Xiaomi Smart Plug 2 Bluetooth Gateway Edition Integration with Home Assistant

sh06y Posted on 2021-02-27 13,119 Views


 

[infobox title="2022.3 Update"] Xiaomi Miot Auto is now available for quick access, no complex configuration is required, seehttps://github.com/al-one/hass-xiaomi-miot/blob/master/README_zh.md [/infobox]

Before you start, use the Mijia APP to connect the socket to the Internet.

1. Get a device token

The traditional root method is not available, but I found a script on GitHub that can quickly get the device token.

Website:https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor

Directions:

downloadtoken_extractor.exe [bdbtn]https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor/raw/master/token_extractor.exe[/bdbtn], run it, enter your account and password as prompted to get the device token, and copy it for later use

2. Access homeassistant

[success]

[Updated on August 19, 2021] Method 1: Use xiaomi-miot (recommended)

1. Download https://github.com/al-one/hass-xiaomi-miot/archive/refs/heads/master.zip, copy the ./custom_components/xiaomi_miot folder to the /config/custom_components directory, and restart homeassistant.

2. Add in the homeassistant integration interface Xiaomi Miot Auto, select local mode, enter the socket token and IP, and confirm.

[/success]

Method 2: Use xiaomi_miio_raw (may not work)

Warehouse Address:https://github.com/syssi/xiaomi_raw

Installation method:

Download the xiaomi_miio_raw source code [bdbtn]https://wwa.lanzous.com/ij1F9m8nbba[/bdbtn], copy it to ./custom_components/, and restart homeassistant

Edit configuration.yaml and configure as follows

sensor:
  - platform: xiaomi_miio_raw
    name: server_plug # 自定义名称
    host: 192.168.0.43 # 插座IP
    token: "xxx" # 前面获取到的token
    max_properties: 10
    default_properties_getter: get_properties
    default_properties: 
      - "{'did': 'power', 'piid': 1, 'siid': 2}"       
      - "{'did': 'temperature', 'piid': 6, 'siid': 2}"       
      - "{'did': 'indicator_light', 'piid': 1, 'siid': 3}"       
      - "{'did': 'on_duration', 'piid': 1, 'siid': 4}"       
      - "{'did': 'off_duration', 'piid': 2, 'siid': 4}"       
      - "{'did': 'countdown', 'piid': 3, 'siid': 4}"       
      - "{'did': 'task_switch', 'piid': 4, 'siid': 4}"       
      - "{'did': 'countdown_info', 'piid': 5, 'siid': 4}"       
      - "{'did': 'power_consumption', 'piid': 1, 'siid': 5}"       
      - "{'did': 'electric_current', 'piid': 2, 'siid': 5}"       
      - "{'did': 'voltage', 'piid': 3, 'siid': 5}"       
      - "{'did': 'electric_power', 'piid': 6, 'siid': 5}"

After restarting, you can find the socket in the developer tools

Parameters such as power need to be further calculated before they can be inserted into the panel(Only the configuration of temperature and power is demonstrated here, the same applies to other parameters)

sensor:
  - platform: template
    sensors:
        server_plug_power: # 自定义
            unique_id: server_plug_power # 自定义
            unit_of_measurement: W # 单位
            value_template: "{{ state_attr('sensor.server_plug', 'electric_power') | int / 100 }}" # 计算
        server_plug_temp: # 自定义
            unique_id: server_plug_temp # 自定义
            unit_of_measurement: ℃ # 单位
            value_template: "{{ state_attr('sensor.server_plug', 'temperature')}}"

After restarting, you can find the parameters you just added in the panel

Finally, encapsulate a switch to control the socket switch

 

Reference Links: https://post.smzdm.com/p/az5g23x0/