fix add and delete actions

This commit is contained in:
NP v/d Spek 2024-11-17 21:11:35 +01:00
parent ff4469f828
commit ea814e3c63
4 changed files with 48 additions and 12 deletions

View file

@ -278,7 +278,7 @@ async def send_action(config, action_id, template_arg, args):
key=CONF_PEER, 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) var = cg.new_Pvariable(action_id, template_arg)
template_ = await cg.templatable(config[CONF_PEER], args, cg.uint64) template_ = await cg.templatable(config[CONF_PEER], args, cg.uint64)
cg.add(var.set_peer(template_)) cg.add(var.set_peer(template_))

View file

@ -316,26 +316,20 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
template<typename... Ts> class NewPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> { template<typename... Ts> class NewPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public: public:
template<typename V> void set_peer(V peer) { this->peer_ = peer; } TEMPLATABLE_VALUE(uint64_t, peer);
void play(Ts... x) override { void play(Ts... x) override {
auto peer = this->peer_.value(x...); auto peer = this->peer_.value(x...);
parent_->add_peer(peer); parent_->add_peer(peer);
} }
protected:
TemplatableValue<uint64_t, Ts...> mac_{};
}; };
template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> { template<typename... Ts> class DelPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
public: public:
template<typename V> void set_peer(V peer) { this->peer_ = peer; } TEMPLATABLE_VALUE(uint64_t, peer);
void play(Ts... x) override { void play(Ts... x) override {
auto peer = this->peer_.value(x...); auto peer = this->peer_.value(x...);
parent_->del_peer(peer); parent_->del_peer(peer);
} }
protected:
TemplatableValue<uint64_t, Ts...> peer_{};
}; };
class ESPNowSentTrigger : public Trigger<const ESPNowPacket, bool> { class ESPNowSentTrigger : public Trigger<const ESPNowPacket, bool> {

View file

@ -7,6 +7,7 @@ esp32:
board: esp32dev board: esp32dev
framework: framework:
type: esp-idf type: esp-idf
# version: 5.1.5
esphome: esphome:
name: "${name}" name: "${name}"
@ -56,3 +57,12 @@ interval:
- espnow.broatcast: - espnow.broatcast:
payload: "hallo everyone" payload: "hallo everyone"
command: 123 command: 123
binary_sensor:
- platform: gpio
pin: GPIO39
name: Button
on_click:
- espnow.peer.new:
peer: kGf7Ii0t
- espnow.peer.del: kGf7Ii0t

View file

@ -7,14 +7,46 @@ espnow:
auto_add_peer: true auto_add_peer: true
peers: peers:
- FF:FF:FF:FF:FF:FF - FF:FF:FF:FF:FF:FF
- 0xFFFFFFFFFFFF
- ZZZZZZZZ
on_receive: on_receive:
- logger.log: - logger.log:
format: "Received: %s RSSI: %d" format: "Received: '%s' from '%s' command: %d RSSI: %d"
args: [packet.get_payload(), packet.rssi] 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: binary_sensor:
- platform: gpio - platform: gpio
pin: GPIO39 pin: GPIO39
name: Button name: Button
on_click: on_click:
- espnow.send: "hallo everyone" - espnow.peer.new:
peer: kGf7Ii0t
- espnow.peer.del: kGf7Ii0t