---
substitutions:
  devicename: "nodemcu-15"
  id_name: nodemcu_15
  Device_Location: Bottle Led Controller
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.25
  unique_id: "nodemcu-15"
  deep_sleep_interval: !secret deep_sleep_interval

logger:
  level: WARN

esp8266:
  board: nodemcuv2

wifi:
  ssid: !secret mogwai_wifi_name
  password: !secret mogwai_wifi_password
  <<: !include common/network.yaml

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "$devicename Fallback Hotspot"
    password: !secret mogwai_ap_key

# Enable Home Assistant API
api:
  encryption:
    key: !secret mogwai_api_key

ota:
  platform: esphome
  password: !secret mogwai_ota_key

captive_portal:

<<: !include common/time-sntp.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

esphome:
  name: $devicename
  friendly_name: "NodeMCU-15 - Ext DBL Relay"
  on_boot:
    then:
      - switch.turn_off: led_onboard1

switch:

  - platform: gpio
    id: led_onboard1
    name: "$devicename Onboard LED"
    pin:
      number: GPIO16
      mode: OUTPUT
      inverted: True
    restore_mode: ALWAYS_OFF

  - platform: template
    name: "01 - Twinkle Stars"
    optimistic: True
    id: twinkle
    turn_on_action:
      - while:
          condition:
            lambda: 'return true;'
          then:
            - switch.turn_on: led_onboard1
            - delay: 50ms
            - switch.turn_off: led_onboard1
            - delay: 50ms
    turn_off_action:
      - switch.turn_off: led_onboard1

  - platform: template
    name: "02 - Twinkle Relay"
    optimistic: True
    id: twinkle_relay
    turn_on_action:
      - while:
          condition:
            lambda: 'return true;'
          then:
            - switch.turn_on: relay1
            - delay: 500ms
            - switch.turn_off: relay2
            - delay: 500ms
            - switch.turn_off: relay1
            - delay: 500ms
            - switch.turn_on: relay2
            - delay: 500ms
    turn_off_action:
      - switch.turn_off: led_onboard1

  - platform: gpio
    id: relay1
    name: "$devicename Relay One"
    pin:
      number: D1
      mode: OUTPUT
      inverted: True
    restore_mode: ALWAYS_OFF

  - platform: gpio
    id: relay2
    name: "$devicename Relay Two"
    pin:
      number: D2
      mode: OUTPUT
      inverted: True
    restore_mode: ALWAYS_OFF

interval:

  - <<: !include common/version-interval.yaml

  - <<: !include common/whatami.yaml

<<: !include common/external-heapmon.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

text_sensor:

  - <<: !include common/version.yaml

mqtt:
  broker: 10.0.1.50
  discovery_object_id_generator: device_name
  discovery_retain: False
  discovery: False
  topic_prefix: /sensors/$devicename
  birth_message:
    topic: /sensors/$devicename/availability
    payload: online
  will_message:
    topic: /sensors/$devicename/availability
    payload: offline
  on_message:
    - topic: /sensors/$devicename/ota_mode
      payload: 'ON'
      then:
        - switch.turn_on: relay1
        - switch.turn_on: relay2
    - topic: /sensors/$devicename/ota_mode
      payload: 'OFF'
      then:
        - switch.turn_off: relay1
        - switch.turn_off: relay2
        - switch.turn_off: led_onboard1
    - topic: /sensors/$devicename/ota_mode
      payload: 'ONB'
      then:
        - switch.turn_on: led_onboard1
    - topic: /sensors/all-sensors/ota
      payload: 'ONB'
      then:
        - switch.turn_on: led_onboard1

    - topic: /sensors/all-sensors/ota
      payload: 'OFF'
      then:
        - switch.turn_off: led_onboard1