esphome:
  name: blau
  friendly_name: Blau

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# ---------------------------------------------------------------------------
# NETZWERK
# ---------------------------------------------------------------------------
wifi:
  power_save_mode: none
  fast_connect: true
  networks:
    - ssid: !secret wifi10_ssid
      password: !secret wifi10_password
      priority: 100
    - ssid: !secret wifi2_ssid
      password: !secret wifi2_password
      priority: 50
    - ssid: !secret wifi3_ssid
      password: !secret wifi2_password
      priority: 50
    - ssid: !secret wifi4_ssid
      password: !secret wifi4_password
      priority: 10
  ap:
    ssid: "NeoPixel-Clock Fallback"
    password: !secret wifi1_password

logger:
  level: DEBUG

captive_portal:

api:
  encryption:
    key: !secret blaukey

ota:
  - platform: esphome
    password: !secret blaupassword

# ---------------------------------------------------------------------------
# I2C  –  PCF8574
# ---------------------------------------------------------------------------
i2c:
  sda: GPIO21
  scl: GPIO22

pcf8574:
  - id: pcf8574_hub
    address: 0x20
    pcf8575: false

# ---------------------------------------------------------------------------
# ESP-NOW
# ---------------------------------------------------------------------------
espnow:
  peers:
    - !secret gelbmac

# ---------------------------------------------------------------------------
# GLOBALS
# ---------------------------------------------------------------------------
globals:
  - id: brightness_pct
    type: int
    initial_value: '100'
  - id: weiss_press_time
    type: uint32_t
    initial_value: '0'

# ---------------------------------------------------------------------------
# ENCODER OBEN  –  Helligkeit
# ---------------------------------------------------------------------------
sensor:
  - platform: rotary_encoder
    id: enc_oben
    name: "Encoder Oben"
    pin_a:
      number: GPIO26
      mode: INPUT_PULLUP
    pin_b:
      number: GPIO25
      mode: INPUT_PULLUP
    resolution: 1
    on_clockwise:
      then:
        - lambda: |-
            id(brightness_pct) = std::min(100, id(brightness_pct) + 5);
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              static std::string s;
              s = "h:" + std::to_string(id(brightness_pct));
              return std::vector<uint8_t>(s.begin(), s.end());
    on_anticlockwise:
      then:
        - lambda: |-
            id(brightness_pct) = std::max(0, id(brightness_pct) - 5);
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              static std::string s;
              s = "h:" + std::to_string(id(brightness_pct));
              return std::vector<uint8_t>(s.begin(), s.end());

  # ---------------------------------------------------------------------------
  # ENCODER UNTEN  –  Menü scrollen
  # ---------------------------------------------------------------------------
  - platform: rotary_encoder
    id: enc_unten
    name: "Encoder Unten"
    pin_a:
      number: GPIO33
      mode: INPUT_PULLUP
    pin_b:
      number: GPIO32
      mode: INPUT_PULLUP
    resolution: 1
    on_clockwise:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "v:+1";
              return std::vector<uint8_t>(s.begin(), s.end());
    on_anticlockwise:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "v:-1";
              return std::vector<uint8_t>(s.begin(), s.end());

# ---------------------------------------------------------------------------
# SCRIPTS  –  ESP-NOW Hilfsskripte für Taster Weiß
# ---------------------------------------------------------------------------
script:
  - id: espnow_send_tab
    then:
      - espnow.send:
          address: !secret gelbmac
          data: !lambda |-
            std::string s = "b:1";
            return std::vector<uint8_t>(s.begin(), s.end());

  - id: espnow_send_flip
    then:
      - espnow.send:
          address: !secret gelbmac
          data: !lambda |-
            std::string s = "f:flip";
            return std::vector<uint8_t>(s.begin(), s.end());

# ---------------------------------------------------------------------------
# TASTER
# ---------------------------------------------------------------------------
binary_sensor:

  # ── Encoder Oben Taster  –  Reserve ───────────────────────────────────
  - platform: gpio
    name: "Encoder Oben Taster"
    pin:
      pcf8574: pcf8574_hub
      number: 7
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - lambda: 'ESP_LOGI("blau", "Encoder Oben Taster: Reserve");'

  # ── Encoder Unten Taster  –  Reserve ──────────────────────────────────
  - platform: gpio
    name: "Encoder Unten Taster"
    pin:
      pcf8574: pcf8574_hub
      number: 6
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - lambda: 'ESP_LOGI("blau", "Encoder Unten Taster: Reserve");'

  # ── Taster Rot  –  Basisfarbe Rot ─────────────────────────────────────
  - platform: gpio
    name: "Taster Rot"
    pin:
      pcf8574: pcf8574_hub
      number: 0
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "k:0";
              return std::vector<uint8_t>(s.begin(), s.end());

  # ── Taster Grün  –  Basisfarbe Grün ───────────────────────────────────
  - platform: gpio
    name: "Taster Gruen"
    pin:
      pcf8574: pcf8574_hub
      number: 1
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "k:1";
              return std::vector<uint8_t>(s.begin(), s.end());

  # ── Taster Blau  –  Basisfarbe Blau ───────────────────────────────────
  - platform: gpio
    name: "Taster Blau"
    pin:
      pcf8574: pcf8574_hub
      number: 2
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "k:2";
              return std::vector<uint8_t>(s.begin(), s.end());

  # ── Taster Gelb  –  Basisfarbe Gelb/Orange ────────────────────────────
  - platform: gpio
    name: "Taster Gelb"
    pin:
      pcf8574: pcf8574_hub
      number: 3
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "k:3";
              return std::vector<uint8_t>(s.begin(), s.end());

  # ── Taster Weiß  –  kurz = Tab wechseln, lang (2s) = Display drehen ──
  - platform: gpio
    name: "Taster Weiss"
    pin:
      pcf8574: pcf8574_hub
      number: 4
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - lambda: 'id(weiss_press_time) = millis();'
    
    on_release:
      then:
        - lambda: |-
            uint32_t held = millis() - id(weiss_press_time);
            ESP_LOGI("blau", "Weiss losgelassen nach %d ms", (int)held);
            if (held >= 2000) {
              ESP_LOGI("blau", "→ sende f:flip");
              id(espnow_send_flip).execute();
            } else {
              ESP_LOGI("blau", "→ sende b:1");
              id(espnow_send_tab).execute();
            }
    

  # ── Taster Schwarz  –  Alles aus ──────────────────────────────────────
  - platform: gpio
    name: "Taster Schwarz"
    pin:
      pcf8574: pcf8574_hub
      number: 5
      mode: INPUT
      inverted: true
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - espnow.send:
            address: !secret gelbmac
            data: !lambda |-
              std::string s = "x:0";
              return std::vector<uint8_t>(s.begin(), s.end());
