#### STARTUP PREAMBLE ####

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 49.3193
  longitude: -124.3136
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 66
  # metric for Metric, imperial for Imperial
  unit_system: imperial
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: UTC
  
  customize:
    switch.open_blackout_blinds:
      assumed_state: false
    switch.close_blackout_blinds:
      assumed_state: false
    switch.stop_blackout_blinds:
      assumed_state: false
    #script.slider_moved_open:
      #hidden: true
    #script.slider_moved_close:
      #hidden: true    


# Enables the frontend
frontend:

http:
  # Uncomment this to add a password (recommended!)
  # api_password: PASSWORD
  # Uncomment this if you are using SSL or running in Docker etc
  # base_url: example.duckdns.org:8123

# Checks for available updates
updater:

# Discover some devices automatically
# discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
# conversation:

# Enables support for tracking state changes over time.
history:

# View all events in a logbook
logbook:

#### END STARTUP PREAMBLE ####

#### BEGIN STARTUP SETTINGS ####

group:

  #### default cannot contain tabbed group
  #default_view:
    #view: yes
    #entities:
      #- group.sensor_bank1

  theater_controls:
    name: Theater Controls
    view: no
    entities:
      - input_slider.theater_blackout_blinds
      - switch.open_blackout_blinds
      - switch.close_blackout_blinds
      - switch.stop_blackout_blinds

  #blind_sensor_group_1:
    #name: Blind Sensor Group 1
    #view: no
    #entities:
      #- sensor.control_status
      #- sensor.slider_started
      #- sensor.slider_stopped
      #- sensor.percent_travel
      #- sensor.direction_travel
      #- sensor.travel_timing
      

# USING mosquitto BROKER, NO AUTHENTICATION
mqtt:
  broker: 192.168.1.71
  port: 1883
  client_id: home-assistant-1
  #protocol: 3.1.1
  #username: mosquitto
  #password: raspberry

  #publish.single('setblinds', 'good', hostname=broker)

input_slider:
  theater_blackout_blinds:
    name: 'Theater Blackout Blinds'
    icon: mdi:window-closed
    min: 0
    max: 100
    step: 25
    initial: 0

switch 10:
  platform: broadlink
  host: 192.168.1.249
  mac: 'b4:43:0d:ee:e7:bf'
  timeout: 15
  switches:
    blinds:
      friendly_name: "Open Blackout Blinds"
      command_off: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
      command_on: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='

switch 20:
  platform: broadlink
  host: 192.168.1.249
  mac: 'b4:43:0d:ee:e7:bf'
  timeout: 15
  switches:
    blinds:
      friendly_name: "Close Blackout Blinds"
      command_off: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
      command_on: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='

switch 30:
  platform: broadlink
  host: 192.168.1.249
  mac: 'b4:43:0d:ee:e7:bf'
  timeout: 15
  switches:
    blinds:
      friendly_name: "Stop Blackout Blinds"
      command_off: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
      command_on: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='

sensor 1:
  - platform: mqtt
    name: "Slider Started"
    state_topic: "setStart/#"

sensor 2:
  - platform: mqtt
    name: "Slider Stopped"
    state_topic: "setStop/#"

sensor 3:
  - platform: mqtt
    name: "Percent Travel"
    state_topic: "setPctTravel/#"

sensor 4:
  - platform: mqtt
    name: "Direction Travel"
    state_topic: "setDirection/#"

sensor 5:
  - platform: mqtt
    name: "Travel Timing"
    state_topic: "setTravelTime/#"

sensor 6:
  - platform: mqtt
    name: "Control STATUS"
    state_topic: "setStatus/#"

#### END SENSOR READINGS ####

automation:

#### AUTOMATION TRIGGERED BY HASS RESTART ####

  - alias: 'HASS Startup Automation'
    hide_entity: true
    trigger:
      platform: event
      event_type: homeassistant_start

    action:

      # SET SENSOR TEXT TO PREVENT SLIDER EVENT 
      # FIRING AT STARTUP [see conditions]

      - service: mqtt.publish
        data:
          topic: "setStatus"
          retain: true
          payload: 'Start'

      # RESTORE SLIDER TO CURRENT POSITION AT STARTUP
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.theater_blackout_blinds
          value: '{{states("sensor.slider_stopped")|round(0)}}'

      # SET SENSOR TEXT TO ALLOW SLIDER EVENT TRIGGER
      - service: mqtt.publish
        data:
          topic: "setStatus"
          retain: true
          payload: 'DONE'


