########################################################################
### Vacuum
########################################################################
vacuum:
  - platform: xiaomi_miio
    host: !secret xiaomi_vacuum_ip
    token: !secret xiaomi_vacuum_token
    name: Xiaomi Vacuum
########################################################################
### Sensor
########################################################################
sensor:
  - platform: template
    sensors:
      vacuum_cleaned_area:
        friendly_name: "Área Limpia"
        unit_of_measurement: 'm2'
        value_template: "{{ state_attr('vacuum.xiaomi_vacuum', 'cleaned_area') }}"
      vacuum_cleaning_time:
        friendly_name: "Tiempo Limpiando"
        unit_of_measurement: 'min'
        value_template: "{{ state_attr('vacuum.xiaomi_vacuum', 'cleaning_time') }}"
      xiaomi_vacuum_ip:
        value_template: !secret xiaomi_vacuum_ip
      vacuum_start_time_weekdays:
        friendly_name: 'Start Time (weekdays)'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.vacuum_start_hour_weekdays") | int, states("input_number.vacuum_start_minutes_weekdays") | int) }}'
        entity_id: input_number.vacuum_start_hour_weekdays
      vacuum_start_time_weekend:
        friendly_name: 'Start Time (weekend)'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.vacuum_start_hour_weekend") | int, states("input_number.vacuum_start_minutes_weekend") | int) }}'
        entity_id: input_number.vacuum_start_hour_weekend
      vacuum_status:
        friendly_name: "Vacuum - State"
        value_template: "State: {{ states.vacuum.xiaomi_vacuum.attributes.status }}"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_weekdays:
        friendly_name: "Vacuum - Weekdays"
        value_template: ""
      vacuum_weekend:
        friendly_name: "Vacuum - Weekend"
        value_template: ""
      vacuum_battery:
        friendly_name: "Vacuum - Battery"
        value_template: "Battery: {{ states.vacuum.xiaomi_vacuum.attributes.battery_level | float | int }}"
        icon_template: '{{ states.vacuum.xiaomi_vacuum.attributes.battery_icon }}'
        device_class: battery
        unit_of_measurement: '%'
        entity_id: vacuum.xiaomi_vacuum
      vacuum_accessories:
        friendly_name: "Vacuum - Consumption"
        value_template: "Consumption"
      vacuum_operation:
        friendly_name: "Vacuum - Operations"
        value_template: "Operations"
      vacuum_main_brush:
        friendly_name: "Vacuum - Main Brush"
        value_template: "Main Brush: {{ states.vacuum.xiaomi_vacuum.attributes.main_brush_left }} h"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_side_brush:
        friendly_name: "Vacuum - Side Brush"
        value_template: "Side Brush: {{ states.vacuum.xiaomi_vacuum.attributes.side_brush_left }} h"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_filter:
        friendly_name: "Vacuum - Filter"
        value_template: "Filter: {{ states.vacuum.xiaomi_vacuum.attributes.filter_left }} h"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_sensor:
        friendly_name: "Vacuum - Drawer"
        value_template: "Drawer: {{ states.vacuum.xiaomi_vacuum.attributes.sensor_dirty_left }} h"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_fan_speed:
        friendly_name: "Vacuum - Power"
        value_template: "Power: {{ states.vacuum.xiaomi_vacuum.attributes.fan_speed }}"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_cleaning_time:
        friendly_name: "Cleaning - Time"
        value_template: "Cleaning Time: {{ states.vacuum.xiaomi_vacuum.attributes.cleaning_time }}"
        entity_id: vacuum.xiaomi_vacuum
      vacuum_cleaned_area:
        friendly_name: Cleaned Area
        unit_of_measurement: 'm ²'
        value_template: "Cleaned - Area {{ states.vacuum.xiaomi_vacuum.attributes.cleaned_area }}"
        entity_id: vacuum.xiaomi_vacuum        
      
      vacuum_fan_speed_list:
        friendly_name: "Vacuum - Fan Speed List"
        value_template: "{{ states.vacuum.xiaomi_vacuum.attributes.fan_speed_list }}"
        entity_id: vacuum.xiaomi_vacuum

#   - platform: rest
#     resource: !secret xiaomi_vacuum_resource_map
#     name: Vacuum Map url
#     value_template: 'http://{{states("sensor.xiaomi_vacuum_ip")}}{{value_json.mapsrc}}'

#################################################################
#                                                               #
#                         Switch                                #
#                                                               #
#################################################################
switch:
  - platform: template
    switches:
      vacuum:
        friendly_name: "Vacuum"
        value_template: "{% if is_state('vacuum.xiaomi_vacuum', 'cleaning') %}on{% else %}off{% endif %}"
        turn_on:
          service: input_select.select_option
          entity_id: input_select.vacuum_zone
          data_template:
            option: "Home"
        turn_off:
          service: vacuum.return_to_base
          data:
            entity_id: vacuum.xiaomi_vacuum
        icon_template: mdi:robot-vacuum

#################################################################
#                                                               #
#                         input Select                          #
#                                                               #
#################################################################

