---
substitutions:
  device: socket-03
  name: Socket 03
  area: Bedroom
  comment: SPARE Socket
  max_power: "3450"
  # <2020
  current_res: "0.00290"
  voltage_div: "940"
  cf1_pin: GPIO14
  # >2020
  # current_res: "0.00117"
  # voltage_div: "755"
  # cf1_pin: GPIO04

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}
esp8266:
  board: esp01_1m

packages:
  common: !include common/common_esp8266.yaml

# BlitzWolf BW-SHP2 and BW-SHP6 (ESP8285 - BL0937 or HJL-01 Energy Monitoring)
# Pin 	  Name              Function
# GPIO15 	GPIO_REL1         Relay
# GPIO13 	GPIO_KEY1         Button
# GPIO00 	GPIO_LED1_INV     Red LED (inverted), Power Status
# GPIO02 	GPIO_LEDLNK_INV   Blue LED (inverted), Link Status
# GPIO05  GPIO_HJL_CF       BL0937 or HJL-01 CF power
# GPIO12  GPIO_NRG_SEL_INV  BL0937 or HJL-01 Sel output (0 = Voltage)
# GPIO14  GPIO_NRG_CF1      BL0937 or HJL-01 CF1 current / voltage
# GPIO01 	GPIO_USER         RXD, Optional sensor
# GPIO03 	GPIO_USER         TXD, Optional sensor

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: yes
    name: Button
    # filters:
    #   invert:
    on_press:
      # min_length: 50ms
      # max_length: 350ms
      then:
        - switch.toggle: relay
    on_double_click:
      # min_length: 50ms
      # max_length: 350ms
      then:
        - switch.turn_on: relay
        - light.turn_on: light_blue
        - delay: 10s
        - switch.turn_off: relay
        - light.turn_off: light_blue

output:
  - platform: gpio
    pin: GPIO00
    id: led_red
    inverted: True
  - platform: gpio
    pin: GPIO02
    id: led_blue
    inverted: True

light:
  - platform: binary
    name: Red LED
    id: light_red
    output: led_red
  - platform: binary
    name: Blue LED
    id: light_blue
    output: led_blue

sensor:
  - platform: hlw8012
    sel_pin:
      number: GPIO12
      inverted: true
    cf_pin: GPIO05
    cf1_pin: ${cf1_pin}
    current_resistor: ${current_res}
    voltage_divider: ${voltage_div}
    current:
      name: Current
      icon: mdi:flash-circle
      accuracy_decimals: 3
    voltage:
      name: Voltage
      icon: mdi:current-ac
    power:
      name: Power
      id: power
      icon: mdi:flash-outline
      on_value_range:
        - above: ${max_power}
          then:
            - light.turn_off: light_red
            - switch.turn_off: relay
            - homeassistant.service:
                service: persistent_notification.create
                data:
                  title: Message from ${name}
                data_template:
                  message: Switch turned off because power exceeded ${max_power} W
    energy:
      name: Energy
      unit_of_measurement: "Wh"
      icon: mdi:flash-outline
    update_interval: 30s
    change_mode_every: 4
  - platform: total_daily_energy
    name: Total Daily Power
    power_id: power
    accuracy_decimals: 5
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:circle-slice-3

switch:
  - platform: gpio
    name: Relay
    # icon: mdi:power-socket-eu
    pin: GPIO15
    id: relay
    on_turn_on:
      - light.turn_on: light_red
    on_turn_off:
      - light.turn_off: light_red

# status_led:
#   pin:
#     number: GPIO00
#     inverted: True

time:
  - !include common/time/homeassistant.yaml
#   - platform: homeassistant
#     timezone: Europe/Bratislava
#     on_time:
#       # Switch on on the hour
#       - seconds: 0
#         minutes: /15
#         hours: "*"
#         then:
#           - switch.turn_on: relay_1
#           - delay: 12min
#           - switch.turn_off: relay_1
