본문 바로가기
IoT

HomeAssistant 샤오미 공기청정기 / 선풍기 연동

by ㅋㅋ잠자 2020. 7. 2.
반응형

안녕하세요? 백업 차원해서 샤오미 공기청정기, 선풍기 설정기를 남깁니다.


물론 제품이 다르기 때문에 개발자 도구에서 기기 이름을 보고 설정하시길 바랍니다.


자동화 부분을 저장하면 자동화가 에러가 나버리는 문제 때문에 조금 최신? 버전에서 새로 자동화를 구성한 코드입니다.


본 코드가 실제로 공기청정기에 물리적으로 스위치로 모드를 변경했을 경우, HA에 반영이 될 수도 안 될 수도 있습니다..
(요즘 공기청정기에 손을 안대고 있어서..)






1. 선풍기 커스텀 컴포넌트


https://github.com/syssi/xiaomi_fan





2. 샤오미 공기 청정기


1. miio 키값 추출 후 등록


fan:

  # Xiaomi Air Purifier 2S

  - platform: xiaomi_miio

    host: 192.168.0.5

    token: 

    name: "공기청정기"


2. 센서 등록


sensors:

- platform: template

  sensors:

    xiaomi_airpurifier_temp:

      friendly_name: 온도

      value_template: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''temperature'')

        }}'

      unit_of_measurement: °C

      device_class: temperature

    xiaomi_airpurifier_humidity:

      friendly_name: 습도

      value_template: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''humidity'')

        }}'

      unit_of_measurement: '%'

      device_class: humidity

    xiaomi_airpurifier_air_quality_pm25:

      friendly_name: 공기질(PM2.5)

      value_template: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''aqi'') }}'

      unit_of_measurement: μg/m³

      icon_template: mdi:weather-fog

    xiaomi_airpurifier_speed:

      friendly_name: 팬속도

      value_template: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''motor_speed'')

        }}'

      unit_of_measurement: rpm

      icon_template: mdi:speedometer

    xiaomi_airpurifier_filter_remaining:

      friendly_name: 필터잔량

      value_template: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''filter_life_remaining'')

        }}'

      unit_of_measurement: '%'

      icon_template: mdi:heart-outline


3. 스위치 등록


switchs:

- platform: template

  switches:

    xiaomi_airpurifier_led:

      friendly_name: LED

      value_template: '{{ is_state_attr(''fan.xiaomi_air_purifier_2s'', ''led'', true)

        }}'

      turn_on:

        service: xiaomi_miio.fan_set_led_on

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      turn_off:

        service: xiaomi_miio.fan_set_led_off

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      icon_template: mdi:lightbulb-outline

    xiaomi_airpurifier_child_lock:

      friendly_name: 잠금

      value_template: '{{ is_state_attr(''fan.xiaomi_air_purifier_2s'', ''child_lock'',

        true) }}'

      turn_on:

        service: xiaomi_miio.fan_set_child_lock_on

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      turn_off:

        service: xiaomi_miio.fan_set_child_lock_off

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      icon_template: mdi:lock-outline

    xiaomi_airpurifier_buzzer:

      friendly_name: 부저

      value_template: '{{ is_state_attr(''fan.xiaomi_air_purifier_2s'', ''buzzer'',

        true) }}'

      turn_on:

        service: xiaomi_miio.fan_set_buzzer_on

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      turn_off:

        service: xiaomi_miio.fan_set_buzzer_off

        data:

          entity_id: fan.xiaomi_air_purifier_2s

      icon_template: mdi:volume-high

    xiaomi_fan_child_lock:
      friendly_name: 잠금
      value_template: '{{ is_state_attr(''fan.seonpunggi'', ''child_lock'', true)
        }}'
      turn_on:
        service: fan.xiaomi_miio_set_child_lock_on
        data:
          entity_id: fan.seonpunggi
      turn_off:
        service: fan.xiaomi_miio_set_child_lock_off
        data:
          entity_id: fan.seonpunggi
      icon_template: mdi:lock-outline


4. 공기청정기 모드 / 선호 속도 조절 인풋 옵션 넘버 생성


input_select:

  # Xiaomi Air Purifier 2S

  xiaomi_airpurifier_mode:

    name: Mode

    options:

      - Auto

      - Silent

      - Favorite

    icon: "mdi:animation-outline"