input_select:
  vacuum_fan_speed_list:
    name: Type of aspiration
    options:
    - Silent
    - Balanced
    - Turbo
    - Maximum power

  vacuum_zone:
    name: Which area to clean?
    options:
      - Choose the area
      - Kitchen #מטבח
      - Bedroom #פרוזדור
      - Dining Room  ## פינת אוכל
      - Home #כל הבית      
      - living Room #סלון
      - Guestroom # אזור הכלבים

#################################################################
#                                                               #
#                         input Boolean                         #
#                                                               #
#################################################################

input_boolean:
  vacuum_zone_settings:
    name: zone settings
    initial: false


#################################################################
#                                                               #
#                         input Number                          #
#                                                               #
#################################################################

input_number:
  vacuum_passes:
    name: How many times?
    initial: 1
    min: 1
    max: 3
    step: 1
  vacuum_start_minutes_weekend:
    name: Start minute
    initial: 0
    min: 0
    max: 59
    step: 1
    mode: slider
  vacuum_start_hour_weekend:
    name: Start hour
    initial: 13
    min: 0
    max: 23
    step: 1
    mode: slider
  vacuum_start_minutes_weekdays:
    name: Start minute
    initial: 0
    min: 0
    max: 59
    step: 1
    mode: slider
  vacuum_start_hour_weekdays:
    name: Start hour
    initial: 15
    min: 0
    max: 23
    step: 1
    mode: slider

#################################################################
#                                                               #
#                         Automation                            #
#                                                               #
#################################################################
automation:

  - alias: Vacuum - Suction speed
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.vacuum_fan_speed_list
    action:
      - service_template: >
          {% if trigger.to_state.state == 'Silent' %}
            script.vacuum_set_quiet
          {% elif trigger.to_state.state == 'Balanced' %}
            script.vacuum_set_balanced
          {% elif trigger.to_state.state == 'Turbo' %}
            script.vacuum_set_turbo
          {% elif trigger.to_state.state == 'Maximum power' %}
            script.vacuum_set_max
          {% endif %}

  - alias: Vacuum - Selection of the area
    initial_state: 'on'
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_select.vacuum_zone
      from: 'Choose the area'
    action:
      - service_template: >
          {% if states.input_select.vacuum_zone.state == "Home" %} #כל הבית
            script.vacuum_home
          {% elif states.input_select.vacuum_zone.state == "Kitchen" %} #מטבח
            script.vacuum_kitchen
          {% elif states.input_select.vacuum_zone.state == "Bedroom" %} ## פרוזדור 
            script.vacuum_bedroom
          {% elif states.input_select.vacuum_zone.state == "Dining Room" %} ## פינת אוכל #
            script.vacuum_dining_room
          {% elif states.input_select.vacuum_zone.state == "Living Room" %} ## סלון ##
            script.vacuum_living_room
          {% elif states.input_select.vacuum_zone.state == "Guestroom" %} #אזור הכלבים
            script.vacuum_guestroom            
          {% else %}
          {% endif %}
      - delay: 00:01:00
      - wait_template: "{{is_state('vacuum.xiaomi_vacuum', 'docked')}}"
      - delay: 00:00:15
      - service: input_select.select_option
        entity_id: input_select.vacuum_zone
        data_template:
          option: "Choose the area"
      - service: input_select.select_option
        entity_id: input_select.vacuum_fan_speed_list
        data_template:
          option: "Turbo"

  - alias: Vacuum Notices
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: vacuum.xiaomi_vacuum
        to: 'paused'
        for:
          hours: 0
          minutes: 0
          seconds: 30
      - platform: state
        entity_id: vacuum.xiaomi_vacuum
        to: 'docked'
      - platform: state
        entity_id: vacuum.xiaomi_vacuum
        to: 'error'
    action:
      - service_template: >
          {% if trigger.to_state.state == 'paused' %}
            script.vacuum_alert_pause
          {% elif trigger.to_state.state == 'docked' %}
            script.vacuum_alert_charging
          {% elif trigger.to_state.state == 'error' %}
            script.vacuum_alert_error
          {% endif %}

  - id: vacuum_cleaning_timer_weekdays
    initial_state: 'off'
    alias: Vacuum Schedule cleaning on weekdays
    trigger:
    - platform: template
      value_template: '{{ states.sensor.vacuum_start_time_weekdays.state == states.sensor.time.state }}'
    condition:
    - condition: time
      weekday:
      - sun
      - mon
      - tue
      - wed
      - thu

    action:
    - data:
        entity_id: vacuum.xiaomi_vacuum
      service: vacuum.start
  - id: Vacuum_cleaning_timer_weekend
    initial_state: 'off'
    alias: Vacuum Schedule cleaning on weekends
    trigger:
    - platform: template
      value_template: '{{ states.sensor.vacuum_start_time_weekend.state == states.sensor.time.state }}'
    condition:
    - condition: time
      weekday:
      - fri      
      - sat
    action:
    - data:
        entity_id: vacuum.xiaomi_vacuum
      service: vacuum.start

