## 要修正項目
#
## 1. MQTT トピック
## echonetlite2mqttの設定にあうように変更してください。
## echonetlite2mqtt/elapi/v2/devices/kaiteki_airy の部分が環境によって異なります。
#
## 2. climateの設定
## unique_idとnameは適宜変更してください。
## name: "快適エアリー 畳スペース"
##      unique_id: climate-kaiteki-airy-tatami-space

input_button:
  airy_on:
    name: 快適エアリーON
  airy_off:
    name: 快適エアリーOFF


mqtt:
  - sensor:
    - name: "KA ZoneConfigs Mirror"
      unique_id: sensor.ka_zoneconfigs_mirror
      state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs"
      value_template: "{{ value_json.zone1OperationStatus | default('') }}"
      json_attributes_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs"
      json_attributes_template: >-
        {{ {'raw': value_json} | tojson }}

    - name: "KA ZoneConfigs Backup"
      unique_id: sensor.ka_zoneconfigs_backup
      state_topic: "kaiteki_airy/backups/zoneconfigs"
      value_template: "{{ value_json.zone1OperationStatus | default('') }}"
      json_attributes_topic: "kaiteki_airy/backups/zoneconfigs"
      json_attributes_template: >-
        {{ {'raw': value_json} | tojson }}

  - climate:
    # 快適エアリー ゾーン制御
    # 各ゾーンの設定は以下の部分のみ異なる：
    # - name, unique_id
    # - zone番号 (zone1, zone2, zone3)
    # - それ以外のロジックは共通
    - name: "快適エアリー 畳スペース"
      unique_id: climate-kaiteki-airy-tatami-space
      modes:
        - "off"
        - "auto"
      mode_state_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone1OperationStatus in ['off','keep'] %}
          off
        {% else %}
          auto
        {% endif %}
      mode_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"
      
      action_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone1OperationStatus == 'off' %}
          off
        {% elif value_json.kaitekiAiryZoneConfigs.zone1OperationStatus == 'keep' %}
          idle
        {% else %}
          {% set m = value_json.operationMode %}
          {% if m == 'cooling' %}
            cooling
          {% elif m == 'heating' %}
            heating
          {% elif m == 'dehumidification' %}
            drying
          {% else %}
            fan
          {% endif %}
        {% endif %}
      action_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"

      temperature_state_template: >-
        {{ value_json.zone1TargetTemp }}
      temperature_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs"
      current_temperature_template: >-
        {{ value_json.zone1Temp }}
      current_temperature_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneTemps"

      mode_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      mode_command_template: >-
        {% set turn = 'off' if value == 'off' else 'on' %}
        {% set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} %}
        {{ current | combine({ 'zone1OperationStatus': turn }) | to_json }}

      temperature_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      temperature_command_template: >-
        {%- set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} -%}
        {{ current | combine({ 'zone1TargetTemp': value | string }) | to_json }}
      

    - name: "快適エアリー リビング"
      unique_id: climate-kaiteki-airy-living
      modes:
        - "off"
        - "auto"
      mode_state_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone2OperationStatus in ['off','keep'] %}
          off
        {% else %}
          auto
        {% endif %}
      mode_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"
      
      action_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone2OperationStatus == 'off' %}
          off
        {% elif value_json.kaitekiAiryZoneConfigs.zone2OperationStatus == 'keep' %}
          idle
        {% else %}
          {% set m = value_json.operationMode %}
          {% if m == 'cooling' %}
            cooling
          {% elif m == 'heating' %}
            heating
          {% elif m == 'dehumidification' %}
            drying
          {% else %}
            fan
          {% endif %}
        {% endif %}
      action_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"
      
      temperature_state_template: >-
        {{ value_json.zone2TargetTemp }}
      temperature_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs"
      current_temperature_template: >-
        {{ value_json.zone2Temp }}
      current_temperature_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneTemps"

      mode_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      mode_command_template: >-
        {% set turn = 'off' if value == 'off' else 'on' %}
        {% set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} %}
        {{ current | combine({ 'zone2OperationStatus': turn }) | to_json }}

      temperature_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      temperature_command_template: >-
        {% set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} %}
        {{ current | combine({ 'zone2TargetTemp': value }) | to_json }}

    - name: "快適エアリー ベース"
      unique_id: climate-kaiteki-airy-base
      modes:
        - "off"
        - "auto"
      mode_state_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone3OperationStatus in ['off','keep'] %}
          off
        {% else %}
          auto
        {% endif %}
      mode_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"
      
      action_template: >-
        {% if value_json.operationStatus == 'false' or value_json.kaitekiAiryZoneConfigs.zone3OperationStatus == 'off' %}
          off
        {% elif value_json.kaitekiAiryZoneConfigs.zone3OperationStatus == 'keep' %}
          idle
        {% else %}
          {% set m = value_json.operationMode %}
          {% if m == 'cooling' %}
            cooling
          {% elif m == 'heating' %}
            heating
          {% elif m == 'dehumidification' %}
            drying
          {% else %}
            fan
          {% endif %}
        {% endif %}
      action_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties"

      
      temperature_state_template: >-
        {{ value_json.zone3TargetTemp }}
      temperature_state_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs"
      current_temperature_template: >-
        {{ value_json.zone3Temp }}
      current_temperature_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneTemps"

      mode_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      mode_command_template: >-
        {% set turn = 'off' if value == 'off' else 'on' %}
        {% set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} %}
        {{ current | combine({ 'zone3OperationStatus': turn }) | to_json }}

      temperature_command_topic: "echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set"
      temperature_command_template: >-
        {% set current = state_attr('sensor.ka_zoneconfigs_mirror', 'raw') or {} %}
        {{ current | combine({ 'zone3TargetTemp': value }) | to_json }}

automation:
  - alias: 快適エアリーOFF(w/ Backup)
    description: ''
    triggers:
    - trigger: state
      entity_id:
      - input_button.airy_off
    conditions: []
    actions:
    # 現在の設定を直接バックアップ
    - action: mqtt.publish
      data_template:
        topic: kaiteki_airy/backups/zoneconfigs
        payload: >-
          {{ state_attr('sensor.ka_zoneconfigs_mirror','raw') | tojson }}
        retain: true
    # ECHONET Liteデバイスを直接OFF
    - action: mqtt.publish
      data:
        topic: echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/operationStatus/set
        payload: "false"
        retain: true
    mode: single

  - alias: 快適エアリーON (from backup)
    description: ''
    triggers:
    - trigger: state
      entity_id:
      - input_button.airy_on
    conditions: []
    actions:
    # ECHONET Liteデバイスを直接ON
    - action: mqtt.publish
      data:
        topic: echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/operationStatus/set
        payload: "true"
        retain: true
    - delay:
        hours: 0
        minutes: 0
        seconds: 3
        milliseconds: 0
    # バックアップセンサーから復元（バックアップが存在する場合のみ）
    - condition: template
      value_template: >-
        {% set backup = state_attr('sensor.ka_zoneconfigs_backup','raw') %}
        {{ backup is mapping and backup|length > 0 }}
    - action: mqtt.publish
      data_template:
        topic: echonetlite2mqtt/elapi/v2/devices/kaiteki_airy/properties/kaitekiAiryZoneConfigs/set
        payload: >-
          {{ state_attr('sensor.ka_zoneconfigs_backup','raw') | tojson }}
        retain: true
    mode: single