input_number:
  # Xiaomi Air Purifier 2S
  xiaomi_airpurifier_favorite_level:
    name: "속도조절"
    initial: 0
    min: 0
    max: 14
    step: 1
    icon: "mdi:weather-windy"


5. 상기 인풋 옵션, 인풋 넘버 값 지정 자동화 생성


1) 공기청정기 값이 변하면 인풋 옵션, 넘버를 초기화 하는 자동화 생성


automations:

- alias: Air Purifier mode changed

  trigger:

  - entity_id: fan.xiaomi_air_purifier_2s

    platform: state

  action:

  - data_template:

      option: '{{ state_attr(''fan.xiaomi_air_purifier_2s'', ''speed'') }}'

    entity_id: input_select.xiaomi_airpurifier_mode

    service: input_select.select_option


- alias: Air Purifier favorite level changed
  trigger:
    platform: state
    entity_id: fan.xiaomi_air_purifier_2s
  action:
    service: input_number.set_value
    entity_id: input_number.xiaomi_airpurifier_favorite_level
    data_template:
      value: '{{ state_attr(''fan.xiaomi_air_purifier_2s'',''favorite_level'') | int }}'


2) 인풋 옵션, 넘버가 바뀌면 공기청정기를 세팅하는 자동화 생성


automations:

- alias: Air Purifier mode change

  trigger:

    entity_id: input_select.xiaomi_airpurifier_mode

    platform: state

  action:

    service: fan.set_speed

    data_template:

      entity_id: fan.xiaomi_air_purifier_2s

      speed: '{{ states(''input_select.xiaomi_airpurifier_mode'')  }}'


- alias: Air Purifier favorite level change
  trigger:
    entity_id: input_number.xiaomi_airpurifier_favorite_level
    platform: state
  action:
    service: xiaomi_miio.fan_set_favorite_level
    data_template:
      entity_id: fan.xiaomi_air_purifier_2s
      level: '{{ states(''input_number.xiaomi_airpurifier_favorite_level'') | int }}'





3. 샤오미 선풍기


1. miio 키값 추출 후 등록


fan:

  # Xiaomi Fan

  - platform: xiaomi_miio_fan

    host: 192.168.0.65

    token: 

    name: "선풍기"


2. 센서 등록


sensors:

- platform: template

  sensors:

    xiaomi_fan_speed:

      friendly_name: 팬속도

      value_template: '{{ state_attr(''fan.seonpunggi'', ''raw_speed'') }}'

      unit_of_measurement: rpm

      icon_template: mdi:speedometer


3. 스위치 등록


왼쪽 오른쪽 버튼은 조금 효율성에 문제가 있습니다. 되기는 합니다.


switchs:

- platform: template

  switches:

    xiaomi_fan_child_lock:

      friendly_name: 잠금

      value_template: '{{ is_state_attr(''fan.seonpunggi'', ''child_lock'', true)

        }}'

      turn_on:

        service: fan.xiaomi_miio_set_child_lock_on

        data:

          entity_id: fan.seonpunggi

      turn_off:

        service: fan.xiaomi_miio_set_child_lock_off

        data:

          entity_id: fan.seonpunggi

      icon_template: mdi:lock-outline

    xiaomi_fan_buzzer:

      friendly_name: 부저

      value_template: '{{ is_state_attr(''fan.seonpunggi'', ''buzzer'', true) }}'

      turn_on:

        service: fan.xiaomi_miio_set_buzzer_on

        data:

          entity_id: fan.seonpunggi

      turn_off:

        service: fan.xiaomi_miio_set_buzzer_off

        data:

          entity_id: fan.seonpunggi

      icon_template: mdi:volume-high

    xiaomi_fan_oscillate:
      friendly_name: 회전
      value_template: '{{ is_state_attr(''fan.seonpunggi'', ''oscillating'', true)
        }}'
      turn_on:
        service: fan.oscillate
        data:
          entity_id: fan.seonpunggi
          oscillating: true
      turn_off:
        service: fan.oscillate
        data:
          entity_id: fan.seonpunggi
          oscillating: false
      icon_template: mdi:rotate-3d
    xiaomi_fan_natural:
      friendly_name: 자연풍
      value_template: '{{ state_attr(''fan.seonpunggi'', ''natural_speed'') != 0 }}'
      turn_on:
        service: fan.xiaomi_miio_set_natural_mode_on
        data:
          entity_id: fan.seonpunggi
      turn_off:
        service: fan.xiaomi_miio_set_natural_mode_off
        data:
          entity_id: fan.seonpunggi
      icon_template: mdi:sprout
    xiaomi_fan_oscillate_left:
      friendly_name: 왼쪽
      value_template: '{{ true }}'
      turn_on:
        service: fan.set_direction
        data:
          entity_id: fan.seonpunggi
          direction: left
      turn_off:
        service: fan.set_direction
        data:
          entity_id: fan.seonpunggi
          direction: left
      icon_template: mdi:arrow-expand-left
    xiaomi_fan_oscillate_right:
      friendly_name: 오른쪽
      value_template: '{{ true }}'
      turn_on:
        service: fan.set_direction
        data:
          entity_id: fan.seonpunggi
          direction: right
      turn_off:
        service: fan.set_direction
        data:
          entity_id: fan.seonpunggi
          direction: right
      icon_template: mdi:arrow-expand-right


