#-------------------------------------------
#  This is the configuration account for @BearStoneHA - My Homes automated twitter account.
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
#-------------------------------------------

notify:
  - name: BearStoneHA
    platform: twitter
    consumer_key: !secret twitter_consumer_key
    consumer_secret: !secret twitter_consumer_secret
    access_token: !secret twitter_access_token
    access_token_secret: !secret twitter_access_token_secret
  - name: ccostan
    platform: twitter
    consumer_key: !secret twitter_consumer_key2
    consumer_secret: !secret twitter_consumer_secret2
    access_token: !secret twitter_access_token2
    access_token_secret: !secret twitter_access_token_secret2

### Building out some Historical stats for tweeting. #####################
sensor:
  - platform: command_line
    command: 'curl https://api.github.com/repos/ccostan/home-assistantconfig'
    name: GitHub Stats
    unit_of_measurement: 'Count'
    scan_interval: 20000
    value_template: '{{ value_json.name }}'
    json_attributes:
      - open_issues
#      - subscribers_count
#      - forks
      - stargazers_count

  - platform: command_line
    name: Lines of Code
    command: "find /config -name '*.yaml' | xargs cat | wc -l"
    unit_of_measurement: 'Lines'
    scan_interval: 20000

  - platform: history_stats
    name: Doorbell Presses
    entity_id: binary_sensor.skybell_front_door_button
    state: 'on'
    type: count
    end: '{{ now() }}'
    duration:
      hours: 24

  - platform: history_stats
    name: Speech time
    entity_id: media_player.livingroomCC
    state: 'playing'
    type: time
    end: '{{ now() }}'
    duration:
      hours: 24

  - platform: history_stats
    name: TV time
    entity_id: media_player.living_room_ultra
    state: 'playing'
    type: time
    end: '{{ now() }}'
    duration:
      hours: 24

  - platform: history_stats
    name: Landscaping Light time
    entity_id: group.landscaping
    state: 'on'
    type: time
    end: '{{ now() }}'
    duration:
      hours: 24

  - platform: history_stats
    name: Bolt Charging time
    entity_id: sensor.carlojuice_charging_status
    state: 'charging'
    type: time
    end: '{{ now() }}'
    duration:
      days: 7

  - platform: version
    name: HA Installed Version
    scan_interval: 86400

  - platform: command_line
    name: "HA Uptime"
    command: echo "$(($(date +%s) - $(date -d "$(head -n1 /config/home-assistant.log | cut -d' ' -f-2)" +%s)))"
    scan_interval: 720
    value_template: >-
      {% set uptime = value | int %}
      {% set seconds = uptime % 60 %}
      {% set minutes = ((uptime % 3600) / 60) | int %}
      {% set hours = ((uptime % 86400) / 3600) | int %}
      {% set days = (uptime / 86400) | int %}
      {%- if days > 0 -%}
        {%- if days == 1 -%}
          1 day
        {%- else -%}
          {{ days }} days
        {%- endif -%}
        {{ ', ' }}
      {%- endif -%}
      {%- if hours > 0 -%}
        {%- if hours == 1 -%}
          1 hour
        {%- else -%}
          {{ hours }} hours
        {%- endif -%}
        {{ ', ' }}
      {%- endif -%}
      {%- if minutes > 0 -%}
        {%- if minutes == 1 -%}
          1 minute
        {%- else -%}
          {{ minutes }} minutes
        {%- endif -%}
      {%- endif -%}

### Building some interesting stats for tweeting. ###
  - platform: template
    sensors:
      sensor_count:
        friendly_name: 'Number of Sensors'
        entity_id: []
        value_template: >-
          {{ states.sensor | list | count }}

      automation_count:
        friendly_name: 'Number of Automations'
        entity_id: []
        value_template: >-
          {{ states.automation| list | count }}

      script_count:
        friendly_name: 'Number of Scripts'
        entity_id: []
        value_template: >
          {{ states.script | list | count }}

      binary_sensor_count:
        friendly_name: 'Number of Binary Sensors'
        entity_id: []
        value_template: >-
          {{ states.binary_sensor | list | count }}

      tracker_count:
        friendly_name: 'Number of Devices'
        entity_id: []
        value_template: >-
          {{ states.device_tracker| list | count }}
      lights_count:
        friendly_name: 'Number of Lights'
        entity_id: []
        value_template: >
          {{ states.light | list | count }}

      protect_count:
        friendly_name: 'Number of Smoke Detectors'
        entity_id: []
        value_template: >
          {% if states('group.protects') == 'on' %}
          {% for e in states.group.protects.attributes.entity_id if states(e) == 'on'  %}
          {% if loop.last %}
          {{ loop.index }}
          {% endif %}
          {%- endfor -%}
          {% else %}
          0
          {% endif %}
      camera_count:
        friendly_name: 'Number of online Cameras'
        entity_id: []
        value_template: >
          {{ states.camera | list | count }}

