# Vacuum Package

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

  furbot_right_brush_life_hours:
    unit_of_measurement: h
    initial: 200
    min: 10
    max: 300
    mode: box

  furbot_filter_life_hours:
    unit_of_measurement: h
    initial: 150
    min: 10
    max: 300
    mode: box

  furbot_sensor_life_hours:
    unit_of_measurement: h
    initial: 30
    min: 10
    max: 300
    mode: box

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

# -----------------------------------------------------------------------------
# Room/Segment Helpers
input_boolean:
  furbot_segment_dining_room:
    name: Dining Room
    icon: mdi:table-furniture

  furbot_segment_kitchen:
    name: Kitchen
    icon: mdi:silverware

  furbot_segment_laundry:
    name: Laundry Room
    icon: mdi:washing-machine

  furbot_segment_entry:
    name: Entry
    icon: mdi:coat-rack

  furbot_segment_office:
    name: Office
    icon: mdi:chair-rolling

  furbot_segment_hallway:
    name: Hallway
    icon: mdi:shoe-print

  furbot_segment_bathroom:
    name: Bathroom
    icon: mdi:shower

  furbot_segment_bedroom:
    name: Bedroom
    icon: mdi:bed

  furbot_segment_living_room:
    name: Living Room
    icon: mdi:television

  furbot_segment_guest_room:
    name: Guest Room
    icon: mdi:account

homeassistant:
  customize:
    input_boolean.furbot_segment_bathroom:
      segment_id: '1'
    input_boolean.furbot_segment_bedroom:
      segment_id: '2'
    input_boolean.furbot_segment_guest_room:
      segment_id: '3'
    input_boolean.furbot_segment_office:
      segment_id: '4'
    input_boolean.furbot_segment_living_room:
      segment_id: '5'
    input_boolean.furbot_segment_laundry:
      segment_id: '6'
    input_boolean.furbot_segment_kitchen:
      segment_id: '7'
    input_boolean.furbot_segment_entry:
      segment_id: '8'
    input_boolean.furbot_segment_dining_room:
      segment_id: '9'
    input_boolean.furbot_segment_hallway:
      segment_id: '10'

group:
  furbot_segments:
    name: Furbot Rooms
    entities:
      - input_boolean.furbot_segment_bathroom
      - input_boolean.furbot_segment_bedroom
      - input_boolean.furbot_segment_guest_room
      - input_boolean.furbot_segment_office
      - input_boolean.furbot_segment_living_room
      - input_boolean.furbot_segment_laundry
      - input_boolean.furbot_segment_kitchen
      - input_boolean.furbot_segment_entry
      - input_boolean.furbot_segment_dining_room
      - input_boolean.furbot_segment_hallway

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

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

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

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

# -----------------------------------------------------------------------------
# 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 Selected Segments
  furbot_clean_segments:
    alias: "Furbot: Clean Selected Rooms"
    sequence:
      - choose:
          # Pause the robot if it is currently cleaning
          - conditions:
              - condition: template
                value_template: "{{ states('vacuum.valetudo_furbot') == 'cleaning' }}"
            sequence:
              - service: mqtt.publish
                data:
                  topic: valetudo/furbot/BasicControlCapability/operation/set
                  payload: 'PAUSE'

          # Resume the robot if it is paused
          - conditions:
              - condition: template
                value_template: "{{ states('vacuum.valetudo_furbot') == 'paused' }}"
            sequence:
              - service: mqtt.publish
                data:
                  topic: valetudo/furbot/BasicControlCapability/operation/set
                  payload: 'START'

        # Start a new cleaning if it is docked
        default:
          - if:
              - condition: template
                value_template: "{{ is_state('group.furbot_segments', 'off') }}"
            then:
              - service: mqtt.publish
                data:
                  topic: valetudo/furbot/BasicControlCapability/operation/set
                  payload: 'START'
            else:
              - service: mqtt.publish
                data:
                  topic: valetudo/furbot/MapSegmentationCapability/clean/set
                  payload: >-
                    {
                      "segment_ids": {{ expand('group.furbot_segments') |
                        selectattr('state','eq','on') |
                        map(attribute='attributes.segment_id') | list | to_json }} 
                    }

# -----------------------------------------------------------------------------
# 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

  # Reset the Cleaning Segments when cleaning is complete
  - alias: "Furbot: Reset Rooms"
    trigger:
      - platform: state
        entity_id: vacuum.valetudo_furbot
        to: docked
    action:
      - service: homeassistant.turn_off
        target:
          entity_id: group.furbot_segments