4. 선풍기 각도 / 속도 조절 버튼 빠 생성


input_select:

  xiaomi_fan_angle:

    name: angle

    options:

      - 30

      - 60

      - 90

      - 120

    icon: "mdi:animation-outline"


input_number:
  xiaomi_fan_speed:
    name: "속도조절"
    initial: 1
    min: 1
    max: 100
    step: 1
    icon: "mdi:weather-windy"


5. 상기 인풋 옵션, 인풋 넘버 값 지정 자동화 생성


1) 선풍기 값이 변하면 인풋 옵션, 넘버를 초기화 하는 자동화 생성


automations:

- alias: xiaomi fan speed changed

  trigger:

    platform: state

    entity_id: fan.seonpunggi

  action:

    service: input_number.set_value

    entity_id: input_number.xiaomi_fan_speed

    data_template:

      value: '{{ state_attr(''fan.seonpunggi'', ''direct_speed'') | float }}'


- alias: xiaomi fan angle changed
  trigger:
    platform: state
    entity_id: fan.seonpunggi
  action:
    service: input_select.select_option
    entity_id: input_select.xiaomi_fan_angle
    data_template:
      option: '{{ state_attr(''fan.seonpunggi'', ''angle'')  }}'


2) 인풋 옵션, 넘버가 바뀌면 공기청정기를 세팅하는 자동화 생성


automations:

- alias: xiaomi fan speed change

  trigger:

    entity_id: input_number.xiaomi_fan_speed

    platform: state

  action:

    service: fan.set_speed

    data_template:

      entity_id: fan.seonpunggi

      speed: '{{ states(''input_number.xiaomi_fan_speed'') | int }}'


- alias: xiaomi fan angle change
  trigger:
    entity_id: input_select.xiaomi_fan_angle
    platform: state
  action:
    service: fan.xiaomi_miio_set_oscillation_angle
    data_template:
      entity_id: fan.seonpunggi
      angle: '{{ states(''input_select.xiaomi_fan_angle'') | int }}'





4. 결과


entities:

  - entity: fan.xiaomi_air_purifier_2s

  - entity: input_select.xiaomi_airpurifier_mode

  - entity: input_number.xiaomi_airpurifier_favorite_level

  - entity: switch.xiaomi_airpurifier_child_lock

  - entity: switch.xiaomi_airpurifier_led

  - entity: switch.xiaomi_airpurifier_buzzer

  - entity: sensor.xiaomi_airpurifier_speed

  - entity: sensor.xiaomi_airpurifier_filter_remaining

show_header_toggle: false

type: entities



entities:

  - entity: fan.seonpunggi

  - entity: switch.xiaomi_fan_child_lock

  - entity: switch.xiaomi_fan_buzzer

  - entity: switch.xiaomi_fan_oscillate

  - entity: input_select.xiaomi_fan_angle

  - entity: input_number.xiaomi_fan_speed

  - entity: sensor.xiaomi_fan_speed

  - entity: switch.xiaomi_fan_natural

show_header_toggle: false

type: entities


entities:
  - entity: switch.xiaomi_fan_oscillate_left
    tap_action:
      action: call-service
      service: fan.set_direction
      service_data:
        direction: left
        entity_id: fan.seonpunggi
  - entity: switch.xiaomi_fan_oscillate_right
    tap_action:
      action: call-service
      service: fan.set_direction
      service_data:
        direction: right
        entity_id: fan.seonpunggi
show_state: false
type: glance



감사합니다.

반응형

댓글