substitutions:
  devicename: og-az-us-room

esphome:
  name: ${devicename}
  platform: ESP8266
  board: nodemcuv2

  on_boot:
    then:
#      - rtttl.play: 'MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'
#      - rtttl.play: 'mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6'
#      - rtttl.play: 'back:d=4,o=6,b=200:4g4,4a#5,4d#5,4a#4,4a#5,4g4,4a#5,4d#5,4a#5,4d#6,4a#4,32g4,4g6,1a#.6,2d7,2g.6,2f.6,4g6,1a#.6,2d7,2g6,4c7,2a#.6,4g6,1a#.6,2d7,2g.6,1f.6,2g.6,2a#.6,2d7,2c.7,2a#.6,4g6,1a#.6,2d7,2g.6,2f.6,4g6,1a#.6,2d7,'
      - rtttl.play: 'BackToTheFuture:d=4,o=5,b=160:p,8c.,16p,g,16p,16c.6,16p,a#.,16p,16a,16p,16g,16p,8a,16p,8g,16p,8f,16p,1g.,1p,g.,16p,c.,16p,2f#.,16p,32g.,32p,32a.,32p,8g,32p,8e,32p,8c,32p,f#,16p,32g.,32p,32a.,32p,8g.,32p,8d.,32p,8g.,32p,8d.6,32p,d6,16p,c#6,16p,32b.,32p,32c#.6,32p,2d6'
#      - rtttl.play: 'PlayedAl:d=4,o=5,b=355:8g#,p,g#,8p,8f,2g#,8p,8g#,p,g#,8p,8f,g#.,f,8g#,g,g,8p,8d#,2g,8p,8g,p,g,8p,8d#,g.,d#,8g,d#,d#,8p,8c,2d#,8p,8d#,p,d#,8p,8c,d#.,c,8d#,f,c#6,8p,8f,2c#6,8p,8f,p,c#6,8p,8f,c6,8p,a#,8g#,g#,g#,8p,8f,2g#,8p,8g#,p,g#,8p,8f,g#.'
#      - rtttl.play: 'Ghostbus:d=16,o=5,b=112:g,g,8b,8g,8a,4f.,g,g,g,g,8f,4g.,g,g,8b,8g,8a,4f.,g,g,g,g,8f,8a,8g,4d.,g,g,8b,8g,8a,4f.,g,g,g,g,8f,4g.';

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
  - service: play_rtttl
    variables:
      song_str: string
    then:
      - rtttl.play:
          rtttl: !lambda 'return song_str;'

  - service: write_tag_random
    then:
    - lambda: |-
        static const char alphanum[] = "0123456789abcdef";
        std::string uri = "//www.home-assistant.io/tag/";
        for (int i = 0; i < 8; i++)
          uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
        uri += "-";
        for (int j = 0; j < 3; j++) {
          for (int i = 0; i < 4; i++)
            uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
          uri += "-";
        }
        for (int i = 0; i < 12; i++)
          uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
        auto message = new nfc::NdefMessage();
        message->add_uri_record(uri);
        ESP_LOGD("tagreader", "Writing payload: %s", uri.c_str());
        id(pn532_board).write_mode(message);

  - service: write_tag_id
    variables:
      tag_id: string
    then:
    - lambda: |-
        auto message = new nfc::NdefMessage();
        std::string uri = "//www.home-assistant.io/tag/";
        uri += tag_id;
        message->add_uri_record(uri);
        id(pn532_board).write_mode(message);

  - service: clean_tag
    then:
    - lambda: 'id(pn532_board).clean_mode();'

  - service: cancel_writing
    then:
    - lambda: 'id(pn532_board).read_mode();'

ota:
  password: "37c297f04462d9e0bba70543ebbfa7ae"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 192.168.1.95
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${devicename}"
    password: "DSFKr3I1aBXy"

captive_portal:

    
# Example configuration entry
light:
  - platform: neopixelbus
    id: neopixel
    name: "NeoPixel Light"
    type: RGB
    variant: WS2812
    pin: RX
    num_leds: 12
    internal: true

  - platform: partition
    name: "Status LED 01"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 0
        to: 0

  - platform: partition
    name: "Status LED 02"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 1
        to: 1

  - platform: partition
    name: "Status LED 03"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 2
        to: 2

  - platform: partition
    name: "Status LED 04"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 3
        to: 3

  - platform: partition
    name: "Status LED 05"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 4
        to: 4

  - platform: partition
    name: "Status LED 06"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 5
        to: 5

  - platform: partition
    name: "Status LED 07"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 6
        to: 6

  - platform: partition
    name: "Status LED 08"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 7
        to: 7

  - platform: partition
    name: "Status LED 09"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 8
        to: 8

  - platform: partition
    name: "Status LED 10"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 9
        to: 9

  - platform: partition
    name: "Status LED 11"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 10
        to: 10

  - platform: partition
    name: "Status LED 12"
    segments:
      # Use first 10 LEDs from the light with ID light1
      - id: neopixel
        from: 11
        to: 11


#RFID - PN532
# Reset Pin D7
# SDA D5
# SCL D6
#
# schau mal bei og-wz-us-tag
# abweichend port D1 / D2
i2c:
  sda: D5
  scl: D6
  scan: true
  id: bus_a
  
pn532_i2c:
  update_interval: 1s
  id: pn532_board
  on_tag:
    then:
    - homeassistant.tag_scanned: !lambda |
        if (!tag.has_ndef_message()) {
          ESP_LOGD("tagreader", "No NDEF");
          return x;
        }
        auto message = tag.get_ndef_message();
        auto records = message->get_records();
        for (auto &record : records) {
          std::string payload = record->get_payload();
          size_t pos = payload.find("//www.home-assistant.io/tag/");
          if (pos != std::string::npos) {
            return payload.substr(pos + 34);
          }
        }
        ESP_LOGD("tagreader", "Bad NDEF, fallback to uid");
        return x;

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
    name: "${devicename} Radar Sensor"
    device_class: motion

  - platform: status
    name: "${devicename} Status"

switch:
  - platform: restart
    name: "${devicename} Neustart"

sensor:
  - platform: wifi_signal
    name: "${devicename} Wi-Fi Signal"
    update_interval: 60s

  - platform: uptime
    name: "${devicename} Uptime"
    update_interval: 60s

# Buzzer  D6
output:
  - platform: esp8266_pwm
    pin: D2
    frequency: 1000 Hz
    id: rtttl_out
      
rtttl:
  output: rtttl_out
  on_finished_playback:
    - logger.log: 'Song ended!'
