# Vacuum Package

# -----------------------------------------------------------------------------
# Vacuum Helpers
input_number:
  furbot_main_brush_life_hours:
    unit_of_measurement: h
    initial: 300
    mode: box

  furbot_right_brush_life_hours:
    unit_of_measurement: h
    initial: 200
    mode: box

  furbot_filter_life_hours:
    unit_of_measurement: h
    initial: 150
    mode: box

  furbot_sensor_life_hours:
    unit_of_measurement: h
    initial: 30
    mode: box

input_text:
  furbot_last_fan_speed:
    name: Furbot Last Fan Speed
    initial: ""

template:
  
  # Consumable Helpers
  sensor:
    - name: Furbot Main Brush Life Pct
      state_class: measurement
      unit_of_measurement: "%"
      state: "{{ (states('sensor.valetudo_furbot_main_brush') | float) / (states('input_number.furbot_main_brush_life_hours') * 3600) }}"

    - name: Furbot Right Brush Life Pct
      state_class: measurement
      unit_of_measurement: "%"
      state: "{{ (states('sensor.valetudo_furbot_right_brush') | float) / (states('input_number.furbot_right_brush_life_hours') * 3600) }}"

    - name: Furbot Filter Life Pct
      state_class: measurement
      unit_of_measurement: "%"
      state: "{{ (states('sensor.valetudo_furbot_main_filter') | float) / (states('input_number.furbot_filter_life_hours') * 3600) }}"

    - name: Furbot Sensor Cleaning Pct
      state_class: measurement
      unit_of_measurement: "%"
      state: "{{ (states('sensor.valetudo_furbot_sensor_cleaning') | float) / (states('input_number.furbot_sensor_life_hours') * 3600) }}"

# -----------------------------------------------------------------------------
# Vacuum Scripts
script:

  # Vacuum the bedroom (room ) only with high suction and two passes
  furbot_deep_clean_bedroom:
    alias: "Furbot: Deep Clean Bedroom"
    sequence:
      # Cache the fan setting and reset it when cleaning finishes
      - service: input_text.set_value
        target:
          entity_id: input_text.furbot_last_fan_speed
        data:
          value: "{{ states('select.valetudo_furbot_fan') }}"

      # Start Cleaning
      - service: mqtt.publish
        data:
          topic: valetudo/furbot/FanSpeedControlCapability/preset/set
          payload: max

      - service: mqtt.publish
        data:
          topic: valetudo/furbot/MapSegmentationCapability/clean/set
          payload: '{"segment_ids": ["2"], "iterations": 2}'

# -----------------------------------------------------------------------------
# Vacuum Automations
automation:

  # Reset the Fan Speed when cleaning is complete (if cached)
  - alias: "Furbot: Reset Fan Speed"
    trigger:
      - platform: state
        entity_id: vacuum.valetudo_furbot
        to: docked
    condition: "{{ states('input_text.furbot_last_fan_speed') != '' }}"
    action:
      - service: mqtt.publish
        data:
          topic: valetudo/furbot/FanSpeedControlCapability/preset/set
          payload_template: "{{ states('input_text.furbot_last_fan_speed') }}"

      - service: input_text.reload
        target:
          entity_id: input_text.furbot_last_fan_speed
