esp8266:
  board: d1_mini_pro
  framework:
    version: latest
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino
esp8266:
  board: nodemcu
esp32:
  board: esp32-c6-devkitc-1
  variant: esp32c6
  framework:
    type: esp-idf
esphome:
  name: "house-garage"
  friendly_name: "house-garage"

esp8266:
  board: d1_mini_pro
  framework:
    version: latest

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "####################################="

ota:
  - platform: esphome
    password: "#################################"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.2.74
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "D1Minipro Fallback Hotspot"
    password: "####################"

captive_portal:

web_server:

sensor:
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

button:
  - platform: restart
    name: "ESP Reboot"
  - platform: safe_mode
    name: "ESP(Safe Mode)"
  - platform: shutdown
    name: "ESP Shutdown"

#relay

switch:
  - platform: gpio
    name: "Garage Door"
    pin: GPIO14
    id: relay
    icon: "mdi:garage"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay
#Deep sleep and OTA update button. (for sensor data that doesn't go Nan, it needs to be added to HA with deepsleep enabled.  You might have to delete it from HA and add it again)  how to stop deep sleep video!

#Deep sleep

esphome:
  name: fjcruiseresp
  friendly_name: FjCruiserESP
  on_boot:
    priority: -100.0
    then:
      - delay: 2s
      - script.execute: test_ota


#################################################
#Deep Sleep
deep_sleep:
  id: deep_sleep_handler
  run_duration: 60s
  sleep_duration: 30min

#################################################   
# Script to test if the otamode switch is on or off
script:
  - id: test_ota
    mode: queued
    then:
      - logger.log: "Checking OTA Mode"
      - if:
          condition:
            binary_sensor.is_on: otamode
          then:
            - logger.log: 'OTA Mode ON'
            - deep_sleep.prevent: deep_sleep_handler
          else:
            - logger.log: 'OTA Mode OFF'
      - delay: 30s
      - script.execute: test_ota

binary_sensor:
  - platform: status
    name: "FJ Status"
  - platform: homeassistant
    id: otamode
    entity_id: input_boolean.FJ_ESP_OTA

sensor:
# voltage checker
- platform: adc
  pin: GPIO3 
  name: "Car Battery"
  update_interval: 60s
  attenuation: 12db
  samples: 10
  filters:
  - multiply: 5.065 # The voltage divider requires us to multiply by 3
# buttons
  #number: GPIO02
  #number: GPIO03
  #number: GPIO04
  #number: GPIO08
  #number: GPIO09
  #number: GPIO10
  #number: GPIO20
  #number: GPIO21
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO03
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Enter Button"


  - platform: gpio
    pin:
      number: GPIO04
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Up Button"


  - platform: gpio
    pin: 
      number: GPIO05
      mode:
        input: true
        pullup: true
      inverted: true  
    name: "Down Button"


  - platform: gpio
    pin:
      number: GPIO21
      mode:
        input: true
        pullup: true
      inverted: true 
    name: "Left Button"


  - platform: gpio
    pin: 
      number: GPIO10
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Right Button"

#Smoke and BME680 air sensor

web_server:

# wifi
sensor:
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

#Air quality sensor
  - platform: bme680
    temperature:
      name: "BME680 Temperature"
      oversampling: 16x
    pressure:
      name: "BME680 Pressure"
    humidity:
      id: "humidity"
      name: "BME680 Humidity"
    gas_resistance:
      id: "gas_resistance"
      name: "BME680 Gas Resistance"
    address: 0x77
    update_interval: 60s
  - platform: template
    name: "BME680 Indoor Air Quality"
    id: iaq
    icon: "mdi:gauge"
    # calculation: comp_gas = log(R_gas[ohm]) + 0.04 log(Ohm)/%rh * hum[%rh]
    lambda: |-
      return log(id(gas_resistance).state) + 0.04 *  id(humidity).state;
    state_class: "measurement"

#MQ-2 Gas and smoke Analog Sensor
  - platform: adc
    pin: GPIO2
    name: "Smoke sensor"
    id: smk
    update_interval: 20s
    filters:
    - multiply: 100
    unit_of_measurement: "%"
    icon: "mdi:percent"

#Air Quality good or bad converter for Air quality sensor
text_sensor:
  - platform: wifi_info
    ip_address:
      name: ESP IP Address 
  - platform: template
    name: "BME680 IAQ Classification"
    icon: "mdi:checkbox-marked-circle-outline"
    lambda: |-
      if (int(id(iaq).state) <= 50) {
        return {"Excellent"};
      }
      else if (int(id(iaq).state) <= 100) {
        return {"Good"};
      }
      else if (int(id(iaq).state) <= 150) {
        return {"Lightly polluted"};
      }
      else if (int(id(iaq).state) <= 200) {
        return {"Moderately polluted"};
      }
      else if (int(id(iaq).state) <= 250) {
        return {"Heavily polluted"};
      }
      else if (int(id(iaq).state) <= 350) {
        return {"Severely polluted"};
      }
      else if (int(id(iaq).state) <= 500) {
        return {"Extremely polluted"};
      }
      else {
        return {"unknown"};
      }

#reset buttons
button:
  - platform: restart
    name: "ESP Reboot"
  - platform: safe_mode
    name: "ESP(Safe Mode)"
  - platform: shutdown
    name: "ESP Shutdown"
  - platform: factory_reset
    name: Restart with Factory Default Settings
#LCD stuff



font:
  - file: gfonts://Roboto
    id: my_font
    size: 20







#hello world text
i2c:
  sda: GPIO6
  scl: GPIO7
  frequency: 300kHz
display:
  - platform: ssd1306_i2c
    model: SSD1306 128x32
    address: 0x3C
    update_interval: 10s
    lambda: |-
      // Print the string "Hello World!" at [0,10]
      it.print(0, 10, id(my_font), "Hello World!");