substitutions:
  devicename: og-wz-us-tag

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

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
  - 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: "f0e515a756b864724c599f2d077461a8"

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

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

captive_portal:

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
    
# Reader
# GPIO ⇄ SDA: D1
# GPIO → SCL: D2
# Reset PIN D5
# PN 532
# 0x3C (60) Display
# 0x3C (61) Reader ???
# schau mal beim sensor esp05 og-az-us-room
i2c:
  sda: D1
  scl: D2
  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: pn532
    uid: 74-10-37-94
    name: "${devicename} NFC Tag"

text_sensor:
  - platform: homeassistant
    entity_id: "sensor.og_wz_us_tag_display_text"
    name: "${devicename} Display Text"
    id: info_state

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    #reset_pin: D0
    address: 0x3C
    rotation: 180
    lambda: |-
      //Printf "Alarm State: <state>" in top center
      it.printf(64, 0, id(my_font), TextAlign::TOP_RIGHT, "Info: %s", id(info_state).state.c_str());

font:
  - file: "fonts/Comic Sans MS.ttf"
    id: my_font
    size: 20
