From 944b2f7cbb7d2866b4a75e9961f3aaea9162fadd Mon Sep 17 00:00:00 2001 From: luka6000 Date: Tue, 5 Nov 2024 22:17:24 +0100 Subject: [PATCH] memory optimizations --- tagtuner-atom-grove.yaml | 95 ++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/tagtuner-atom-grove.yaml b/tagtuner-atom-grove.yaml index 69f1511..66eb425 100644 --- a/tagtuner-atom-grove.yaml +++ b/tagtuner-atom-grove.yaml @@ -25,6 +25,10 @@ esphome: effect: HelloWorld - delay: 1000ms - light.turn_off: led1 + - wait_until: + condition: + api.connected: + timeout: 20s - text_sensor.template.publish: id: status state: "Ready" @@ -197,6 +201,13 @@ light: duration: 50ms script: + - id: wait_input + then: + - delay: 3s + - text_sensor.template.publish: + id: status + state: "Waiting for input" + - id: led_blink then: - light.turn_on: @@ -292,10 +303,8 @@ text_sensor: condition: lambda: 'return id(status).state != "Waiting for input";' then: - - delay: 3s - - text_sensor.template.publish: - id: status - state: "Waiting for input" + - script.stop: wait_input + - script.execute: wait_input sensor: - platform: rotary_encoder @@ -460,6 +469,10 @@ pn532_i2c: - text_sensor.template.publish: id: status state: "Tag removed" + - wait_until: + condition: + api.connected: + timeout: 20s - homeassistant.event: event: esphome.tagtuner data: @@ -471,28 +484,20 @@ pn532_i2c: - logger.log: "on_tag" - text_sensor.template.publish: id: status - state: "Tag read" - - text_sensor.template.publish: - id: status - state: !lambda 'return x;' + state: !lambda 'return "Tag "+x;' - lambda: |- id(playlist)=""; id(artist)=""; id(uri)=""; - if (tag.has_ndef_message()) { - auto message = tag.get_ndef_message(); - auto records = message->get_records(); - for (auto &record : records) { + for (auto &record : tag.get_ndef_message()->get_records() ) { std::string payload = record->get_payload(); std::string type = record->get_type(); - std::string tag_artist = payload.substr(0, 7); - std::string tag_playlist = payload.substr(0, 9); - if ( tag_artist == "artist/" ) { + if ( payload.substr(0, 7) == "artist/" ) { id(artist)=payload.substr(7); } - else if ( tag_playlist == "playlist/" ) { + else if ( payload.substr(0, 9) == "playlist/" ) { id(playlist)=payload.substr(9); } else if (type == "U" && payload.substr(0, 20) != "https://mb.senic.com" ) { @@ -505,32 +510,36 @@ pn532_i2c: 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; + - 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: - - text.set: - id: playlist_artist - value: !lambda |- - return id(artist); - - text.set: - id: playlist_info - value: !lambda |- - return id(playlist); - - text.set: - id: playlist_uri - value: !lambda |- - ESP_LOGD("tagtuner", "URI NDEF"); - return id(uri); - - homeassistant.event: - event: esphome.tagtuner - data: - action: "tag_scanned" - uid: !lambda 'return x;' - uri: !lambda 'return id(uri);' - artist: !lambda 'return id(artist);' - playlist: !lambda 'return id(playlist);' + - wait_until: + condition: + api.connected: + timeout: 20s + - homeassistant.event: + event: esphome.tagtuner + data: + action: "tag_scanned" + uid: !lambda 'return x;' + uri: !lambda 'return id(uri);' + artist: !lambda 'return id(artist);' + playlist: !lambda 'return id(playlist);' + - text.set: + id: playlist_artist + value: !lambda |- + return id(artist); + - text.set: + id: playlist_info + value: !lambda |- + return id(playlist); + - text.set: + id: playlist_uri + value: !lambda |- + ESP_LOGD("tagtuner", "URI NDEF"); + return id(uri); - script.execute: led_blink