input_boolean:
  ac_heating:
    name: AC Heating enabled
    icon: mdi:air-conditioner

# https://github.com/hristo-atanasov/Tasmota-IRHVAC/
climate:
  - platform: tasmota_irhvac
    name: "TOSHIBA AC"
    command_topic: "cmnd/ir_blaster_1/irhvac"
    state_topic: "tele/ir_blaster_1/RESULT"
    temperature_sensor: sensor.multisensor_6_temperature
    protocol: "TOSHIBA_AC"
    max_temp: 30  # optional - default 32 int value
    min_temp: 17
    target_temp: 22  # optional - default 26 int value
    away_temp: 18  # optional - default 24 int value
    supported_modes:
      - "heat"
      - "cool"
      - "dry"
      - "auto"
      - "off"
    supported_fan_speeds:
      - "min"
      - "low"
      - "medium"
      - "high"
      - "max"
      - "auto"
    supported_swing_list:
      - "off"

input_number:
  toshiba_heat_temp:
    name: AC heat temp
    unit_of_measurement: "ºC"
    min: 24
    max: 30
    step: 1
    icon: mdi:air-conditioner
  toshiba_cool_temp:
    name: AC cool temp
    unit_of_measurement: "ºC"
    min: 17
    max: 21
    step: 1
    icon: mdi:air-conditioner
  toshiba_trigger_temp:
    name: AC cooling trigger temp
    unit_of_measurement: "ºC"
    min: 18
    max: 25
    step: 1
    icon: mdi:air-conditioner

script:
  toshiba_ac_toggle:
    sequence:
      - service: >-
          {% if is_state('climate.toshiba_ac', 'off') and is_state('input_boolean.ac_heating', 'off') %}
            script.toshiba_ac_on_cool
          {% elif is_state('climate.toshiba_ac', 'off') and is_state('input_boolean.ac_heating', 'on') %}
            script.toshiba_ac_on_heat
          {% else %}
            climate.turn_off
          {% endif %}
        data_template:
          entity_id: "{% if not is_state('climate.toshiba_ac', 'off') %}climate.toshiba_ac{%endif%}"

  toshiba_ac_on_cool:
    sequence:
      - service: climate.turn_on
        entity_id: climate.toshiba_ac
      - delay: "00:00:01"
      - service: climate.set_hvac_mode
        entity_id: climate.toshiba_ac
        data:
          hvac_mode: "cool"
      - delay: "00:00:01"
      - service: climate.set_fan_mode
        entity_id: climate.toshiba_ac
        data:
          fan_mode: "auto"
      - delay: "00:00:01"
      - service: climate.set_temperature
        entity_id: climate.toshiba_ac
        data_template:
          temperature: "{{ states('input_number.toshiba_cool_temp') | default(19) }}"
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "AC · Switching on · Cool"
          domain: climate

  toshiba_ac_on_heat:
    sequence:
      - service: climate.turn_on
        entity_id: climate.toshiba_ac
      - delay: "00:00:01"
      - service: climate.set_hvac_mode
        entity_id: climate.toshiba_ac
        data:
          hvac_mode: "heat"
      - delay: "00:00:01"
      - service: climate.set_fan_mode
        entity_id: climate.toshiba_ac
        data:
          fan_mode: "auto"
      - delay: "00:00:01"
      - service: climate.set_temperature
        entity_id: climate.toshiba_ac
        data_template:
          temperature: "{{ states('input_number.toshiba_heat_temp') | default(26) }}"
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "AC · Switching on · Heat"
          domain: climate

  toshiba_ac_on_cool_if_hot:
    sequence:
      - condition: and
        conditions:
          - condition: template
            value_template: "{{ states('sensor.multisensor_6_temperature')|float > states('input_number.toshiba_trigger_temp')|float }}"
          - condition: numeric_state
            entity_id: sensor.dark_sky_apparent_temperature
            above: 23
          - condition: state
            entity_id: input_boolean.someone_home
            state: "on"
          - condition: state
            entity_id: binary_sensor.living_room_right_window
            state: "off"
          - condition: state
            entity_id: group.bedroom_windows
            state: "off"
          - condition: state
            entity_id: climate.toshiba_ac
            state: "off"
      - service: script.toshiba_ac_on_cool

automation:
  - alias: Climate · AC · Turn on if hot
    trigger:
      - platform: template
        value_template: "{{ states('sensor.multisensor_6_temperature')|float > states('input_number.toshiba_trigger_temp')|float }}"
      - platform: state
        entity_id:
          - binary_sensor.living_room_right_window
          - group.bedroom_windows
        from: "on"
        to: "off"
    action:
      - service: script.toshiba_ac_on_cool_if_hot

  - alias: Climate · AC · Turn off
    trigger:
      - platform: time
        at: "05:00:00"

      # TODO: this below trigger works only for summer :)
      - platform: numeric_state
        entity_id: sensor.multisensor_6_temperature
        below: 20

      - platform: state
        entity_id:
          - binary_sensor.living_room_right_window
          - group.bedroom_windows
        from: "off"
        to: "on"
    condition:
      condition: not
      conditions:
        - condition: state
          entity_id: climate.toshiba_ac
          state: 'off'
    action:
      - service: climate.turn_off
        entity_id: climate.toshiba_ac
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "AC · Switching off"
          domain: climate
