sensor:
  - platform: wifi_signal
    name: "NFC Playbox WiFi RSSI"
    update_interval: 30s

  - platform: uptime
    name: "NFC Playbox Uptime"
    
  - platform: homeassistant
    id: spotvol
    entity_id: media_player.ada
    attribute: volume_level
    on_value:
      then:
        - lambda: |-
            ESP_LOGI("vol", "HA volume_level x=%.3f", (float)x);
            if (!isnan(x)) {
              id(local_vol) = x;
              ESP_LOGI("vol", "local_vol updated to %.3f", id(local_vol));
            } else {
              ESP_LOGW("vol", "HA volume_level is NaN (ignoring)");
            }
        - component.update: display_main
   # filters:
   # - multiply: 100

binary_sensor:
  - platform: homeassistant
    entity_id: input_boolean.esp32_spotify_volume
    id: display_volume

script:
  - id: wake_screen
    mode: restart
    then:
        if:
          condition:
            light.is_off: backlight
          then:
            - light.turn_on:
                id: backlight
                brightness: 100%
                transition_length: 0s
            - delay: 60s
            - light.turn_off: backlight
            - lambda: |-
                id(current_image) = 0;
            - component.update: display_main 

  - id: send_volume_debounced
    mode: restart
    then:
      - delay: 400ms
      - homeassistant.service:
          service: media_player.volume_set
          data:
            entity_id: media_player.ada
            volume_level: !lambda "ESP_LOGI(\"vol\", \"debounced send volume_set=%.3f\", id(local_vol)); return id(local_vol);"

  - id: volume_flash_hide
    mode: restart
    then:
      - delay: 5s
      - lambda: |-
          id(show_volume) = false;
      - component.update: display_main

  - id: set_vol_and_flash
    mode: restart
    parameters:
      level: float
    then:
      - lambda: |-
          float clamped = std::max(0.0f, std::min(0.4f, level));
          ESP_LOGI("vol", "set_vol_and_flash target=%.3f (clamped)", clamped);
          id(local_vol) = clamped;
          id(show_volume) = true;
          ESP_LOGI("vol", "show_volume=true, local_vol=%.3f", id(local_vol));
      - component.update: display_main
      - script.execute: send_volume_debounced        
      - script.execute: volume_flash_hide   

  - id: volume_up_trigger
    then:
      - lambda: |-
          float step = 0.02f;
          float target = id(local_vol) + step;
          ESP_LOGI("vol", "volume_up_trigger step=%.2f from=%.3f -> %.3f", step, id(local_vol), target);
          id(set_vol_and_flash)->execute(target);

  - id: volume_down_trigger
    then:
      - lambda: |-
          float step = 0.02f;
          float target = id(local_vol) - step;
          ESP_LOGI("vol", "volume_down_trigger step=%.2f from=%.3f -> %.3f", step, id(local_vol), target);
          id(set_vol_and_flash)->execute(target);


rc522_spi:
  spi_id: spi_bus_rc522
  cs_pin: 12
  reset_pin: 11
  update_interval: 3s
  on_tag:
    then:
      - homeassistant.tag_scanned: !lambda 'return x;'
      - lambda: |-
           std::string uid = x;
           ESP_LOGI("nfc", "Tag scanned: %s", uid.c_str());
           //  
           if (uid == "04-A1-4E-94-2E-02-89") { // spellista_dolly
             id(current_image) = 1;
           } else if (uid == "04-01-BA-52-2E-02-89") { // spellista_godnattstund
             id(current_image) = 2;
           } else if (uid == "04-51-0C-91-2E-02-89") { // spellista_disco
             id(current_image) = 3; 
           } else if (uid == "04-01-DF-98-2E-02-89") { // spellista_rosahelikopter
             id(current_image) = 4; 
           } else {
             id(current_image) = 0;
           }
           ESP_LOGI("nfc", "current_image set to %d", id(current_image));
      - component.update: display_main
      
output:
  - platform: ledc
    pin: 1
    id: lcd_bl