2025/06/01

HA연결 상태에 따른 esphome 벽스위치 액션 전환


벽스위치의 첫번째 버튼을 클릭시 HA연결상태를 체크하는 바이너리 센서를 확인해 on이면 ha의 entity를 토글하고 off(ha와 연결되지 않은 상태)면 연결된 relay를 토글.

이러면 정전복귀 직후 또는 HA서버가 연결되지 않은 상태에서도 클릭버튼이 상황에 맞게 작동. 예비로 길게 누름은 relay 토글로 고정.

첫번째 릴레이에 연결된 샤오미 천장등은 power-on behavior를 on으로 설정해 두면 바이너리 센서 off시 relay toggle로 on/off 제어가 되고 HA에 연결된 상태면 homeassistant.service로 제어.

binary_sensor:

  - platform: status
    id: ha_connected
    internal: true

  - platform: gpio
    pin:
      number: 8
      mode: INPUT_PULLUP
      inverted: True
    name: "bedroom Button1"
    on_multi_click:

      - timing:
          - ON for at most 0.5s
          - OFF for at least 0.4s
        then:
          - if:
              condition:
                binary_sensor.is_on: ha_connected
              then:
                - homeassistant.service:
                    service: light.toggle
                    data:
                      entity_id: light.yeelink_ceiling
                      color_temp_kelvin: '4000'
                      brightness: '255'
              else:
                - switch.toggle: load1

      - timing:
          - ON for at most 0.5s
          - OFF for at most 0.3s
          - ON for at most 0.3s
          - OFF for at least 0.1s
        then:
          - homeassistant.service:
              service: light.turn_on
              data:
                entity_id: light.yeelink_ceiling
                color_temp_kelvin: '2700'
                brightness: '255'

      - timing:
          - ON for 0.6s to 5s
          - OFF for at least 0.1s
        then:
          - switch.toggle: load1