#### AUTOMATION TRIGGERED BY SLIDER CHANGE ####

  - alias: 'Theater Blackout Blind Slider Automation'
    hide_entity: True
    trigger:
      platform: state
      entity_id: input_slider.theater_blackout_blinds
   
    # THESE CONDITIONS MUST ALL BE MET FOR TRIGGER TO WORK
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: 'script.slider_moved_open'
          state: 'off'
        - condition: state
          entity_id: 'script.slider_moved_close'
          state: 'off'
        - condition: template
          value_template: '{{ states("sensor.control_status") == "DONE" }}'
        

    action:
      service: script.turn_on

      data_template:

        #### SCRIPT DIRECTION DECISION re OPEN or CLOSE ####

        entity_id: >
          {% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
            script.slider_moved_close
          {% else %}
            script.slider_moved_open
          {% endif %}

        #### VARIABLE VALUES PASSED TO SCRIPTS ####
         
        variables:
          set_start: '{{ trigger.from_state.state | int }}'
          set_stop: '{{ trigger.to_state.state | int }}'
          full_excursion: '30' #seconds

          travel_pct: >
            {% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
              {{ trigger.from_state.state | int - trigger.to_state.state | int }}
            {% else %}
              {{ trigger.to_state.state | int - trigger.from_state.state | int }}
            {% endif %}

          direction_travel: >
            {% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
              CLOSE
            {% else %}
              OPEN
            {% endif %}

          travel_time: >
            {% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
              {{ (( trigger.from_state.state | int - trigger.to_state.state | int)/100*30) | round(0) }}
            {% else %}
              {{ ((trigger.to_state.state | int - trigger.from_state.state | int)/100*30) | round(0) }}
            {% endif %}


script:
  slider_moved_open:
    sequence:

      #### YOU CAN DYNAMICALLY CONTROL THE VISIBILITY OF GROUPS [not used here]

      #- service: group.set_visibility
        #data: 
          #entity_id: group.theater_blackout_blinds
          #visible: true

      #### UPDATE SENSOR DISPLAY

      - service: mqtt.publish
        data_template:
          topic: "setStatus"
          retain: true
          payload: 'Moving'

      - service: mqtt.publish
        data_template:
          topic: "setStart"
          retain: true
          payload: '{{ set_start }}'

      - service: mqtt.publish
        data_template:
          topic: "setStop"
          retain: true
          payload: '{{ set_stop }}'

      - service: mqtt.publish
        data_template:
          topic: "setPctTravel"
          retain: true
          payload: '{{ travel_pct }}'

      - service: mqtt.publish
        data_template:
          topic: "setDirection"
          retain: true
          payload: '{{ direction_travel }}'

      - service: mqtt.publish
        data_template:
          topic: "setTravelTime"
          retain: true
          payload: '{{ travel_time }}'

      - delay: 00:00:04

      - service: switch.turn_on
        entity_id: switch.open_blackout_blinds

      - delay: 00:00:0{{ travel_time }}

      #- delay: 00:00:04

      - service: switch.turn_off
        entity_id: switch.open_blackout_blinds

      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.theater_blackout_blinds
          value: '{{ set_stop }}'

      - service: mqtt.publish
        data_template:
          topic: "setStatus"
          retain: true
          payload: 'DONE'

      - delay: 00:00:02


  slider_moved_close:
    sequence:

      - service: mqtt.publish
        data_template:
          topic: "setStatus"
          retain: true
          payload: 'Moving'

      - service: mqtt.publish
        data_template:
          topic: "setStart"
          retain: true
          payload: '{{ set_start }}'

      - service: mqtt.publish
        data_template:
          topic: "setStop"
          retain: true
          payload: '{{ set_stop }}'

      - service: mqtt.publish
        data_template:
          topic: "setPctTravel"
          retain: true
          payload: '{{ travel_pct }}'

      - service: mqtt.publish
        data_template:
          topic: "setDirection"
          retain: true
          payload: '{{ direction_travel }}'

      - service: mqtt.publish
        data_template:
          topic: "setTravelTime"
          retain: true
          payload: '{{ travel_time }}'

      - delay: 00:00:04

      - service: switch.turn_on
        entity_id: switch.close_blackout_blinds

      - delay: 00:00:0{{ travel_time }}

      #- delay: 00:00:04

      - service: switch.turn_off
        entity_id: switch.close_blackout_blinds

      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.theater_blackout_blinds
          value: '{{ set_stop }}'

      - service: mqtt.publish
        data_template:
          topic: "setStatus"
          retain: true
          payload: 'DONE'

      - delay: 00:00:02


#-------------------------------------------------