esphome:
  name: garden-presence
  friendly_name: Gartenhuette-Presence

esp8266:
  board: esp07

logger:
  level: DEBUG  # Log für die Ausgaben

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true  # versucht sofort zu verbinden ohne Scan

  ap:
    ssid: "Garten-Presence Fallback Hotspot"
    password: !secret ap_fallback_password

ota:
  - platform: esphome

api:
  encryption:
    key: !secret esphome_api_encryption_key

i2c:
  sda: GPIO2
  scl: GPIO14
  scan: True

# MCP23017
mcp23017:
  - id: mcp1
    address: 0x20


# Rows (Outputs)
output:
  - platform: gpio
    id: row1
    pin:
      mcp23xxx: mcp1
      number: 1
  - platform: gpio
    id: row2
    pin:
      mcp23xxx: mcp1
      number: 2
  - platform: gpio
    id: row3
    pin:
      mcp23xxx: mcp1
      number: 3
  - platform: gpio
    id: row4
    pin:
      mcp23xxx: mcp1
      number: 4

binary_sensor:
  - platform: gpio
    id: col1
    pin:
      mcp23xxx: mcp1
      number: 5
      mode: INPUT_PULLUP
    name: "PA5"
    internal: true
  - platform: gpio
    id: col2
    pin:
      mcp23xxx: mcp1
      number: 6
      mode: INPUT_PULLUP
    name: "PA6"
    internal: true
  - platform: gpio
    id: col3
    pin:
      mcp23xxx: mcp1
      number: 7
      mode: INPUT_PULLUP
    name: "PA7"
    internal: true

  - platform: gpio
    pin:
      mcp23xxx: mcp1
      number: 15
      mode: INPUT
    name: "PIR"
    filters:
      - delayed_on: 50ms
      - delayed_off: 50ms

text_sensor:
  - platform: template
    name: "Keypad LastKey"
    id: keypad_last_key

# Matrix Keypad
globals:
  - id: scan_active
    type: bool
    restore_value: no
    initial_value: 'true'
  - id: last_key
    type: char
    restore_value: no
    initial_value: ' '

script:
  - id: deactivate_scan
    mode: restart
    then:
      - delay: 60s
      - lambda: |-
          id(scan_active) = false;
          ESP_LOGD("keypad", "Scan deaktiviert!");


interval:
  - interval: 200ms
    then:
      # Row 1
      - output.turn_off: row1
      - delay: 5ms
      - lambda: |-
          bool col_state[3];
          col_state[0] = !id(col1).state;
          col_state[1] = !id(col2).state;
          col_state[2] = !id(col3).state;

          if (col_state[0] && id(last_key) != '1') { id(last_key) = '1'; id(keypad_last_key).publish_state("1"); ESP_LOGD("keypad", "Key pressed: 1"); }
          if (col_state[1] && id(last_key) != '2') { id(last_key) = '2'; id(keypad_last_key).publish_state("2"); ESP_LOGD("keypad", "Key pressed: 2"); }
          if (col_state[2] && id(last_key) != '3') { id(last_key) = '3'; id(keypad_last_key).publish_state("3"); ESP_LOGD("keypad", "Key pressed: 3"); }

          if (!col_state[0] && !col_state[1] && !col_state[2]) { id(last_key) = '0'; }
      - output.turn_on: row1

      # Row 2
      - output.turn_off: row2
      - delay: 5ms
      - lambda: |-
          bool col_state[3];
          col_state[0] = !id(col1).state;
          col_state[1] = !id(col2).state;
          col_state[2] = !id(col3).state;

          if (col_state[0] && id(last_key) != '4') { id(last_key) = '4'; id(keypad_last_key).publish_state("4"); ESP_LOGD("keypad", "Key pressed: 4"); }
          if (col_state[1] && id(last_key) != '5') { id(last_key) = '5'; id(keypad_last_key).publish_state("5"); ESP_LOGD("keypad", "Key pressed: 5"); }
          if (col_state[2] && id(last_key) != '6') { id(last_key) = '6'; id(keypad_last_key).publish_state("6"); ESP_LOGD("keypad", "Key pressed: 6"); }

          if (!col_state[0] && !col_state[1] && !col_state[2]) { id(last_key) = '0'; }
      - output.turn_on: row2

      # Row 3
      - output.turn_off: row3
      - delay: 5ms
      - lambda: |-
          bool col_state[3];
          col_state[0] = !id(col1).state;
          col_state[1] = !id(col2).state;
          col_state[2] = !id(col3).state;

          if (col_state[0] && id(last_key) != '7') { id(last_key) = '7'; id(keypad_last_key).publish_state("7"); ESP_LOGD("keypad", "Key pressed: 7"); }
          if (col_state[1] && id(last_key) != '8') { id(last_key) = '8'; id(keypad_last_key).publish_state("8"); ESP_LOGD("keypad", "Key pressed: 8"); }
          if (col_state[2] && id(last_key) != '9') { id(last_key) = '9'; id(keypad_last_key).publish_state("9"); ESP_LOGD("keypad", "Key pressed: 9"); }

          if (!col_state[0] && !col_state[1] && !col_state[2]) { id(last_key) = '0'; }
      - output.turn_on: row3

      # Row 4
      - output.turn_off: row4
      - delay: 5ms
      - lambda: |-
          bool col_state[3];
          col_state[0] = !id(col1).state;
          col_state[1] = !id(col2).state;
          col_state[2] = !id(col3).state;

          if (col_state[0] && id(last_key) != '*') { id(last_key) = '*'; id(keypad_last_key).publish_state("*"); ESP_LOGD("keypad", "Key pressed: *"); }
          if (col_state[1] && id(last_key) != '0') { id(last_key) = '0'; id(keypad_last_key).publish_state("0"); ESP_LOGD("keypad", "Key pressed: 0"); }
          if (col_state[2] && id(last_key) != '#') { id(last_key) = '#'; id(keypad_last_key).publish_state("#"); ESP_LOGD("keypad", "Key pressed: #"); }

          if (!col_state[0] && !col_state[1] && !col_state[2]) { id(last_key) = '0'; }
      - output.turn_on: row4

