# Laundry Package

# -----------------------------------------------------------------------------
# Washer Final Pump-Out State Machine
input_select:
  washer_complete_detect_state:
    name: Washer Complete Detect State
    options:
      - Idle
      - Stage 1         # Iw > 4A, <70s
      - Stage 2         # Iw ~ [2A, 4A], 60s
      - Stage 3         # dIw ~ [-1.0A, -0.4A], 45s
      - Triggered       # Iw < 0.2A, 60s

input_number:
  washer_complete_threshold_mid:
    min: 0.0
    max: 4.0
    step: 0.1

# -----------------------------------------------------------------------------
# Laundry Automations
automation:
  - alias: Notify when Dryer Done
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.dryer_power_electric_consumption_a
        below: 1.0
        for:
          minutes: 1
    action:
      - service: notify.everyone
        data:
          message: Dryer is done

  - alias: Washer Complete Detect - Stage 1
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.washer_power_electric_consumption_a
        above: 4
    condition:
      - "{{ is_state('input_select.washer_complete_detect_state', 'Idle') }}"
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.washer_complete_detect_state
        data:
          option: Stage 1

  - alias: Washer Complete Detect - Stage 2
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.washer_power_electric_consumption_a
        above: 2
        below: 4
        for:
          seconds: 60
    condition:
      - "{{ is_state('input_select.washer_complete_detect_state', 'Stage 1') }}"
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.washer_complete_detect_state
        data:
          option: Stage 2
      - service: input_number.set_value
        target:
          entity_id: input_number.washer_complete_threshold_mid
        data:
          value: "{{ states('sensor.washer_power_electric_consumption_a')|float }}"

  - alias: Washer Complete Detect - Stage 3
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.washer_power_electric_consumption_a
        value_template: "{{ 
            states('sensor.washer_power_electric_consumption_a')|float
          - states('input_number.washer_complete_threshold_mid')|float
        }}"
        above: -1.0
        below: -0.4
        for:
          seconds: 45
    condition:
      - "{{ is_state('input_select.washer_complete_detect_state', 'Stage 2') }}"
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.washer_complete_detect_state
        data:
          option: Stage 3

  - alias: Washer Complete Detect - Trigger
    mode: single
    trigger:
      - platform: numeric_state
        entity_id: sensor.washer_power_electric_consumption_a
        below: 0.2
        for:
          seconds: 60
    condition:
      - "{{ is_state('input_select.washer_complete_detect_state', 'Stage 3') }}"
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.washer_complete_detect_state
        data:
          option: Triggered

  - alias: Washer Complete Detect - Timeout
    mode: single
    trigger:
      - platform: state
        entity_id: input_select.washer_complete_detect_state
        to: Stage 1
        for:
          seconds: 120
      - platform: state
        entity_id: input_select.washer_complete_detect_state
        to: Stage 2
        for:
          seconds: 240
      - platform: state
        entity_id: input_select.washer_complete_detect_state
        to: Stage 3
        for:
          seconds: 180
      - platform: state
        entity_id: input_select.washer_complete_detect_state
        to: Triggered
        for:
          seconds: 60
    action:
      - service: input_select.select_option
        target:
          entity_id: input_select.washer_complete_detect_state
        data:
          option: Idle

  - alias: Notify when Washer Done
    mode: single
    trigger:
      - platform: state
        entity_id: input_select.washer_complete_detect_state
        to: Triggered
    action:
      # The trigger occurs about 2 minutes before the lid unlocks
      - delay: 120
      - service: notify.everyone
        data:
          message: Washer is Done
