#-------------------------------------------
# HACS Related Information
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
# https://github.com/CCOSTAN/Home-AssistantConfig/issues/797
#-------------------------------------------

automation:
  - alias: HACS Component Update Notification
    id: e5266b0b-017f-4c69-83a3-357aaa03dbe2
    mode: single
    trigger:
      - platform: state
        entity_id: sensor.hacs

    condition:
      - condition: numeric_state
        entity_id: sensor.hacs
        above: 0

    action:
      - service: ifttt.trigger
        data_template:  {"event":"GitPost", "value2":"HACS - {{ as_timestamp(now()) | timestamp_custom('%I:%M:%S %p %d%b%Y', true) }}", "value1":"Update to HACS {{state_attr('sensor.hacs','repositories')}}: There is {{ states.sensor.hacs.state }} update."}

      - service: script.tweet_engine_image
        data_template:
          tweet: >-
            Hey @CCostan - There are {{ states.sensor.hacs.state }} updates to HACS components:
            {%- for repo in state_attr("sensor.hacs","repositories") -%}
            {{repo['display_name'] }} {{ repo['installed_version'] }} to {{ repo['available_version'] }}
            {%- endfor %}
          image: >-
            {% set pictures = [
              "/config/www/custom_ui/floorplan/images/branding/repo_ad.png",
              "/config/www/custom_ui/floorplan/images/branding/repo_ad2.png",
              "/config/www/custom_ui/floorplan/images/branding/repo_ad3.png",
              "/config/www/custom_ui/floorplan/images/branding/bearstoneflow.png",
              "/config/www/custom_ui/floorplan/images/branding/repo_ad4.png"
            ] %}
            {{ pictures|random }}
          who: 'BearStoneHA'

      - delay:
          days: 1