#################################################################
#                                                               #
#                             Script                            #
#                                                               #
#################################################################
script:
  vacuum_set_quiet:
    sequence:
      service: vacuum.set_fan_speed
      data:
        entity_id: vacuum.xiaomi_vacuum
        fan_speed: Quiet

  vacuum_set_balanced:
    sequence:
      service: vacuum.set_fan_speed
      data:
        entity_id: vacuum.xiaomi_vacuum
        fan_speed: Balanced

  vacuum_set_turbo:
    sequence:
      service: vacuum.set_fan_speed
      data:
        entity_id: vacuum.xiaomi_vacuum
        fan_speed: Turbo
 
  vacuum_set_max:
    sequence:
      service: vacuum.set_fan_speed
      data:
        entity_id: vacuum.xiaomi_vacuum
        fan_speed: Max

#################################################################
#                            ZONE                               #
#################################################################
  vacuum_home:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: [[23341,19451,32741,30201]] #כל הבית
    - service: script.vacuum_alert_start
    
  vacuum_cocina:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: !secret vacuum_cocina
    - service: script.vacuum_alert_start

  vacuum_bedroom:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: [[28489,22014,30139,26214,1]] #פרוזדור
    - service: script.vacuum_alert_start

  vacuum_dining_room:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: [[26253,21948,28503,25948]] #פינת אוכל
    - service: script.vacuum_alert_start

  vacuum_living_room:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: [[23327,21890,26077,26040]] #סלון 
    - service: script.vacuum_alert_start

  vacuum_entrada:
    sequence:
    - service: vacuum.xiaomi_clean_zone
      data:
        entity_id: vacuum.xiaomi_vacuum
        repeats_template: '{{states.input_number.vacuum_passes.state|int}}'
        zone: !secret vacuum_entrada
    - service: script.vacuum_alert_start


#################################################################
#                         Alerts                                #
#################################################################
  vacuum_alert_start:
    alias: Vacuum alert start
    sequence:
    - service: notify.telegram_diego
      data_template:
        title: "House alert"
        message: "The robot has started cleaning the area: {{ states.input_select.vacuum_zone.state | lower }}."
    - service: notify.telegram_diego
      data:
        title: "House alert ({{now().strftime('%H:%M')}})"
        message: "The robot has started cleaning the area: {{ states.input_select.vacuum_zone.state | lower }}"

  vacuum_alert_pause:
    alias: Vacuum alert pausa
    sequence:
    - service: notify.telegram_diego
      data_template:
        title: "House alert"
        message: "The vacuum cleaner has stopped cleaning the area: {{ states.input_select.vacuum_zone.state | lower }}. {% if 'error' in states.vacuum.xiaomi_vacuum.state %}Error code: {{ states.vacuum.xiaomi_vacuum.attributes.error }}.{% else %}{% endif %}"
    - service: notify.telegram_diego
      data:
        title: "House alert ({{now().strftime('%H:%M')}})"
        message: "The vacuum cleaner has stopped cleaning the area: {{ states.input_select.vacuum_zone.state | lower }}. {% if 'error' in states.vacuum.xiaomi_vacuum.state %}Error code: {{ states.vacuum.xiaomi_vacuum.attributes.error }}.{% else %}{% endif %}"

  vacuum_alert_back_to_home:
    alias: Vacuum alert back to home
    sequence:
    - condition: template
      value_template: "{% if is_state('input_select.vacuum_zone', 'Choose the area') %}False{% else %}True{% endif %}"
    - service: notify.telegram_diego
      data_template:
        title: "House alert"
        message: "The robot has finished cleaning the area {{ states.input_select.vacuum_zone.state | lower }}. cleaned {{ states.vacuum.xiaomi_vacuum.attributes.cleaned_area }} square meters in {{ states.vacuum.xiaomi_vacuum.attributes.cleaning_time }} minutes and now it is loading"
    - service: notify.avitelegram
      data:
        title: "House alert ({{now().strftime('%H:%M')}})"
        message: "The robot has finished cleaning the area {{ states.input_select.vacuum_zone.state | lower }}. cleaned {{ states.vacuum.xiaomi_vacuum.attributes.cleaned_area }} square meters in {{ states.vacuum.xiaomi_vacuum.attributes.cleaning_time }} minutes and now it is loading."

  vacuum_alert_error:
    alias: Vacuum alert error
    sequence:
    - service: notify.telegram_diego
      data_template:
        title: "House alert"
        message: "The vacuum cleaner stopped and reported an error while cleaning the area {{ states.input_select.vacuum_zone.state | lower }}."
    - service: notify.telegram_diego
      data:
        title: "House alert ({{now().strftime('%H:%M')}})"
        message: "The vacuum cleaner stopped and reported an error while cleaning the area {{ states.input_select.vacuum_zone.state | lower }}."

#################################################################
#                                                               #
#                             Customize                         #
#                                                               #
#################################################################

homeassistant:
  customize:

    switch.vacuum:
      icon: mdi:robot-vacuum

    input_select.vacuum_fan_speed_list:
      icon: mdi:speedometer

    input_select.vacuum_zone:
      icon: mdi:map-marker

    input_number.vacuum_passes:
      name: Pass
      icon: mdi:shuffle-disabled
