esphome:
  name: pwm-lufter
  on_boot:
    priority: -100
    then:
      - script.execute: startup_check

esp8266:
  board: esp12e

# Logging, OTA, API, WiFi, Webserver
logger:

api:
  encryption:
    key: "Sag ich nicht"

ota:
  platform: esphome
  password: !secret ota

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.202.63
    gateway: !secret gateway
    subnet: !secret subnet

  ap:
    ssid: "PWM Lüfter"
    password: "PWM Lüfter"

web_server:
  port: 80
  auth:
    username: !secret web_server_un
    password: !secret web_server_pw
  include_internal: true
  local: true

captive_portal:

# -----------------------------
# RPM Sensoren (Fan1 bis Fan5)
# -----------------------------
sensor:
  - platform: pulse_counter
    pin:
      number: GPIO14
      mode:
        input: true
        pullup: true
    name: Fan1 RPM
    id: fan_pulse_1
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: pulse_counter
    pin:
      number: GPIO12
      mode:
        input: true
        pullup: true
    name: Fan2 RPM
    id: fan_pulse_2
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: pulse_counter
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: true
    name: Fan3 RPM
    id: fan_pulse_3
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: pulse_counter
    pin:
      number: GPIO5
      mode:
        input: true
        pullup: true
    name: Fan4 RPM
    id: fan_pulse_4
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

  - platform: pulse_counter
    pin:
      number: GPIO4
      mode:
        input: true
        pullup: true
    name: Fan5 RPM
    id: fan_pulse_5
    unit_of_measurement: 'RPM'
    filters:
      - multiply: 0.5
    count_mode:
      rising_edge: INCREMENT
      falling_edge: DISABLE
    update_interval: 3s

# -----------------------------
# PWM-Ausgänge
# -----------------------------
output:
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 25000 Hz
    id: fan_pwm_output  # Direkte PWM an 4-Pin-Lüfter

  - platform: esp8266_pwm
    pin: GPIO3
    frequency: 25000 Hz
    id: mosfet_pwm_output  # PWM zur Spannungssteuerung des MOSFETs

# -----------------------------
# Fan-Steuerung über UI (1 Regler)
# -----------------------------
number:
  - platform: template
    name: "Lüftergeschwindigkeit"
    id: fan_speed_control
    min_value: 0
    max_value: 100
    step: 5
    unit_of_measurement: "%"
    optimistic: true
    on_value:
      then:
        - lambda: |-
            id(fan_speed_level) = x / 100.0;
            if (id(use_pwm_mode)) {
              id(fan_pwm_output).set_level(id(fan_speed_level));
              id(mosfet_pwm_output).set_level(1.0);
            } else {
              id(mosfet_pwm_output).set_level(id(fan_speed_level));
              id(fan_pwm_output).set_level(0.0);
            }

# -----------------------------
# Globale Variablen
# -----------------------------
globals:
  - id: use_pwm_mode
    type: bool
    restore_value: no
    initial_value: 'true'

  - id: fan_speed_level
    type: float
    restore_value: no
    initial_value: '0.0'

# -----------------------------
# PWM or MOSFET Modus
# ----
switch:
  - platform: template
    name: "PWM direkt Modus"
    id: pwm_mode_switch
    optimistic: true
    restore_mode: DISABLED
    turn_on_action:
      - lambda: |-
          id(use_pwm_mode) = true;
          id(fan_mode_text).update();
          id(fan_pwm_output).set_level(id(fan_speed_level));
          id(mosfet_pwm_output).set_level(1.0);  // MOSFET voll durchgeschaltet im PWM-Modus
    turn_off_action:
      - lambda: |-
          id(use_pwm_mode) = false;
          id(fan_mode_text).update();
          id(mosfet_pwm_output).set_level(id(fan_speed_level));
          id(fan_pwm_output).set_level(0.0);

# -----------------------------
# Textsensor zur Anzeige des Modus
# -----------------------------
text_sensor:
  - platform: template
    name: "Lüftermodus"
    id: fan_mode_text
    lambda: |-
      if (id(use_pwm_mode)) {
        return std::string("PWM direkt");
      } else {
        return std::string("MOSFET-Spannung");
      }
    update_interval: never

# -----------------------------
# Startup-Skript zur Erkennung des Lüftertyps
# -----------------------------
script:
  - id: startup_check
    mode: queued
    
    then:
      - logger.log: "Starte automatische Lüftererkennung..."
      - output.set_level:
          id: mosfet_pwm_output
          level: 1.0  # Sicherstellen, dass Lüfter Strom bekommt
      - output.set_level:
          id: fan_pwm_output
          level: 0.0  # 0 % Testsignal
      - delay: 2s
      - lambda: |-
          bool pwm_works = (
            id(fan_pulse_1).state < 100 &&
            id(fan_pulse_2).state < 100 &&
            id(fan_pulse_3).state < 100 &&
            id(fan_pulse_4).state < 100 &&
            id(fan_pulse_5).state < 100
          );

          if (pwm_works) {
            ESP_LOGI("fan", "Mindestens ein Lüfter hat RPM < 100 – PWM-Modus aktiviert");
            id(pwm_mode_switch).turn_on();
          } else {
            ESP_LOGW("fan", "Kein Lüfter reagiert auf PWM – MOSFET-Modus aktiviert");
            id(pwm_mode_switch).turn_off();
          }
