###############################################################################
# Bedroom 1 Ikea Air Filter Package
# File: /config/packages/bedroom_1_ikea_air_filter.yaml
# Version: 1.2
# Date: 2026-07-20
#
# Purpose:
# - Creates a dropdown helper called "Bedroom 1 Ikea Fan".
# - Auto mode controls the Bedroom 1 Ikea air filter speed by event and timing.
# - Manual dropdown modes set a fixed fan speed and disable auto control.
#
# Behaviour notes:
# - Quiet time turning on sets fan speed to 2.
# - Bedside lamp or Jump Fade control turning on sets fan speed to 2.
# - Bedside lamp turning off does not change the fan speed.
# - There is no 10-minute re-check, so the fan will not ramp back up just
#   because the bedside lamp was switched off.
# - Time boundaries can still change the fan speed later.
#
# Auto timing:
# - 21:00 to midnight: fan speed 5
# - 09:00 to 15:00: fan speed 4
# - Default: fan speed 2
###############################################################################

input_select:
  bedroom_1_ikea_fan:
    name: "Bedroom 1 Ikea Fan"
    icon: mdi:fan
    options:
      - "Auto"
      - "Off"
      - "High"
      - "Medium"
      - "Low"
      - "Quiet"

script:
  bedroom_1_ikea_air_filter_set_speed_if_needed:
    alias: "Bedroom 1 Ikea Air Filter - Set Speed If Needed"
    description: >
      Sets the Bedroom 1 Ikea air filter speed only when the requested speed
      is different from the current speed.
    mode: queued
    fields:
      target_speed:
        name: "Target speed"
        description: "Fan speed level from 0 to 6."
        example: 2
    sequence:
      - variables:
          fan_speed_entity: number.bedroom_1_air_filter_bedroom_1_air_filter_speed_level
          fan_current_speed: "{{ states(fan_speed_entity) | float(-1) }}"
          fan_target_speed: "{{ target_speed | float }}"

      - condition: template
        value_template: "{{ fan_current_speed != fan_target_speed }}"

      - action: number.set_value
        target:
          entity_id: number.bedroom_1_air_filter_bedroom_1_air_filter_speed_level
        data:
          value: "{{ fan_target_speed | int }}"

automation:
  - id: bedroom_1_ikea_air_filter_auto_control
    alias: "Bedroom 1 Ikea Air Filter - Auto Control"
    description: >
      Automatically controls the Bedroom 1 Ikea air filter speed when the
      Bedroom 1 Ikea Fan dropdown is set to Auto.
    mode: restart
    triggers:
      # Re-apply the correct speed after Home Assistant restarts.
      - trigger: homeassistant
        event: start

      # Quiet time only forces low when it turns on.
      # Turning quiet time off does not automatically ramp the fan back up.
      - trigger: state
        entity_id: input_boolean.quiet_time
        to: "on"

      # The lamp state catches direct light control.
      - trigger: state
        entity_id: light.master_bedroom_bedside_lamp_arlec_bulb_master_bedroom_bedside_lamp_arlec_bulb
        to: "on"

      # The Jump Fade switch catches a bedside press while the lamp is already on.
      - trigger: state
        entity_id: switch.master_bedroom_bedside_lamp_arlec_bulb_master_bedroom_bedside_lamp_arlec_bulb_jump_fade_up_down
        to: "on"

      # Main timing boundaries.
      - trigger: time
        at:
          - "00:00:00"
          - "09:00:00"
          - "15:00:00"
          - "21:00:00"

    conditions:
      # Manual dropdown modes disable automatic control.
      - condition: state
        entity_id: input_select.bedroom_1_ikea_fan
        state: "Auto"

    actions:
      - variables:
          trigger_entity: "{{ trigger.entity_id | default('') }}"
          bedside_lamp_entity: light.master_bedroom_bedside_lamp_arlec_bulb_master_bedroom_bedside_lamp_arlec_bulb
          bedside_lamp_switch_entity: switch.master_bedroom_bedside_lamp_arlec_bulb_master_bedroom_bedside_lamp_arlec_bulb_jump_fade_up_down
          bedside_lamp_on_trigger: >-
            {{
              trigger_entity in [bedside_lamp_entity, bedside_lamp_switch_entity]
              and trigger.to_state is defined
              and trigger.to_state.state == 'on'
            }}
          quiet_time_on_trigger: >-
            {{
              trigger_entity == 'input_boolean.quiet_time'
              and trigger.to_state is defined
              and trigger.to_state.state == 'on'
            }}
          auto_target_speed: >-
            {% if bedside_lamp_on_trigger %}
              2
            {% elif quiet_time_on_trigger %}
              2
            {% elif is_state('input_boolean.quiet_time', 'on') %}
              2
            {% elif now().hour >= 21 %}
              5
            {% elif now().hour >= 9 and now().hour < 15 %}
              4
            {% else %}
              2
            {% endif %}

      - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
        data:
          target_speed: "{{ auto_target_speed | int }}"

  - id: bedroom_1_ikea_air_filter_dropdown_control
    alias: "Bedroom 1 Ikea Air Filter - Dropdown Control"
    description: >
      Handles the Bedroom 1 Ikea Fan dropdown. Auto enables the automatic
      control automation. Manual modes disable automatic control and set
      the requested fixed speed.
    mode: restart
    triggers:
      # Apply the selected dropdown mode when it changes.
      - trigger: state
        entity_id: input_select.bedroom_1_ikea_fan

      # Re-apply the selected dropdown mode after Home Assistant restarts.
      - trigger: homeassistant
        event: start

    actions:
      - choose:
          - alias: "Auto mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "Auto"
            sequence:
              - action: automation.turn_on
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              # Auto starts at speed 2, then immediately evaluates the
              # current timing rules.
              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 2

              - delay:
                  seconds: 1

              - action: automation.trigger
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control
                data:
                  skip_condition: false

          - alias: "Off mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "Off"
            sequence:
              - action: automation.turn_off
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 0

          - alias: "High mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "High"
            sequence:
              - action: automation.turn_off
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 6

          - alias: "Medium mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "Medium"
            sequence:
              - action: automation.turn_off
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 4

          - alias: "Low mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "Low"
            sequence:
              - action: automation.turn_off
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 2

          - alias: "Quiet mode"
            conditions:
              - condition: state
                entity_id: input_select.bedroom_1_ikea_fan
                state: "Quiet"
            sequence:
              - action: automation.turn_off
                target:
                  entity_id: automation.bedroom_1_ikea_air_filter_auto_control

              - action: script.bedroom_1_ikea_air_filter_set_speed_if_needed
                data:
                  target_speed: 1
