# Example ESPHome code to emulate stock firmware functionality while maintaining 100% 
# local control through Home Assistant.

substitutions:
  device_name: '<NODE_NAME>'     # used internally.. e.g. teckin_sp20
  friendly_name: '<Node Name>'   # displayed in HA.. e.g. Teckin SP20
  wifi_ssid: '<WIFI_SSID>'
  wifi_password: '<WIFI_PASSWORD>'

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: ${wifi_ssid}
  password: ${wifi_password}

# Enable Logs from the device
logger:

# Enable Home Assistant API
api:

# Enable Over-The-Air updates
ota:

sensor:
  # Power measuring sensor
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO5
    cf1_pin: GPIO14
    current:
      name: ${friendly_name} Current
    power:
      name: ${friendly_name} Power
      id: power
      filters:
        # Some calibration
        - multiply: 0.4545
    voltage:
      name: ${friendly_name} Voltage
    current_resistor: 0.001  # default 0.001
    voltage_divider: 910     # default 2351
    change_mode_every: 3     # default 8
    update_interval: 3s      # default 60s

  # Sum power readings over the day
  - platform: total_daily_energy
    name: ${friendly_name} Total Daily Energy
    power_id: power
    filters:
      - multiply: 0.001   # convert Wh to kWh
    unit_of_measurement: kWh

# Enable getting local time for total daily energy calculation
time:
  - platform: homeassistant
    id: homeassistant_time

status_led:
  pin:
    number: GPIO0   # Red LED
    inverted: True

binary_sensor:
  - platform: gpio
    pin: GPIO13
    id: button
    name: ${friendly_name} Button
    on_press:
      - switch.toggle: relay
    internal: True

switch:
  - platform: gpio
    id: blue_led
    pin:
      number: GPIO2
      inverted: True
    
  - platform: gpio
    pin: GPIO4
    id: relay
    name: ${friendly_name}
    # Tie Blue LED to relay
    on_turn_on:
    - switch.turn_on: blue_led
    on_turn_off:
    - switch.turn_off: blue_led