#############################################################

automation:

  - alias: 'New Twitter follower!'
    id: 61cd0bab-4743-4392-8e43-b204d7ebe817
    trigger:
      platform: event
      event_type: ifttt_webhook_received
      event_data:
        action: new_follower

    condition:
      - condition: state
        entity_id: group.bed
        state: 'off'

    action:
      - service: media_player.play_media
        data_template:
          entity_id:
            - media_player.livingroomCC
            - media_player.bedroom_alarm_panel
          media_content_id: "https://raw.githubusercontent.com/CCOSTAN/Home-AssistantConfig/master/config/sounds/twitter-chirp.mp3"
          media_content_type: audio/mp4

  - alias: 'Closed Github Issue'
    id: cccd9bef-17d9-48ae-87e3-6cf883712779
    trigger:
        platform: event
        #Sent from IFTTT Webhooks : {"action":"closed_github", "repo":" {{RepositoryName}}", "issue":" {{IssueTitle}}","issueurl":" {{IssueURL}}"}
        event_type: ifttt_webhook_received
        event_data:
          action: closed_github

    action:
      - service: script.tweet_engine_image
        data_template:
          tweet: >-
            {% set repo = trigger.event.data.repo %}
            {% set issue = trigger.event.data.issue %}
            {% set issueurl = trigger.event.data.issueurl %}
            {% set phrases = [
              "New Code: " ~ issue ~ " " ~ issueurl
            ] %}
            {% set hashtags = [
              "#Github",
              "#SmartHomeCode"
            ] %}
            {{ phrases|random ~ " " ~ hashtags|random }}
          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 }}

  - alias: 'Random House stats'
    id: b274a40d-cb67-4d6a-94d1-5fa685ae5ea4
    trigger:
      - platform: time_pattern
        hours: '/2'
        minutes: 45
    condition:
      - condition: template
        value_template: >
          {%- if states.automation.random_house_stats.attributes.last_triggered -%}
            {{ (as_timestamp(now()) - as_timestamp(states.automation.random_house_stats.attributes.last_triggered)) > 10000 }}
          {%- else -%}
            true
          {%- endif -%}
    action:
      - delay: '0{{ range(0,2) | random | int }}:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}'
      - service: script.tweet_engine_random
        data_template:
          pick: >-
            {% set pick = [
              "solar",
              "robot",
              "door",
              "weather",
              "cloud",
              "speedtest",
              "internet",
              "stocks",
              "night",
              "tv",
              "promo",
              "joke"
            ] %}
            {{ pick|random }}

  - alias: 'Random YouTube Videos BearStone'
    id: b6554d97-a3d1-48e4-9035-632ac622504b
    trigger:
      - platform: time_pattern
        hours: '/7'
        minutes: 18
    condition:
      - condition: template
        value_template: >
          {%- if states.automation.random_youtube_videos_bearstone.attributes.last_triggered -%}
            {{ (as_timestamp(now()) - as_timestamp(states.automation.random_youtube_videos_bearstone.attributes.last_triggered)) > 10000 }}
          {%- else -%}
            true
          {%- endif -%}
    action:
      - delay: '0{{ range(0,2) | random | int }}:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}'
      - service: script.tweet_youtube_engine_setup
        data_template:
          pick: !include ../templates/speech/youtube_picker.yaml

  - alias: 'Random YouTube Videos CCostan'
    id: 3e8a4db0-165b-4e1b-acb0-ac4503525660

    trigger:
      - platform: time_pattern
        hours: '/22'
        minutes: 45
    condition:
      - condition: template
        value_template: >
          {%- if states.automation.random_youtube_videos_ccostan.attributes.last_triggered -%}
            {{ (as_timestamp(now()) - as_timestamp(states.automation.random_youtube_videos_ccostan.attributes.last_triggered)) > 10000 }}
          {%- else -%}
            true
          {%- endif -%}
    action:
      - service: script.tweet_youtube_engine_setup
        data_template:
          who: 'ccostan'
          pick: !include ../templates/speech/youtube_picker.yaml