fan:
  - platform: philips_airpurifier_http
    host: !secret philips_purifier_ip

input_boolean:
  purifier_turned_on_for_bad_air:
    name: Purifier enabled for bad air

input_select:
  purifier_preset_mode:
    name: mode
    icon: mdi:speedometer
    options:
      - auto
      - manual
      - sleep
      - allergen

  purifier_speed:
    name: speed
    icon: mdi:speedometer
    options:
      - 0
      - 20
      - 40
      - 60
      - 80
      - 100

  purifier_target_humidity:
    name: Target humidity
    icon: mdi:water-percent
    options:
      - 40
      - 50
      - 60

sensor:
  - platform: template
    sensors:
      purifier_air_quality:
        friendly_name: Air quality
        value_template: "{{ state_attr('fan.philips_airpurifier', 'pm25') }}"
        icon_template: mdi:weather-windy
      purifier_humidity:
        friendly_name: Humidity
        unit_of_measurement: "%"
        device_class: humidity
        value_template: "{{ state_attr('fan.philips_airpurifier', 'humidity') }}"
        icon_template: mdi:water
      purifier_temperature:
        friendly_name: Temperature
        unit_of_measurement: "°C"
        device_class: temperature
        value_template: "{{ state_attr('fan.philips_airpurifier', 'temperature') }}"
        icon_template: mdi:thermometer
      purifier_preset_mode:
        friendly_name: Preset Mode
        value_template: "{{ state_attr('fan.philips_airpurifier', 'preset_mode') }}"
        icon_template: >-
          {% set value = state_attr('fan.philips_airpurifier', 'preset_mode') %}
          {% if value == 'auto' %}
            {{ 'mdi:brightness-auto' }}
          {% elif value == 'manual' %}
            {{ 'mdi:speedometer-medium' }}
          {% elif value == 'sleep' %}
            {{ 'mdi:power-sleep' }}
          {% elif value == 'allergen' %}
            {{ 'mdi:allergy' }}
          {% endif %}
      purifier_speed:
        friendly_name: Speed
        value_template: "{{ state_attr('fan.philips_airpurifier', 'percentage') }}"
      purifier_allergens:
        friendly_name: Allergen index
        value_template: "{{ state_attr('fan.philips_airpurifier', 'allergen_index') }}"
        icon_template: mdi:weather-windy-variant
      purifier_function:
        friendly_name: Function
        value_template: "{{ state_attr('fan.philips_airpurifier', 'function') }}"
        icon_template: >-
          {% if state_attr('fan.philips_airpurifier', 'function') == 'Purification' %}
            {{ 'mdi:air-purifier' }}
          {% else %}
            {{ 'mdi:air-humidifier' }}
          {% endif %}
      purifier_pre_filter:
        friendly_name: Pre-filter
        unit_of_measurement: "Hrs"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'pre_filter') }}"
        icon_template: mdi:air-filter
      purifier_wick_filter:
        friendly_name: Wick filter
        unit_of_measurement: "Hrs"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'wick_filter') }}"
        icon_template: mdi:air-filter
      purifier_carbon_filter:
        friendly_name: Carbon filter
        unit_of_measurement: "Hrs"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'carbon_filter') }}"
        icon_template: mdi:air-filter
      purifier_hepa_filter:
        friendly_name: HEPA filter
        unit_of_measurement: "Hrs"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'hepa_filter')}}"
        icon_template: mdi:air-filter
      purifier_target_humidity:
        friendly_name: Target humidity
        unit_of_measurement: "%"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'target_humidity') }}"
        icon_template: mdi:water-percent
      purifier_brightness:
        friendly_name: Brightness
        unit_of_measurement: "%"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'light_brightness') }}"
        icon_template: mdi:brightness-6
      purifier_timer:
        friendly_name: Timer
        unit_of_measurement: "hrs"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'timer') }}"
        icon_template: mdi:timer-outline
      purifier_time_remaining:
        friendly_name: Time remaining
        unit_of_measurement: "minutes"
        value_template: "{{ state_attr('fan.philips_airpurifier', 'timer_remaining_minutes') }}"
        icon_template: mdi:timer-outline

binary_sensor:
  - platform: template
    sensors:
      purifier_out_of_water:
        friendly_name: Water level
        device_class: problem
        value_template: "{{ state_attr('fan.philips_airpurifier', 'water_level') == 0 }}"
      purifier_child_lock:
        friendly_name: Child lock
        value_template: "{{ state_attr('fan.philips_airpurifier', 'child_lock') }}"
      purifier_display_light:
        friendly_name: Display light
        value_template: "{{ state_attr('fan.philips_airpurifier', 'display_light') }}"
      purifier_filter_needs_change:
        friendly_name: Filter needs change
        device_class: problem
        value_template: >

          {{ is_state('sensor.purifier_pre_filter', '0')
              or is_state('sensor.purifier_wick_filter', '0')
              or is_state('sensor.purifier_carbon_filter', '0')
              or is_state('sensor.purifier_hepa_filter', '0') }}


script:
  purifier_toggle_function:
    alias: Purifier · Toggle function
    sequence:
      - condition: state
        entity_id: binary_sensor.purifier_out_of_water
        state: 'off'
      - service: philips_airpurifier_http.set_function
        entity_id: fan.philips_airpurifier
        data_template:
          function: >-
            {% if state_attr('fan.philips_airpurifier', 'function') == 'Purification' %}
              {{ 'Purification & Humidification' }}
            {% else %}
              {{ 'Purification' }}
            {% endif %}

