#############################################
#############################################

#############################################
#############################################

#############################################
# Variable Substitutions
# Give this a useful name & description here
# and change values accordingly
#############################################
substitutions:
  devicename: "esp-winefridge"
  friendly_name: "Wine Fridge Controller"
  description_comment: "Peltier based wine fridge thermostat controller"

  #if NOT using a secrets file, just replace these with the passwords etc in speech marks
  api_key: !secret esp-winefridge_api_key #unfortunately you can't use substitutions in secrets names
  ota_pass: !secret esp-winefridge_ota_pass #unfortunately you can't use substitutions in secrets names
  wifi_ssid: !secret wifi_ssid
  wifi_pass: !secret wifi_password
  mqtt_server: !secret mqtt_server
  mqtt_username: !secret mqtt_username
  mqtt_password: !secret mqtt_password

  mqtt_topic: "esphome" #main topic for the mqtt server, call it what you like
  update_time: 30s #update time for for various temp sensors etc

#############################################
# ESPHome
# https://esphome.io/components/esphome.html
#############################################
esphome:
  name: ${devicename}
  comment: ${description_comment} #appears on the esphome page in HA
  #on_boot:    #Initial Setting stuff
    #priority: -200
    #then:

#############################################
# ESP Platform and Framework
# https://esphome.io/components/esp32.html OR
# https://esphome.io/components/esp8266.html
#############################################
#esp32:
#  board: 
#  framework:
#    type: arduino
#    #type: esp-idf  #Suggested Use ESP-IDF Framework for ESP32, or unplugging the UART Cable Might Cause ESP32 Hang. 
#    version: recommended #recommended, latest or dev

esp8266:
  board: d1_mini



#############################################    
# ESPHome Logging Enable
# https://esphome.io/components/logger.html
############################################# 
logger:
  level: INFO  #INFO Level suggested, or DEBUG for testing
  #baud_rate: 0 #set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM)
  #esp8266_store_log_strings_in_flash: false
  #tx_buffer_size: 64

#############################################    
# Enable the Home Assistant API
# https://esphome.io/components/api.html
#############################################  
api:
  encryption:
    key: ${api_key}

#############################################    
# Enable Over the Air Update Capability
# https://esphome.io/components/ota.html?highlight=ota
#############################################  
ota:
  - platform: esphome
    password: ${ota_pass}

#############################################    
# Safe Mode
# Safe mode will detect boot loops
# https://esphome.io/components/safe_mode
#############################################  
safe_mode:

#############################################    
# Wifi Settings 
# https://esphome.io/components/wifi.html
#
# Power Save mode (can reduce wifi reliability)
# NONE (least power saving, Default for ESP8266)
# LIGHT (Default for ESP32)
# HIGH (most power saving)
#############################################  
wifi:
  ssid: ${wifi_ssid}
  password: ${wifi_pass}
  #power_save_mode: LIGHT #https://esphome.io/components/wifi.html#wifi-power-save-mode
  #manual_ip: #optional static IP address
    #static_ip: 192.168.x.x
    #gateway: 192.168.X.x
    #subnet: 255.255.255.0
  ap:   #Details for fallback hotspot in case wifi connection fails https://esphome.io/components/wifi.html#access-point-mode
    ssid: $devicename fallback AP
    password: !secret fallback_ap_password
    ap_timeout: 30min #default is 1min

#############################################    
# Web Portal for display and monitoring
# Turning this off if you don't really need it
# is probably a good idea to save resources.
# Also, don't use it with other big ESP32 components
# such as Bluetooth proxy (it will run out of flash
# and not compile, or it will crash occasionally)
# https://esphome.io/components/web_server.html
#############################################   
web_server:
  port: 80
#  version: 1 #V1 occasionally works better, V2 The nicer page
#   username: !secret web_server_username #probably a good idea to secure it
#    password: !secret web_server_password

#############################################
# MQTT Monitoring
# https://esphome.io/components/mqtt.html?highlight=mqtt
# MUST also have api enabled if you enable MQTT
#############################################
mqtt:
  broker: ${mqtt_server}
  topic_prefix: ${mqtt_topic}/${devicename}
  username: ${mqtt_username}
  password: ${mqtt_password}


#############################################
# General espHome status LED
# Not needed, but can be useful to see issues
# https://esphome.io/components/status_led.html
#############################################
status_led:
  pin:
    number: GPIO2 #Wemos ESP32 and ESP8266 Onboard LEDs use GPIO2
    inverted: false
    #ignore_strapping_warning: True #https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins

#############################################
#############################################
# General Sensors 
# https://esphome.io/components/sensor/index.html
#############################################
#############################################
sensor:

  ################################
  # UPTIME
  # Device uptime info	
  # https://esphome.io/components/sensor/uptime.html
  ################################
  - platform: uptime
    name: ${friendly_name} Uptime

  ################################
  # WIFI SIGNAL
  # Quality of Wifi in dBm
  # https://esphome.io/components/sensor/wifi_signal.html
  ################################
  - platform: wifi_signal
    name: ${friendly_name} WiFi Signal
    update_interval: ${update_time}
    #retain: true #retain useful if sleeping
  
  - platform: resistance
    sensor: temp_analogue
    configuration: DOWNSTREAM
    resistor: 4.7kOhm
    name: Resistance Sensor

  - platform: adc
    id: temp_analogue
    #update_interval: 10s
    update_interval: never #don't update until the correect GPIO turns on.  This allows two devices on one AI.
    name: "Temp Analogue"
    pin: A0
    filters:
      - multiply: 3.3

  - platform: template
    name: "Analogue Sensor 1"
    update_interval: never
    id: template_analoguesensor_1
    lambda: |-
      return id(temp_analogue);

  - platform: template
    name: "Analogue Sensor 2"
    update_interval: never
    id: template_analoguesensor_2
    lambda: |-
      return id(temp_analogue);
    # lambda: |- return float(id(ch1s));

switch:
  - platform: gpio
    pin: D0
    id: a1_sensor_vcc

  - platform: gpio
    pin: D1
    id: a2_sensor_vcc

interval:
  - interval: 60s
    then:
      - switch.turn_on: a1_sensor_vcc
      - component.update: temp_analogue
      - component.update: template_analoguesensor_1
      - switch.turn_off: a1_sensor_vcc
      - switch.turn_on: a2_sensor_vcc
      - component.update: temp_analogue
      - component.update: template_analoguesensor_2
      - switch.turn_off: a2_sensor_vcc



