Adding the on_broadcast for real now.

This commit is contained in:
NP v/d Spek 2024-11-17 18:39:24 +01:00
parent f42fee8e62
commit ff4469f828
3 changed files with 23 additions and 6 deletions

View file

@ -27,7 +27,9 @@ ESPNowReceiveTrigger = espnow_ns.class_(
ESPNowNewPeerTrigger = espnow_ns.class_( ESPNowNewPeerTrigger = espnow_ns.class_(
"ESPNowNewPeerTrigger", automation.Trigger.template() "ESPNowNewPeerTrigger", automation.Trigger.template()
) )
ESPNowBroadcaseTrigger = espnow_ns.class_(
"ESPNowBroadcaseTrigger", automation.Trigger.template()
)
SendAction = espnow_ns.class_("SendAction", automation.Action) SendAction = espnow_ns.class_("SendAction", automation.Action)
NewPeerAction = espnow_ns.class_("NewPeerAction", automation.Action) NewPeerAction = espnow_ns.class_("NewPeerAction", automation.Action)
DelPeerAction = espnow_ns.class_("DelPeerAction", automation.Action) DelPeerAction = espnow_ns.class_("DelPeerAction", automation.Action)
@ -37,6 +39,7 @@ CONF_CONFORMATION_TIMEOUT = "conformation_timeout"
CONF_ESPNOW = "espnow" CONF_ESPNOW = "espnow"
CONF_RETRIES = "retries" CONF_RETRIES = "retries"
CONF_ON_RECEIVE = "on_receive" CONF_ON_RECEIVE = "on_receive"
CONF_ON_BROADCAST = "on_broadcast"
CONF_ON_SENT = "on_sent" CONF_ON_SENT = "on_sent"
CONF_ON_NEW_PEER = "on_new_peer" CONF_ON_NEW_PEER = "on_new_peer"
CONF_PEER = "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_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.Optional(CONF_ON_SENT): automation.validate_automation(
{ {
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESPNowSentTrigger), 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( cv.Optional(CONF_ON_NEW_PEER): automation.validate_automation(
@ -169,6 +178,14 @@ async def to_code(config):
trigger, [(ESPNowPacketConst, "packet")], conf 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, []): for conf in config.get(CONF_ON_NEW_PEER, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
if CONF_COMMAND in conf: if CONF_COMMAND in conf:

View file

@ -191,7 +191,7 @@ class ESPNowDefaultProtocol : public ESPNowProtocol {
this->on_broadcast_.add(std::move(callback)); 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); this->on_broadcast_.call(packet);
return this->on_broadcast_.size() > 0; return this->on_broadcast_.size() > 0;
}; };
@ -207,7 +207,7 @@ class ESPNowDefaultProtocol : public ESPNowProtocol {
void add_on_peer_callback(std::function<void(const ESPNowPacket)> &&callback) { void add_on_peer_callback(std::function<void(const ESPNowPacket)> &&callback) {
this->on_new_peer_.add(std::move(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); this->on_new_peer_.call(packet);
return this->on_new_peer_.size() > 0; return this->on_new_peer_.size() > 0;
}; };

View file

@ -39,7 +39,7 @@ espnow:
[ [
packet.get_payload(), packet.get_payload(),
packet.get_peer_code().c_str(), packet.get_peer_code().c_str(),
packet.command, packet.get_command(),
packet.rssi, packet.rssi,
] ]
@ -48,7 +48,7 @@ espnow:
then: then:
- logger.log: - logger.log:
format: "Broadcast Received from: '%s' RSSI: %d: %s" 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:
- interval: 10sec - interval: 10sec