diff --git a/esphome/components/espnow/__init__.py b/esphome/components/espnow/__init__.py index 6aa3104aac..bbc53a3361 100644 --- a/esphome/components/espnow/__init__.py +++ b/esphome/components/espnow/__init__.py @@ -27,7 +27,9 @@ ESPNowReceiveTrigger = espnow_ns.class_( ESPNowNewPeerTrigger = espnow_ns.class_( "ESPNowNewPeerTrigger", automation.Trigger.template() ) - +ESPNowBroadcaseTrigger = espnow_ns.class_( + "ESPNowBroadcaseTrigger", automation.Trigger.template() +) SendAction = espnow_ns.class_("SendAction", automation.Action) NewPeerAction = espnow_ns.class_("NewPeerAction", automation.Action) DelPeerAction = espnow_ns.class_("DelPeerAction", automation.Action) @@ -37,6 +39,7 @@ CONF_CONFORMATION_TIMEOUT = "conformation_timeout" CONF_ESPNOW = "espnow" CONF_RETRIES = "retries" CONF_ON_RECEIVE = "on_receive" +CONF_ON_BROADCAST = "on_broadcast" CONF_ON_SENT = "on_sent" CONF_ON_NEW_PEER = "on_new_peer" CONF_PEER = "peer" @@ -115,10 +118,16 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional(CONF_COMMAND): cv.Range(min=16, max=255), } ), + cv.Optional(CONF_ON_BROADCAST): automation.validate_automation( + { + cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPNowBroadcaseTrigger), + cv.Optional(CONF_COMMAND): cv.Range(min=0, max=255), + } + ), cv.Optional(CONF_ON_SENT): automation.validate_automation( { cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPNowSentTrigger), - cv.Optional(CONF_COMMAND): cv.Range(min=16, max=255), + cv.Optional(CONF_COMMAND): cv.Range(min=0, max=255), } ), cv.Optional(CONF_ON_NEW_PEER): automation.validate_automation( @@ -169,6 +178,14 @@ async def to_code(config): trigger, [(ESPNowPacketConst, "packet")], conf ) + for conf in config.get(CONF_ON_BROADCAST, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) + if CONF_COMMAND in conf: + cg.add(trigger.set_command(conf[CONF_COMMAND])) + await automation.build_automation( + trigger, [(ESPNowPacketConst, "packet")], conf + ) + for conf in config.get(CONF_ON_NEW_PEER, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) if CONF_COMMAND in conf: diff --git a/esphome/components/espnow/espnow.h b/esphome/components/espnow/espnow.h index 3d64790a05..acde03b378 100644 --- a/esphome/components/espnow/espnow.h +++ b/esphome/components/espnow/espnow.h @@ -191,7 +191,7 @@ class ESPNowDefaultProtocol : public ESPNowProtocol { this->on_broadcast_.add(std::move(callback)); } - void on_broadcast(const ESPNowPacket &packet) override { + bool on_broadcast(const ESPNowPacket &packet) override { this->on_broadcast_.call(packet); return this->on_broadcast_.size() > 0; }; @@ -207,7 +207,7 @@ class ESPNowDefaultProtocol : public ESPNowProtocol { void add_on_peer_callback(std::function &&callback) { this->on_new_peer_.add(std::move(callback)); } - void on_new_peer(const ESPNowPacket &packet) override { + bool on_new_peer(const ESPNowPacket &packet) override { this->on_new_peer_.call(packet); return this->on_new_peer_.size() > 0; }; diff --git a/esphome/components/espnow/test.yaml b/esphome/components/espnow/test.yaml index 41ad6e945a..ddd69fb236 100644 --- a/esphome/components/espnow/test.yaml +++ b/esphome/components/espnow/test.yaml @@ -39,7 +39,7 @@ espnow: [ packet.get_payload(), packet.get_peer_code().c_str(), - packet.command, + packet.get_command(), packet.rssi, ] @@ -48,7 +48,7 @@ espnow: then: - logger.log: format: "Broadcast Received from: '%s' RSSI: %d: %s" - args: [packet.get_peer_code().c_str(), packet.rssi] + args: [packet.get_peer_code().c_str(), packet.rssi, packet.get_payload()] interval: - interval: 10sec