Skip to content

Commit

Permalink
ndef code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
spllug committed Mar 13, 2022
1 parent b9e68aa commit b3715bd
Showing 1 changed file with 76 additions and 51 deletions.
127 changes: 76 additions & 51 deletions tagreader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ i2c:
scan: False
frequency: 400kHz

globals:
- id: source
type: std::string
- id: url
type: std::string
- id: info
type: std::string

pn532_i2c:
id: pn532_board
on_tag:
Expand All @@ -224,69 +232,86 @@ pn532_i2c:
green: 100%
blue: 0%
flash_length: 500ms

- lambda: |-
id(source)="";
id(url)="";
id(info)="";
if (tag.has_ndef_message()) {
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (auto &record : records) {
std::string payload = record->get_payload();
std::string type = record->get_type();
size_t hass = payload.find("https://www.home-assistant.io/tag/");
size_t applemusic = payload.find("https://open.spotify.com");
size_t spotify = payload.find("https://music.apple.com");
size_t sonos = payload.find("sonos-2://");
if (type == "U" and ( hass != std::string::npos ) {
ESP_LOGD("tagreader", "Found Home Assistant tag NDEF");
id(source)="hass";
id(url)=payload;
id(info)=payload.substr(pos + 34);
}
else if (type == "U" and ( applemusic != std::string::npos ) {
ESP_LOGD("tagreader", "Found Apple Music tag NDEF");
id(source)="amusic";
id(url)=payload;
}
else if (type == "U" and ( spotify != std::string::npos ) {
ESP_LOGD("tagreader", "Found Spotify tag NDEF");
id(source)="spotify";
id(url)=payload;
}
else if (type == "U" and ( sonos != std::string::npos ) {
ESP_LOGD("tagreader", "Found Sonos app tag NDEF");
id(source)="sonos";
id(url)=payload;
}
else if (type == "T" and id(source)=="sonos" {
ESP_LOGD("tagreader", "Found Sonos info tag NDEF");
id(info)=payload;
}
}
}
else {
id(source)="uid"
}
- if:
condition:
lambda: 'return ( id(source)=="uid" );'
then:
- homeassistant.tag_scanned: !lambda |-
ESP_LOGD("tagreader", "No NDEF");
return x;
else:
- if:
condition:
lambda: |
if (tag.has_ndef_message()) {
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (auto &record : records) {
std::string payload = record->get_payload();
std::string type = record->get_type();
size_t applemusic = payload.find("https://open.spotify.com");
size_t spotify = payload.find("https://music.apple.com");
if (type == "U" and (
applemusic != std::string::npos or
spotify != std::string::npos
)) {
ESP_LOGD("tagreader", "Found music tag NDEF");
return true;
}
}
}
return false;
lambda: 'return ( id(source)=="hass" )'
then:
- homeassistant.tag_scanned: !lambda 'return id(info);'
else:
- homeassistant.event:
event: esphome.music_tag
data_template:
reader: !lambda |
reader: !lambda |-
return App.get_name().c_str();
url: !lambda |
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (auto &record : records) {
std::string payload = record->get_payload();
std::string type = record->get_type();
size_t pos = payload.find("https://");
if (type == "U" and pos != std::string::npos) {
ESP_LOGD("tagreader", "Sending event music_tag");
return payload;
}
}
return x;
- 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("https://www.home-assistant.io/tag/");
if (pos != std::string::npos) {
return payload.substr(pos + 34);
}
}
ESP_LOGD("tagreader", "Not HA NDEF, fallback to uid");
return x;
source: !lambda |-
return id(source);
url: !lambda |-
return id(url);
info: !lambda |-
return id(info);
- if:
condition:
switch.is_on: buzzer_enabled
then:
- rtttl.play: "success:d=24,o=5,b=100:c,g,b"
- delay: 0.1s

- delay: 0.11s #to fix slow pn532 component

# Define the buzzer output
output:
Expand Down

0 comments on commit b3715bd

Please sign in to comment.