substitutions:
  devicename: "retro-switch-4p"
  friendlyname: "Retro Switch 4P"

  #### Home Assistant switch entities.
  ha_switch_1: switch.s1
  ha_switch_2: switch.s2
  ha_switch_3: switch.s2
  ha_switch_4: switch.s3

esp32:
  board: esp32dev
  framework:
    type: arduino

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

ota:
  - platform: esphome
    password: "YOUR_PASSWORD"

wifi:
  ap:
    ssid: "${devicename} Hotspot"

captive_portal:

script:
  - id: sync_rocker_to_ha
    parameters:
      rocker_state: bool
      ha_state: bool
      ha_entity: string
    then:
      # Only send command if state is different
      - if:
          condition:
            lambda: 'return rocker_state != ha_state;'
          then:
            - if:
                condition:
                  lambda: 'return rocker_state;'
                then:
                  - homeassistant.service:
                      service: switch.turn_on
                      data:
                        entity_id: !lambda 'return ha_entity;'
                else:
                  - homeassistant.service:
                      service: switch.turn_off
                      data:
                        entity_id: !lambda 'return ha_entity;'


################################
# GPIO outputs for LEDs
################################

output:
  - platform: gpio
    pin: GPIO5
    id: led1_out

  - platform: gpio
    pin: GPIO18
    id: led2_out

  - platform: gpio
    pin: GPIO19
    id: led3_out

  - platform: gpio
    pin: GPIO21
    id: led4_out

################################
# Read HA switch state → update LEDs
################################

switch:
  # ---- STATUS SOURCES FROM HOME ASSISTANT ----
  - platform: homeassistant
    entity_id: ${ha_switch_1}
    id: ha_sw1
    internal: true
    on_turn_on:
      - output.turn_on: led1_out
    on_turn_off:
      - output.turn_off: led1_out

  - platform: homeassistant
    entity_id: ${ha_switch_2}
    id: ha_sw2
    internal: true
    on_turn_on:
      - output.turn_on: led2_out
    on_turn_off:
      - output.turn_off: led2_out

  - platform: homeassistant
    entity_id: ${ha_switch_3}
    id: ha_sw3
    internal: true
    on_turn_on:
      - output.turn_on: led3_out
    on_turn_off:
      - output.turn_off: led3_out

  - platform: homeassistant
    entity_id: ${ha_switch_4}
    id: ha_sw4
    internal: true
    on_turn_on:
      - output.turn_on: led4_out
    on_turn_off:
      - output.turn_off: led4_out
        
################################
# Latching Rocker Switches
################################

binary_sensor:
  - platform: gpio
    id: rocker_1
    pin:
      number: GPIO27
      mode: INPUT_PULLUP
      inverted: true
    name: "Rocker 1"
    filters:
      - delayed_on: 20ms
      - delayed_off: 20ms
    on_state:
      then:
        - script.execute:
            id: sync_rocker_to_ha
            rocker_state: !lambda 'return id(rocker_1).state;'
            ha_state: !lambda 'return id(ha_sw1).state;'
            ha_entity: ${ha_switch_1}

  - platform: gpio
    id: rocker_2
    pin:
      number: GPIO26
      mode: INPUT_PULLUP
      inverted: true
    name: "Rocker 2"
    filters:
      - delayed_on: 20ms
      - delayed_off: 20ms
    on_state:
      then:
        - script.execute:
            id: sync_rocker_to_ha
            rocker_state: !lambda 'return id(rocker_2).state;'
            ha_state: !lambda 'return id(ha_sw2).state;'
            ha_entity: ${ha_switch_2}
  
  - platform: gpio
    id: rocker_3
    pin:
      number: GPIO25
      mode: INPUT_PULLUP
      inverted: true
    name: "Rocker 3"
    filters:
      - delayed_on: 20ms
      - delayed_off: 20ms
    on_state:
      then:
        - script.execute:
            id: sync_rocker_to_ha
            rocker_state: !lambda 'return id(rocker_3).state;'
            ha_state: !lambda 'return id(ha_sw3).state;'
            ha_entity: ${ha_switch_3}
  
  - platform: gpio
    id: rocker_4
    pin:
      number: GPIO33
      mode: INPUT_PULLUP
      inverted: true
    name: "Rocker 4"
    filters:
      - delayed_on: 20ms
      - delayed_off: 20ms
    on_state:
      then:
        - script.execute:
            id: sync_rocker_to_ha
            rocker_state: !lambda 'return id(rocker_4).state;'
            ha_state: !lambda 'return id(ha_sw4).state;'
            ha_entity: ${ha_switch_4}