From ea814e3c63e90fc5d1c02433938112ce447139c8 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Sun, 17 Nov 2024 21:11:35 +0100 Subject: [PATCH] fix add and delete actions --- esphome/components/espnow/__init__.py | 2 +- esphome/components/espnow/espnow.h | 10 ++----- esphome/components/espnow/test.yaml | 10 +++++++ tests/components/espnow/common.yaml | 38 ++++++++++++++++++++++++--- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/esphome/components/espnow/__init__.py b/esphome/components/espnow/__init__.py index bbc53a3361..49ca3bbb16 100644 --- a/esphome/components/espnow/__init__.py +++ b/esphome/components/espnow/__init__.py @@ -278,7 +278,7 @@ async def send_action(config, action_id, template_arg, args): key=CONF_PEER, ), ) -async def del_peer_action(config, action_id, template_arg, args): +async def add_del_peer_action(config, action_id, template_arg, args): var = cg.new_Pvariable(action_id, template_arg) template_ = await cg.templatable(config[CONF_PEER], args, cg.uint64) cg.add(var.set_peer(template_)) diff --git a/esphome/components/espnow/espnow.h b/esphome/components/espnow/espnow.h index acde03b378..7f5e0fa5ed 100644 --- a/esphome/components/espnow/espnow.h +++ b/esphome/components/espnow/espnow.h @@ -316,26 +316,20 @@ template class SendAction : public Action, public Parente template class NewPeerAction : public Action, public Parented { public: - template void set_peer(V peer) { this->peer_ = peer; } + TEMPLATABLE_VALUE(uint64_t, peer); void play(Ts... x) override { auto peer = this->peer_.value(x...); parent_->add_peer(peer); } - - protected: - TemplatableValue mac_{}; }; template class DelPeerAction : public Action, public Parented { public: - template void set_peer(V peer) { this->peer_ = peer; } + TEMPLATABLE_VALUE(uint64_t, peer); void play(Ts... x) override { auto peer = this->peer_.value(x...); parent_->del_peer(peer); } - - protected: - TemplatableValue peer_{}; }; class ESPNowSentTrigger : public Trigger { diff --git a/esphome/components/espnow/test.yaml b/esphome/components/espnow/test.yaml index ddd69fb236..0165ca323c 100644 --- a/esphome/components/espnow/test.yaml +++ b/esphome/components/espnow/test.yaml @@ -7,6 +7,7 @@ esp32: board: esp32dev framework: type: esp-idf + # version: 5.1.5 esphome: name: "${name}" @@ -56,3 +57,12 @@ interval: - espnow.broatcast: payload: "hallo everyone" command: 123 + +binary_sensor: + - platform: gpio + pin: GPIO39 + name: Button + on_click: + - espnow.peer.new: + peer: kGf7Ii0t + - espnow.peer.del: kGf7Ii0t diff --git a/tests/components/espnow/common.yaml b/tests/components/espnow/common.yaml index 83030aabde..1fb18b7cdc 100644 --- a/tests/components/espnow/common.yaml +++ b/tests/components/espnow/common.yaml @@ -7,14 +7,46 @@ espnow: auto_add_peer: true peers: - FF:FF:FF:FF:FF:FF + - 0xFFFFFFFFFFFF + - ZZZZZZZZ on_receive: - logger.log: - format: "Received: %s RSSI: %d" - args: [packet.get_payload(), packet.rssi] + format: "Received: '%s' from '%s' command: %d RSSI: %d" + args: + [ + packet.get_payload(), + packet.get_peer_code().c_str(), + packet.get_command(), + packet.rssi, + ] + + on_broadcast: + - command: 123 + then: + - logger.log: + format: "Broadcast Received from: '%s' RSSI: %d: %s" + args: + [ + packet.get_peer_code().c_str(), + packet.rssi, + packet.get_payload(), + ] + +interval: + - interval: 10sec + then: + - espnow.broatcast: + payload: "hallo everyone" + command: 123 + - espnow.send: + payload: "hallo everyone" + command: 123 binary_sensor: - platform: gpio pin: GPIO39 name: Button on_click: - - espnow.send: "hallo everyone" + - espnow.peer.new: + peer: kGf7Ii0t + - espnow.peer.del: kGf7Ii0t