-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagtuner-core-nfc.yaml
295 lines (281 loc) · 7.4 KB
/
tagtuner-core-nfc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# this is a modified PN532 to make it work with 888byte tags
external_components:
# - source: ./components
- source: github://luka6000/TagTuner@main
components: [ pn532 ]
refresh: 1min
binary_sensor:
- platform: template
name: "=Tag="
id: is_tag
device_class: vibration
entity_category: DIAGNOSTIC
disabled_by_default: true
- platform: template
name: "=Reading="
id: txt_reading
device_class: running
entity_category: DIAGNOSTIC
lambda: |-
if ( !id(pn532_board).is_writing() ) {
return true;
} else {
return false;
}
- platform: template
name: "=Writing="
id: txt_writing
device_class: running
entity_category: DIAGNOSTIC
lambda: |-
if ( id(pn532_board).is_writing() ) {
return true;
} else {
return false;
}
script:
- id: wait_input
then:
- delay: 3s
- text_sensor.template.publish:
id: status
state: "Waiting for input"
- id: set_tag
then:
- text.set:
id: playlist_artist
value: !lambda |-
return id(artist);
- delay: 10ms
- text.set:
id: playlist_info
value: !lambda |-
return id(playlist);
- delay: 10ms
- text.set:
id: playlist_uri
value: !lambda |-
ESP_LOGD("tagtuner", "URI NDEF");
return id(uri);
text:
- platform: template
id: playlist_artist
name: "Playlist artist"
icon: mdi:account-music
entity_category: CONFIG
optimistic: true
min_length: 0
max_length: 50
mode: text
initial_value: " "
- platform: template
id: playlist_info
name: "Playlist name or album title"
icon: mdi:playlist-music
entity_category: CONFIG
optimistic: true
min_length: 0
max_length: 100
mode: text
initial_value: " "
- platform: template
id: playlist_uri
name: "Playlist URI"
icon: mdi:link-variant
entity_category: CONFIG
optimistic: true
min_length: 0
max_length: 255
mode: text
initial_value: " "
text_sensor:
- platform: template
id: status
name: "Status"
icon: mdi:ladybug
entity_category: DIAGNOSTIC
on_value:
if:
condition:
lambda: 'return id(status).state != "Waiting for input";'
then:
- script.stop: wait_input
- script.execute: wait_input
button:
- platform: restart
name: "Restart"
id: btn_restart
entity_category: DIAGNOSTIC
- platform: template
name: Cancel writing
id: btn_cancel_writing
icon: "mdi:broadcast-off"
entity_category: CONFIG
on_press:
then:
- lambda: 'id(pn532_board).read_mode();'
- wait_until:
timeout: 30s
condition:
not:
pn532.is_writing:
- delay: 5ms
- text_sensor.template.publish:
id: status
state: "Cancel writing tag"
- script.execute: led_ok
- platform: template
name: Erase Tag
id: btn_erase_tag
icon: "mdi:nfc-search-variant"
entity_category: CONFIG
on_press:
then:
- script.execute: led_tagwrite
- text.set:
id: playlist_artist
value: ''
- text.set:
id: playlist_info
value: ''
- text.set:
id: playlist_uri
value: ''
- lambda: 'id(pn532_board).format_mode();'
- text_sensor.template.publish:
id: status
state: "Place tag"
- wait_until:
timeout: 30s
condition:
not:
pn532.is_writing:
- if:
condition:
pn532.is_writing:
then:
- lambda: 'id(pn532_board).read_mode();'
- text_sensor.template.publish:
id: status
state: "Finished erasing tag"
- script.execute: led_success
- platform: template
name: Write Tag
id: btn_write_tag
icon: "mdi:cast-audio-variant"
entity_category: CONFIG
on_press:
then:
- script.execute: led_tagwrite
- lambda: |-
auto message = new nfc::NdefMessage();
message->add_text_record("TagTuner");
std::string uri = "";
std::string artist = "artist/";
std::string playlist = "playlist/";
uri += id(playlist_uri).state;
artist += id(playlist_artist).state;
playlist += id(playlist_info).state;
if ( artist != "" ) {
message->add_text_record(artist);
}
if ( playlist != "" ) {
message->add_text_record(playlist);
}
if ( uri != "" ) {
message->add_uri_record(uri);
}
ESP_LOGD("ndef", "Writing payload: %s", uri.c_str());
id(pn532_board).write_mode(message);
- text_sensor.template.publish:
id: status
state: "Place tag"
- wait_until:
timeout: 30s
condition:
not:
pn532.is_writing:
- if:
condition:
pn532.is_writing:
then:
- lambda: 'id(pn532_board).read_mode();'
- text_sensor.template.publish:
id: status
state: "Finished writing tag"
- script.execute: led_success
globals:
- id: artist
type: std::string
- id: playlist
type: std::string
- id: uri
type: std::string
pn532_i2c:
- id: pn532_board
i2c_id: bus_i2c
update_interval: 350ms
on_tag_removed:
- binary_sensor.template.publish:
id: is_tag
state: false
- text_sensor.template.publish:
id: status
state: "Tag removed"
- script.execute:
id: tagtuner_event
action: "tagRemoved"
uid: !lambda 'return x;'
uri: ""
artist: ""
playlist: ""
- script.execute: led_blink
on_tag:
- binary_sensor.template.publish:
id: is_tag
state: true
- text_sensor.template.publish:
id: status
state: !lambda 'return "Tag "+x;'
- lambda: |-
id(playlist)="";
id(artist)="";
id(uri)="";
if (tag.has_ndef_message()) {
for (auto &record : tag.get_ndef_message()->get_records() ) {
std::string payload = record->get_payload();
std::string type = record->get_type();
if ( payload.substr(0, 7) == "artist/" ) {
id(artist)=payload.substr(7);
}
else if ( payload.substr(0, 9) == "playlist/" ) {
id(playlist)=payload.substr(9);
}
else if (type == "U"
&& payload.substr(0, 20) != "https://mb.senic.com"
&& payload.substr(0, 23) != "https://www.philips.com"
) {
id(uri)=payload;
}
}
}
- if:
condition:
lambda: 'return id(uri) == "" ;'
then:
- text_sensor.template.publish:
id: status
state: "Plain UID tag"
- homeassistant.tag_scanned: !lambda |-
ESP_LOGD("tagtuner", "No TagTuner NDEF, using UID");
return x;
else:
- script.execute:
id: tagtuner_event
action: "tagScanned"
uid: !lambda 'return x;'
uri: !lambda 'return id(uri);'
artist: !lambda 'return id(artist);'
playlist: !lambda 'return id(playlist);'
- script.execute: set_tag
- script.execute: led_ok