【IoT】Home Assistant 集成 Aqara 空调伴侣

Posted by 西维蜀黍 on 2021-05-16, Last Modified on 2024-02-26
Model ID Model number Product name Shape
acpartner.v3 KTBL11LM Aqara Air Conditioning Companion square
lumi.acpartner.mcn02 KTBL03LM Xiaomi Air Conditioning Companion 2 square

Aqara Air Conditioning Companion

米家 App 中设置

  • 约克空调,ID 3203,第3个设置

基本控制

与 HA 集成

# 如果 HA 运行在容器中
$ docker exec -it homeassistant bash

先将Aqara 空调伴侣绑定到米家 App 中,自然地,能够通过米家 App 来控制空调。

获取米家设备 token。

Clone https://github.com/syssi/xiaomi_airconditioningcompanion/tree/master 放至 HA 的 custom_components 文件夹下。

$ curl -OL https://github.com/syssi/xiaomi_airconditioningcompanion/archive/refs/heads/master.zip; unzip master.zip

$ cd /config/; mkdir custom_components; mv /tmp/xiaomi_airconditioningcompanion-master/custom_components/* custom_components/

配置 HA 配置文件 configuration.yaml,增加以下部分:

climate:
  - platform: xiaomi_miio_airconditioningcompanion
    name: Aqara Air Conditioning Companion
    host: 192.168.18.116
    token: 9565b569c5c1ab58bd42b3634758556e
    target_sensor: sensor.temperature_158d0001f53706
    scan_interval: 60
    
# for debug
logger:
  default: warn
  logs:
    custom_components.xiaomi_miio_airconditioningcompanion: debug
    miio: debug

Reference

直接控制

$ mkdir miioPlayground
$ virtualenv miioPlayground -p python3
$ cd miioPlayground
$ source bin/activate

$ pip3 install python-miio
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e info
Model: lumi.acpartner.v3
Hardware version: MW300
Firmware version: 1.4.1_162

$ miiocli airconditioningcompanion --ip 192.168.18.112 --token 9565b569c5c1ab58bd42b3634758556e --help
Usage: miiocli airconditioningcompanion [OPTIONS] COMMAND [ARGS]...

Options:
  --ip TEXT     [required]
  --token TEXT  [required]
  --help        Show this message and exit.

Commands:
  info                Get miIO protocol information from the device.
  learn               Learn an infrared command.
  learn_result        Read the learned command.
  learn_stop          Stop learning of a infrared command.
  off                 Turn the air condition off by infrared.
  on                  Turn the air condition on by infrared.
  raw_command         Send a raw command to the device.
  send_command        Send a command to the air conditioner.
  send_configuration  partial(func, *args, **keywords) - new function with...
  send_ir_code        Play a captured command.
  status              Return device status.
  test_properties     Helper to test device properties.
  
$ miiocli airconditioningcompanion --ip 192.168.2.112 --token cef9d9980e4a83bfa17c963781930a64 status
Power: on
Load power: 0
Air Condition model: b'\x01\x05\x14G\x00\x002\x03\x01'
LED: False
Target temperature: 22 °C
Swing mode: SwingMode.On
Fan speed: FanSpeed.High
Mode: OperationMode.Cool

$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e on  
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e off

红外控制

# 红外学习
# 输入以下命令后,立刻用红外遥控器对着空调伴侣发送红外信号
# 比如,我的音响,切换到蓝牙(BT)
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e learn_result
Reading learned infrared commands
['FE000000000000000000000000070024224B003800AB00E201C403830F591388430000000000000000010101010101010101000000000000000001010101010101054206B3']

# 获取我的音响 切换到光纤(OPT)红外信号
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e learn_result
Reading learned infrared commands
['FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5']

# 获取我的音响 关机 红外信号
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e learn_result
Reading learned infrared commands
['FE0000000000000000000000000500222247003800AD01C4038613883200000000000000000101010101010101010001000101000000010001000001010414']

# 获取我的音响 开机 红外信号
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e learn_result
Reading learned infrared commands
['FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9']

# 发送红外信号,以切换到光纤
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e send_ir_code  "FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5" "FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5"
Sending the supplied infrared command
['ok']

# 发送红外信号,以切换到蓝牙
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e send_ir_code  "FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9" "FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9"

# 发送红外信号,以关机
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e send_ir_code "FE0000000000000000000000000500222247003800AD01C4038613883200000000000000000101010101010101010001000101000000010001000001010414" 
"FE0000000000000000000000000500222247003800AD01C4038613883200000000000000000101010101010101010001000101000000010001000001010414"

# 发送红外信号,以开机
$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e send_ir_code "FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9" "FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9"

配置 HA 配置文件 configuration.yaml

switch:
  - platform: template
    switches:
      switch_to_hivi_bluetooth:
        unique_id: aaabbbcccsw
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AB00E201C403830F591388430000000000000000010101010101010101000000000000000001010101010101054206B3
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5
      switch_to_hivi_optical:
        unique_id: switch_to_hivi_optical
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AB00E201C403830F591388430000000000000000010101010101010101000000000000000001010101010101054206B3
      turn_on_off_hivi:
        unique_id: turn_on_off_hivi
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE0000000000000000000000000500222247003800AD01C4038613883200000000000000000101010101010101010001000101000000010001000001010414

Verify 配置文件:

$ hass --script check_config -c .

重启 HA:

检查 switch 是否已经被正确添加:

添加对应 swtich:

然后,就可以在 Home App中看到这些新的 switch了。

总配置:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


climate:
  - platform: xiaomi_miio_airconditioningcompanion
    name: Aqara Air Conditioning Companion
    host: 192.168.18.116
    token: 9565b569c5c1ab58bd42b3634758556e
    target_sensor: sensor.temperature_158d0001f53706
    scan_interval: 60

switch:
  - platform: template
    switches:
      switch_to_hivi_bluetooth:
        unique_id: aaabbbcccsw
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AB00E201C403830F591388430000000000000000010101010101010101000000000000000001010101010101054206B3
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5
      switch_to_hivi_optical:
        unique_id: switch_to_hivi_optical
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003700AD00E401C403840F571388430000000000000000010101010101010101000100000000000001000101010101054206B5
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AB00E201C403830F591388430000000000000000010101010101010101000000000000000001010101010101054206B3
      turn_on_off:
        unique_id: turn_on_off
        turn_on:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE000000000000000000000000070024224B003800AD00E401C403830F5B1388430000000000000000010101010101010101000100010100000001000100000101054206B9
        turn_off:
          service: xiaomi_miio_airconditioningcompanion.climate_send_command
          target:
            entity_id: climate.aqara_air_conditioning_companion
          data:
            command: FE0000000000000000000000000500222247003800AD01C4038613883200000000000000000101010101010101010001000101000000010001000001010414

Xiaomi Air Conditioning Companion 2

通过 miiocli 控制

$ mkdir miioPlayground
$ virtualenv miioPlayground -p python3
$ cd miioPlayground
$ source bin/activate
$ pip3 install python-miio

$ miiocli airconditioningcompanion --ip 192.168.18.116 --token 9565b569c5c1ab58bd42b3634758556e info
Model: lumi.acpartner.mcn02
Hardware version: esp32
Firmware version: 2.1.8_0016

$ miiocli airconditioningcompanion --ip 192.168.18.112 --token 9565b569c5c1ab58bd42b3634758556e --help
Usage: miiocli airconditioningcompanionmcn02 [OPTIONS] COMMAND [ARGS]...

Options:
  --ip TEXT     [required]
  --token TEXT  [required]
  --model TEXT
  --help        Show this message and exit.

Commands:
  info             Get (and cache) miIO protocol information from the...
  off              Turn the air condition off by infrared.
  on               Turn the air condition on by infrared.
  raw_command      Send a raw command to the device.
  send_command     Send a command to the air conditioner.
  status           Return device status.
  test_properties  Helper to test device properties.
  
$ miiocli airconditioningcompanionmcn02 --ip 192.168.31.117 --token 7d55b761ed021c9e50b1d2e85f89f941 status
Power: on
Load power: 12
Target temperature: 30 °C
Swing mode: SwingMode.On
Fan speed: FanSpeed.Low
Mode: OperationMode.Heat

$ miiocli airconditioningcompanionmcn02 --ip 192.168.31.117 --token 7d55b761ed021c9e50b1d2e85f89f941 on
$ miiocli airconditioningcompanionmcn02 --ip 192.168.31.117 --token 7d55b761ed021c9e50b1d2e85f89f941 off

与 HA 集成

Clone https://github.com/syssi/xiaomi_airconditioningcompanion/tree/master 放至 HA 的 custom_components 文件夹下。

$ curl -OL https://github.com/EugeneLiu/xiaomi_airconditioningcompanionMCN02/archive/refs/heads/master.zip; unzip master.zip

$ cd /config/; mkdir custom_components; mv /tmp/xiaomi_airconditioningcompanion-master/custom_components/* custom_components/

配置 HA 配置文件 configuration.yaml,增加以下部分:

climate:
  - platform: xiaomi_miio_airconditioningcompanionmcn02
    name: xiaomi Air Conditioning Companion 2
    host: 192.168.31.117
    token: 7d55b761ed021c9e50b1d2e85f89f941
    target_sensor: sensor.temperature_158d0001f53706
    scan_interval: 60
    
    
# for debug
logger:
  default: warn
  logs:
    custom_components.xiaomi_miio_airconditioningcompanion: debug
    miio: debug

Reference

Xiaomi Air Conditioning Companion 2