#-------------------------------------------
#  This is the configuration account for @BearStoneHA - My Home's automated twitter account.
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
#-------------------------------------------
# homeassistant:
#   customize_glob:
#     "input_select.log_*":
#       icon: mdi:bug
#
#       hidden: False

#Random number - https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new

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

### Building out some Historical stats for tweeting. #####################
sensor:

  - platform: mqtt
    state_topic: "ifttt/message"
    name: "ifttt formatted message"

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

  # - platform: history_stats
  #   name: HVAC Time
  #   entity_id: sensor.downstairs_thermostat_hvac_state
  #   state: 'on'
  #   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

group:
  tweet_stats:
    entities:
      - sensor.doorbell_presses
      - sensor.landscaping_light_time
      - sensor.hvac_time

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

automation:
  - alias: 'New Twitter follower!'
    hide_entity: True
    trigger:
      - platform: event
        event_type: 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/sounds/twitter-chirp.mp3"
          media_content_type: audio/mp4

      - service: script.tweet_engine
        data_template:
          tweet: >-
            {% set handle = trigger.event.data.value1 %}
            {% set location = trigger.event.data.location %}
            {% set phrases = [
              "Thanks for the Follow " ~ handle ~ "! Chirp the Bird from " ~ location ~ "!!",
              "Hey " ~ handle ~ "! Thanks for the Follow from " ~ location ~ " !",
              "New follower " ~ handle ~ " from " ~ location ~ ", I just sounded a chirp in the house.",
              "Boom! The " ~ location ~ " follow from " ~ handle ~ " makes me sound a chirp in the house.",
              "Wanna know who is super cool? "~ handle ~" IS!!! Thanks for making me CHIRP!",
              "If the AMP (http://amzn.to/2j18dlT) is on & I get a new follower like " ~ handle ~ ", I sound a chirp!"
            ] %}
            {% set hashtags = [
              "#ThanksForTheFollow",
              "#FollowBack",
              "#ChirpTheHouse",
              "#FollowMe",
              "#MakingitChirp",
              "(http://www.vmwareinfo.com/2017/12/giving-house-twitter-account.html)",
              "(http://www.vmwareinfo.com/2017/11/building-digital-cuckoo-clock-with-home.html)"
            ] %}
            {{ phrases|random ~ " " ~ hashtags|random }}

  - alias: 'Closed Github Issue'
    hide_entity: True
    trigger:
      - platform: event
        event_type: closed_github

    action:
      - service: script.tweet_engine
        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 Alert: " ~ issue ~ "(" ~ issueurl ~ "). Repo: (https://github.com/CCOSTAN/Home-AssistantConfig/commits/master) "
            ] %}
            {% set hashtags = [
              "#Github",
              "#HomeAutomation",
              "#SmartHomeCode"
            ] %}
            {{ phrases|random ~ " " ~ hashtags|random }}


  - alias: 'Random House stats'
    hide_entity: True
    trigger:
      - platform: time
        hours: '/3'
        minutes: 01
        seconds: 00
    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(10,59) | random | int }}:00'
      - service: script.tweet_engine
        data_template:
          tweet: >
            {{ [
            "The number of Doorbell presses yesterday was {{states.sensor.doorbell_presses.state}}",
            "The #Landscaping lights where on for about {{states.sensor.landscaping_light_time.attributes.value}} yesterday.",
            "So far, I have prevented {{states.sensor.pihole_ads_blocked_today.state}} ads from hitting the network via Pi-hole! http://www.pi-hole.net",
            "Pi-hole (http://www.pi-hole.net) has serviced {{states.sensor.pihole_dns_queries_today.state}} DNS requests today! #PiZero",
            "{{states.sensor.doorbell_presses.state}} people came to the door today and pressed the bell.",
            "The #Docker Host has been running for {{states.sensor.since_last_boot_templated.state}}",
            "The #Docker Host for Home Assistant is using {{states.sensor.disk_used_.state}}% of the hard drive currently. ",
            "The {{states.media_player.living_room_ultra.attributes.friendly_name}} #Roku is playing #{{states.media_player.living_room_ultra.attributes.app_name}}.",
            "The {{states.media_player.upstairs_living_room.attributes.friendly_name}} #Roku is playing #{{states.media_player.upstairs_living_room.attributes.app_name}}. (http://amzn.to/2lYZEup)",
            "I am running Home Assistant version {{states.sensor.ha_installed_version.state}} (https://github.com/CCOSTAN/Home-AssistantConfig)",
            "{{states.sensor.doorbell_presses.state}} doorbell presses occurred in the last 24 hours.",
            "I keep the average humidity of the house at {{states.sensor.downstairs_thermostat_humidity.state}} percent. Outside is {{states.sensor.dark_sky_humidity.state}} #Nest (http://amzn.to/2BWNk5N)",
            "Outside is {{states.sensor.dark_sky_temperature.state}}. I keep the average temperature at {{states.sensor.downstairs_thermostat_temperature.state}}. #Nest (http://amzn.to/2BWNk5N)",
            "I know that it will be {{states.sensor.dark_sky_minutely_summary.state}} So I will adjust the Heating/Cooling, irrigation and lighting accordingly. #Nest #Rachio #Hue",
            "Average internet stats are Download: {{states.sensor.speedtest_download.state}} Mbit/s & Upload {{states.sensor.speedtest_upload.state}} Mbit/s.",
            "Todays Sleep Number is {{states.sensor.sleepnumber_carlo_stacey_sleepnumber.state}}. Wifi connected Bed FTW! #SleepStat (http://amzn.to/2D10BcQ)",
            "Our Sleep Number is {{states.sensor.sleepnumber_carlo_carlo_sleepnumber.state}}. Wifi connected Bed! #SleepStat (http://amzn.to/2D10BcQ)",
            "The current UV index is {{states.sensor.dark_sky_uv_index.state}} and the Moon is {{states.sensor.moon.state}}.  #Space",
            "Home Assistant has been running for {{states.sensor.ha_uptime.state}} minutes. (https://github.com/CCOSTAN/Home-AssistantConfig)",
            "My @getspectrum internet download speed is about {{states.sensor.speedtest_download.state}} Mbit/s and I am a #cordcutter. @TabloTV",
            "Average Cloud Coverage: {{states.sensor.dark_sky_cloud_coverage.state}}%, Average Temp: {{states.sensor.dark_sky_temperature.state}}°F #Solar (http://www.vmwareinfo.com/2018/01/going-green-to-save-some-green-in-2018.html)"
            ] | random + " #RandomStat"}}