automation:
  - alias: Purifier · On start · Sync and update state
    trigger:
      platform: state
      entity_id: fan.philips_airpurifier
      to: 'on'
    action:
      - service: input_select.select_option
        entity_id: input_select.purifier_target_humidity
        data_template:
          option: "{{ states('sensor.purifier_target_humidity') }}"
      - service: input_select.select_option
        entity_id: input_select.purifier_speed
        data_template:
          option: "{{ states('sensor.purifier_speed') }}"
      - condition: state
        entity_id: binary_sensor.purifier_out_of_water
        state: "on"
      - service: philips_airpurifier_http.set_function
        entity_id: fan.philips_airpurifier
        data:
          function: 'Purification'

  - alias: Purifier · Change preset mode
    trigger:
      platform: state
      entity_id: input_select.purifier_preset_mode
    action:
      - service: fan.set_preset_mode
        entity_id: fan.philips_airpurifier
        data_template:
          preset_mode: "{{ states('input_select.purifier_preset_mode') }}"

  - alias: Purifier · Sync preset mode
    trigger:
      platform: state
      entity_id: sensor.purifier_preset_mode
    action:
      - service: input_select.select_option
        entity_id: input_select.purifier_preset_mode
        data_template:
          option: "{{ states('sensor.purifier_preset_mode') }}"

  - alias: Purifier · Change speed
    trigger:
      platform: state
      entity_id: input_select.purifier_speed
    action:
      - service: fan.set_percentage
        entity_id: fan.philips_airpurifier
        data_template:
          percentage: "{{ states('input_select.purifier_speed') }}"

  - alias: Purifier · Sync speed
    trigger:
      platform: state
      entity_id: sensor.purifier_speed
    action:
      - service: input_select.select_option
        entity_id: input_select.purifier_speed
        data_template:
          option: "{{ states('sensor.purifier_speed') }}"

  - alias: Purifier · Change humidity
    trigger:
      platform: state
      entity_id: input_select.purifier_target_humidity
    action:
      - service: philips_airpurifier_http.set_target_humidity
        entity_id: fan.philips_airpurifier
        data_template:
          humidity: >-
            {% set state = states('input_select.purifier_target_humidity') | int %}
            {% if state == 40 %}
              40
            {% elif state == 50 %}
              50
            {% else %}
              60
            {% endif %}

  - alias: Purifier · Sync humidity
    trigger:
      platform: state
      entity_id: sensor.purifier_target_humidity
    action:
      - service: input_select.select_option
        entity_id: input_select.purifier_target_humidity
        data_template:
          option: "{{ states('sensor.purifier_target_humidity') }}"

  - alias: Purifier · Turn on · Low humidity
    trigger:
      platform: numeric_state
      entity_id: sensor.bedroom_weather_humidity
      below: 20
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: binary_sensor.purifier_out_of_water
          state: "off"
        - condition: state
          entity_id: fan.philips_airpurifier
          state: "off"
        - condition: state
          entity_id: input_boolean.georgi_home
          state: "on"
    action:
      - service: fan.turn_on
        entity_id: fan.philips_airpurifier
      - service: philips_airpurifier_http.set_function
        entity_id: fan.philips_airpurifier
        data:
          function: "Purification & Humidification"
      - service: script.say
        data:
          message: "Hey, I'm turning on the purifier, because humidity is low and there is water in the tank"
      - service: logbook.log
        data:
          name: "💨 Climate module · "
          message: "Purifier On · Humidity is low"
          domain: fan

  - alias: Purifier · Turn on · Low air quality
    trigger:
      platform: numeric_state
      entity_id: sensor.purifier_air_quality
      above: 30
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.georgi_home
          state: "on"
        - condition: state
          entity_id: fan.philips_airpurifier
          state: "off"
    action:
      - service: fan.turn_on
        entity_id: fan.philips_airpurifier
      - service: input_boolean.turn_on
        entity_id: input_boolean.purifier_turned_on_for_bad_air
      - service: script.say
        data:
          message: "Hey, I'm turning on the purifier, because air quality is low"
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "Purifier On · Air quality is low"
          domain: fan
      - condition: state
        entity_id: binary_sensor.purifier_out_of_water
        state: "off"
      - service: philips_airpurifier_http.set_function
        entity_id: fan.philips_airpurifier
        data:
          function: "Purification & Humidification"
      - service: script.say
        data:
          message: "Also, switching to humidification, because there is water in the tank"
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "Purifier Function · Switching to humidification - there is water in the tank"
          domain: fan

  - alias: Purifier · Turn off · Okay humidity
    trigger:
      platform: numeric_state
      entity_id: sensor.bedroom_weather_humidity
      above: 55
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: fan.philips_airpurifier
          state: "on"
        - condition: state
          entity_id: sensor.purifier_function
          state: "Purification & Humidification"
        - condition: numeric_state
          entity_id: sensor.purifier_air_quality
          below: 30
    action:
      - service: fan.turn_off
        entity_id: fan.philips_airpurifier
      - service: script.say
        data:
          message: "Hey, I'm turning the purifier off, because humidity is okay"
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "Purifier Off · Humidity is okay"
          domain: fan

  - alias: Purifier · Turn off · Air quality returned to normal
    trigger:
      platform: numeric_state
      entity_id: sensor.purifier_air_quality
      below: 20
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: fan.philips_airpurifier
          state: "on"
        - condition: state
          entity_id: input_boolean.purifier_turned_on_for_bad_air
          state: "on"
        - condition: state
          entity_id: group.bedroom_windows
          state: "off"

    action:
      - service: fan.turn_off
        entity_id: fan.philips_airpurifier
      - service: input_boolean.turn_off
        entity_id: input_boolean.purifier_turned_on_for_bad_air
      - service: logbook.log
        data:
          name: "💨 Climate Module · "
          message: "Purifier Off · Air quality returned to normal"
          domain: fan

homeassistant:
  customize:
    fan.philips_airpurifier:
      friendly_name: Air purifier
