---
substitutions:
  devicename: "nodemcu-04"
  id_name: nodemcu_04
  Device_Location: Study UVA
  update_interval: !secret d1-update_interval
  ipaddress: 10.0.0.14
  unique_id: "nodemcu-04"
  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/nodemcu-led-onboard-both-mqtt.yaml

<<: !include common/time.yaml

<<: !include common/website.yaml

<<: !include common/wifi1.yaml

<<: !include common/uptime_formatted.yaml

esphome:
  name: $devicename
  friendly_name: "Study UVA Settings"
  includes:
    - veml6075_custom_sensor.h
  libraries:
    - "Wire"
    - SparkFun_VEML6075=https://github.com/sparkfun/SparkFun_VEML6075_Arduino_Library.git
  on_boot:
    then:
      - switch.turn_off: led_onboard1
      - switch.turn_off: led_onboard2

switch:

  - <<: !include common/nodemcu-led-onboard1.yaml

  - <<: !include common/nodemcu-led-onboard2.yaml

  - <<: !include common/nodemcu-led-onboard1-2-shuffle.yaml

  - <<: !include common/restart.yaml

interval:

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

  - <<: !include common/whatami.yaml

sensor:

  - <<: !include common/wifi2.yaml

  - <<: !include common/uptime.yaml

  - <<: !include common/free_space.yaml

  # UV 6075 Sensor
  - platform: custom
    lambda: |-
      auto veml6075 = new VEML6075CustomSensor(50000);
      App.register_component(veml6075);
      return {veml6075->uva_sensor, veml6075->uvb_sensor};
    sensors:
      - name: "uva"
        id: zelva_uva
        unit_of_measurement: "µW/cm²"
        accuracy_decimals: 0
      - name: "uvb"
        id: zelva_uvb
        unit_of_measurement: "µW/cm²"
        accuracy_decimals: 0

  - platform: template
    name: "Random Numbers"
    id: random_sensor
    lambda: |-
      return rand() % 100;
    update_interval: !secret d1-update_interval

text_sensor:

  - <<: !include common/version.yaml

i2c:
  sda: D1
  scl: D2
  scan: True
  id: bus_a

binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    name: Prevent Deep Sleep
    entity_id: input_boolean.prevent_deep_sleep

deep_sleep:
  id: deep_sleep_1
  # run_duration: $deep_sleep_interval
  sleep_duration: 10min


script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: $deep_sleep_interval
      - if:
          condition:
            binary_sensor.is_on: prevent_deep_sleep
          then:
            - logger.log: 'Skipping sleep, per prevent_deep_sleep'
          else:
            - deep_sleep.enter: deep_sleep_1
      - script.execute: consider_deep_sleep


external_components:
  - source:
      type: git
      url: https://github.com/ssieb/custom_components
    components: [heapmon, crash]
  - source:
      type: git
      url: https://github.com/robertklep/esphome-custom-component
    components: [custom, custom_component]

sun:
  latitude: !secret latitude
  longitude: !secret longitude