substitutions:
  node_name: garmotion
  wifi_static_ip:  !secret garmotion_ip
  mqtt_secretword: !secret garmotion_mqtt_secretword
  mqtt_password:   !secret garmotion_mqtt_password
  log_level: DEBUG

  espin_led:     'D4'  # GPIO 2
  espin_button:  'D3'  # GPIO 0

  espin_scl:     'D1'  # GPIO 5
  espin_sda:     'D2'  # GPIO 4

  external_led:  'D8'  # GPIO 15

  # This is coarse-grained. We play dead for a period of time, but
  # the clock we check to see the elapsed time is only updated
  # every once in a while. That's just to reduce clutter when
  # looking at the debug logs since the uptime sensor debug logs
  # on every update.
  PLAY_DEAD_PERIOD:       '60'   # seconds
  UPTIME_UPDATE_PERIOD:  '20s' # time with unit

esphome:
  name: ${node_name}
  platform: ESP8266
  board: d1_mini

wifi:
  ssid:      !secret wifi_ssid
  id:        !secret wifi_ssid
  password:  !secret wifi_password
  power_save_mode: high
  fast_connect: off
  manual_ip:
    static_ip: ${wifi_static_ip}
    gateway: !secret wifi_gateway
    subnet:  !secret wifi_subnet
    dns1:    !secret wifi_dns1
    dns2:    !secret wifi_dns2
  # This is the fallback AP if the node
  # can't connect to the above wifi network.
  ap:
    ssid: "${node_name} Fallback AP"
    password: !secret wifi_ap_password

captive_portal:
web_server:
  port: 80
  auth:
    username: !secret web_username
    password: !secret web_password

logger:
  level: ${log_level}
api:
  password: !secret api_password
  reboot_timeout: 0s
ota:
  password: !secret ota_password
switch:
  - platform: restart
    name: "${node_name} Reboot"

mqtt:
  id: i_mqtt
  broker: !secret mqtt_broker
  client_id: ${node_name}
  username: ${node_name}
  password: ${mqtt_password}
  reboot_timeout: 20min

switch:
  - platform: gpio
    name: "${node_name} On-board LED"
    id: i_onboard_led
    internal: true
    pin:
      number: ${espin_led}
      inverted: yes

  - platform: gpio
    name: "${node_name} external LED"
    id: i_external_led
    internal: true
    pin: ${external_led}

i2c:
  sda: ${espin_sda}
  scl: ${espin_scl}
  scan: True
  id: i_i2c


sensor:
    # We used uptime as an easy way to examine relative
    # times. We don't really care about the absolute values.
  - platform: uptime
    id: device_uptime
    name: "${node_name} Uptime"
    update_interval: $UPTIME_UPDATE_PERIOD
    internal: true

  - platform: bme280
    id: i_bme280
    address: 0x76
    update_interval: 60s
    temperature:
      name: "${node_name} Temp"
      id: i_temp
      device_class: "temperature"
      oversampling: 16x
    pressure:
      name: "${node_name} AirPr"
      id: i_press
      device_class: "pressure"
      oversampling: 16x
    humidity:
      name: "${node_name} RHumidity"
      id: i_hum
      device_class: "humidity"
      oversampling: 16x

binary_sensor:
  - platform: gpio
    pin:
      number: D5
    name: "${node_name} PIR Wall"
    id: "pir_wall"
    device_class: motion
    internal: true
    on_press:
      then:
        - logger.log: "Oh, it is                                       ON D5 wall"
        - switch.turn_on: i_onboard_led
        - switch.turn_on: i_external_led
    on_release:
      then:
        - logger.log: "Oh, it is                                      OFF D5 wall"
        - switch.turn_off: i_onboard_led
        - switch.turn_off: i_external_led

  - platform: gpio
    pin:
      number: D6
    name: "${node_name} PIR Near"
    id: "pir_near"
    device_class: motion
    internal: true
    on_press:
      then:
        - logger.log: "Oh, it is                                       ON D6 near"
        - switch.turn_on: i_onboard_led
        - switch.turn_on: i_external_led
    on_release:
      then:
        - logger.log: "Oh, it is                                      OFF D6 near"
        - switch.turn_off: i_onboard_led
        - switch.turn_off: i_external_led

  - platform: gpio
    pin:
      number: D7
    name: "${node_name} PIR Far"
    id: "pir_far"
    device_class: motion
    internal: true
    on_press:
      then:
        - logger.log: "Oh, it is                                       ON D7 far"
        - switch.turn_on: i_onboard_led
        - switch.turn_on: i_external_led
    on_release:
      then:
        - logger.log: "Oh, it is                                      OFF D7 far"
        - switch.turn_off: i_onboard_led
        - switch.turn_off: i_external_led

  # When someone is actually moving around, the individual motion sensors
  # blink on and off quite often. To simplify things for the action of
  # controlling the lights, this template sensor does a couple of things:
  #  -- it consolidates all the other PIRs and is on when any of them are on
  #     (since they all control the same light switch)
  #  -- after motion is detected, it plays dead for a little while to
  #     avoid sending a lot of noisy readings over the network; as long
  #     as the play-dead period is small compared to the automation delay
  #     in turning off the lights, it's a benefit in reducing clutter and
  #     resource consumption on the HA server
  # Because of the above, the individual PIR sensors are marked internal.
  - platform: template
    name: "${node_name} Any PIR"
    id: any_pir
    device_class: motion
    internal: false

    lambda: |-
      static auto start_playing_dead = 0;
      static auto is_currently_on = false;
      static auto said_dead = false;
      auto now = id(device_uptime).state;
      if (is_currently_on  &&  (now - start_playing_dead) < $PLAY_DEAD_PERIOD) {
        if (!said_dead) {
          ESP_LOGD("main", "---------- playing dead ----------");
          // otherwise it prints this a zillion times per second
          said_dead = true;
        }
        return {};
      }
      if (id(pir_wall).state || id(pir_near).state || id(pir_far).state) {
        start_playing_dead = now;
        said_dead = false;
        is_currently_on = true;
      } else {
        is_currently_on = false;
      }
      return is_currently_on;

    on_press:
      then:
        - logger.log: "Oh, it is                                       ON Any PIR"
    on_release:
      then:
        - logger.log: "Oh, it is                                      OFF Any PIR"
