#-------------------------------------------
#  Space Related Packages
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
#-------------------------------------------
#------ISS----------------------------------
#-------------------------------------------
# binary_sensor:
#   - platform: iss
#     show_on_map: true
#-------------------------------------------

sensor:

  - platform: rest
    scan_interval: 1800
    name: SpaceX Starman
    json_attributes:
      - earth_distance_mi
    value_template: '{{ value_json["speed_mph"] }}'
    unit_of_measurement: "mph"
    resource: 'https://api.spacexdata.com/v2/info/roadster'

  - platform: rest
    scan_interval: 1800
    name: SpaceX
    json_attributes:
      - mission_name
      - launch_site
      - rocket
    value_template: '{{ value_json["launch_date_unix"] }}'
    resource: 'https://api.spacexdata.com/v2/launches/next'

automation:
  - alias: Launch Window Approaching
    id: 1d42fc4f-a37d-4283-b64b-09242a145598
    trigger:
      - platform: template
        value_template: '{{ (now().strftime("%s") | int + 600) == (states.sensor.spacex.state | int) }}'

    action:
      - service: script.notify_engine
        data:
          value1: 'Go Outside!  There is a Rocket Launch very soon! {{states.sensor.next_launch.attributes.stream }}'
          apns_id: 'information'

      - service: script.tweet_engine_no_image
        data:
          tweet: >-
            {{ [
            "There is a Rocket launch happening right now!  I can see it if I look closely. {{states.sensor.next_launch.attributes.stream }}",
            "Check out {{ states('sensor.next_launch') }} on {{states.sensor.next_launch.attributes.stream }} right now.  Launch Window is opening."
            ] | random + " #Space #SpaceX #ManKind " }}

# This automation was also moved to a macro in the speech_engine.
# It's a random fact now.

  - alias: 'ISS is above Me -Tweet'
    id: 7f0610b6-508e-4409-b39d-620045164715

    trigger:
      - platform: state
        entity_id:
          - binary_sensor.iss
        to: 'on'
        from: 'off'

    action:
      - service: script.tweet_engine_image
        data:
          tweet: >-
            {{ [
            "The ISS is above me right now!",
            "The International Space Station flys by once a day & that time is NOW! (For me)",
            "I can look up RIGHT NOW and see the International Space Station.",
            "The #ISS just flew by and there is 1 Starman driving a #Tesla and {{states.binary_sensor.iss.attributes.number_of_people_in_space}} people in",
            "The #ISS just flew by and there are {{states.binary_sensor.iss.attributes.number_of_people_in_space}} people in",
            "The International Space Station Rocks! And it's above me RIGHT NOW!"
            ] | random + " #Space"}}
          image: >-
            {{ [
            "/config/www/custom_ui/floorplan/images/branding/space.png",
            "/config/www/custom_ui/floorplan/images/branding/space2.png",
            "/config/www/custom_ui/floorplan/images/branding/starman.png"
            ] | random }}

  - alias: 'Full Moon -Tweet'
    id: 2382bec3-53c6-4dc9-9b0f-1cee0b8f9735
    trigger:
      - platform: state
        entity_id: sensor.moon
        to: 'full_moon'
    action:
      - delay: '0{{ (range(1, 5)|random|int) }}:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}:00'
      - service: script.tweet_engine_image
        data:
          tweet: >-
            {{ [
            "There is a Full Moon out tonight!",
            "Wish my solar panels picked up Moon rays.  Full Moon out tonight.",
            "The moon is huge! And full. ",
            "Turn down the lights, there is a Full Moon out tonight.",
            "If you want to see the full moon, tonight is the night.",
            "Get out your telescopes, it will be a Full Moon out tonight!",
            "Not a half moon, Not a quarter moon.  There is a FULL moon out tonight."
            ] | random + "#Space"}}
          image: >-
            {{ [
            "/config/www/custom_ui/floorplan/images/branding/moon.png",
            "/config/www/custom_ui/floorplan/images/branding/moon2.png",
            "/config/www/custom_ui/floorplan/images/branding/moon3.png",
            "/config/www/custom_ui/floorplan/images/branding/starman.png"
            ] | random }}