substitutions:
  device_name: "athom-smart-plug"
  friendly_name: "prise_ecran"
  project_name: "prise.ecran"
  project_version: "1.0"
  adresse_ip: 10.0.2.254

packages:
  ## include wifi networks, manual_ip, ap
  wifi: !include common/wifi.yaml
  #include logger with syslog, api, ota, webserver, captive portal
  device_base: !include common/device_base.yaml

esphome:
  name: "${device_name}"
  name_add_mac_suffix: true
  project:
    name: "${project_name}"
    version: "${project_version}"
esp8266:
  board: esp8285



binary_sensor:
#include status
  - <<: !include common/generic_binarysensor.yaml

  - platform: gpio
    pin:
      number: 3
      mode: INPUT_PULLUP
      inverted: true
    name: "${friendly_name} Power Button"
    disabled_by_default: true
    on_multi_click:
      - timing:
          - ON for at most 1s
          - OFF for at least 0.2s
        then:
          - switch.toggle: relay
      - timing:
          - ON for at least 4s
        then:
          - switch.turn_on: restart_switch

sensor:
#include uptime et wifisignal
  - <<: !include common/generic_sensor.yaml

  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: True
    cf_pin: GPIO4
    cf1_pin: GPIO5
    voltage_divider: 780
    current:
      name: "${friendly_name} Current"
      filters:
          - calibrate_linear:
            - 0.0000 -> 0.0110 # Relay off no load
            - 0.0097 -> 0.0260 # Relay on no load
            - 0.9270 -> 0.7570
            - 2.0133 -> 1.6330
            - 2.9307 -> 2.3750
            - 5.4848 -> 4.4210
            - 8.4308 -> 6.8330
            - 9.9171 -> 7.9830
          # Normalize for plug load
          - lambda: if (x < 0.0260) return 0; else return (x - 0.0260);
    voltage:
      name: "${friendly_name} Voltage"

    power:
      name: "${friendly_name} Power"
      id: socket_my_power
      unit_of_measurement: W
      filters:
          - calibrate_linear:
            - 0.0000 -> 0.5900 # Relay off no load
            - 0.0000 -> 1.5600 # Relay on no load
            - 198.5129 -> 87.8300
            - 434.2469 -> 189.5000
            - 628.6241 -> 273.9000
            - 1067.0067 -> 460.1000
            - 1619.8098 -> 699.2000
            - 2043.0282 -> 885.0000
          # Normalize for plug load
          - lambda: if (x < 1.5600) return 0; else return (x - 1.5600);
    change_mode_every: 1
    update_interval: 5s

  - platform: total_daily_energy
    name: "${friendly_name} Total Daily Energy"
    power_id: socket_my_power
    unit_of_measurement: kWh
    accuracy_decimals: 3
    filters:
      - multiply: 0.001
 

switch:
#include restart
  - <<: !include common/generic_switch.yaml

  - platform: gpio
    name: "${friendly_name}"
    pin: GPIO14
    id: relay
    restore_mode: RESTORE_DEFAULT_ON
    on_turn_on:
      - light.turn_on: blue_led

    on_turn_off:
      - light.turn_off: blue_led

light:
  - platform: status_led
    name: "${friendly_name} Status LED"
    id: blue_led
    disabled_by_default: true
    pin:
      inverted: true
      number: GPIO13

time:
  - platform: sntp

text_sensor:
#include wifi info : ipaddress ssid
 - <<: !include common/generic_textsensor.yaml
