#-------------------------------------------
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# Neato Support for D7 Connected Botvac - control my [Neato Vacuum](https://amzn.to/2kqnnqu) with Home Assistant.
#-------------------------------------------

##############################################################################
###  Configuration - Authentication via the DEVELOPER Portal
##############################################################################

# neato:
#   client_id: !secret neato_client_id
#   client_secret: !secret neato_client_secret

##############################################################################
###  Automations - Tweet out When neato is heading back to his Dock for recharging.
##############################################################################
automation:
  - alias: 'Vacuum -Tweet'
    id: 2346efcd-e8a8-4e86-b5b6-43e54a72a95f
    mode: single
    trigger:
      - platform: state
        entity_id: vacuum.carlo_neato
        to: 'cleaning'

    condition:

      - condition: numeric_state
        entity_id: vacuum.carlo_neato
        attribute: battery_level
        below: 20

      - condition: template
        value_template: >
          {%- if states.automation.vacuum_tweet.attributes.last_triggered -%}
            {{ (as_timestamp(now()) - as_timestamp(states.automation.vacuum_tweet.attributes.last_triggered)) > 15000 }}
          {%- else -%}
            true
          {%- endif -%}

    action:
      - service: script.tweet_engine_image
        data:
          tweet: >
            {{ [
            "#Neato returning to base. Vacuumed up a TON. (https://amzn.to/2CKHZya)",
            "Everyday I have the Neato Botvac go out and clean the house on its own.",
            "Since we have a cat that sheds all day, we have Neato Vacuum go out daily to clean.",
            "Time to recharge the Neato Vacuum."
            ] | random + " Battery Level:{{states.vacuum.carlo_neato.attributes.battery_level }} #Robots"}}
          image: >-
            {{ [
            "/config/www/custom_ui/floorplan/images/branding/neato.png"
            ] | random }}

##############################################################################
###  Automations - Help Neato!
### https://www.vcloudinfo.com/2020/05/home-assistant-neato-vacuum-automation.html
##############################################################################

  - alias: 'Help Neato'
    id: 6548de52-a4a4-4df2-9d66-9c2c15577a7e
    trigger:
      - platform: state
        entity_id: vacuum.carlo_neato
        to: 'error'
      - platform: event
        event_type: event_did_someone_help_neato_loop

    condition:
        - condition: state
          entity_id: vacuum.carlo_neato
          state: 'error'

    action:
      - wait_template: "{{ states.group.family.state == 'home' }}"
      - wait_template: "{{ is_state('group.bed', 'off') }}"
      - delay: 00:05:00

      - service: script.speech_engine
        data:
          value1: >
            {% set error =  states.vacuum.carlo_neato.attributes['status'] %}
            {{ [
            "Neato is asking for help.  The error reported is " ~ error + " .  Please find him and help him.",
            "Please be nice and help Neato.  He is complaining about " ~ error + ".",
            "If you don't want to vacuum, you need to help Neato.  He is complaining about " ~ error +" and needs help.",
            "Something is wrong with Neato.  Please find him and help him.  I have detected a " ~ error + " error."
            ] | random }}

      - service: vacuum.locate
        entity_id: vacuum.carlo_neato

      - service: script.notify_engine
        data:
          title: 'Help Neato'
          value1: "{{ states.vacuum.carlo_neato.attributes['status'] }}"
          who: 'family'
          ios_category: 'camera'
          camera_entity: 'camera.carlo_neato_cleaning_map'
          content_type: 'jpeg'
          apns_id: 'information'

      - delay: 00:20:00
      - event: event_did_someone_help_neato_loop