[wireguard] Use the apply helpers for the enable, disable, enabled and peer_online automations (#19616)

This commit is contained in:
J. Nick Koston
2026-09-25 11:04:51 -04:00
committed by GitHub
parent db566b25d0
commit d346835e79
2 changed files with 10 additions and 76 deletions
+10 -50
View File
@@ -41,16 +41,6 @@ _WG_KEY_REGEX = re.compile(r"^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw480]=$")
wireguard_ns = cg.esphome_ns.namespace("wireguard")
Wireguard = wireguard_ns.class_("Wireguard", cg.Component, cg.PollingComponent)
AllowedIP = wireguard_ns.struct("AllowedIP")
WireguardPeerOnlineCondition = wireguard_ns.class_(
"WireguardPeerOnlineCondition", automation.Condition
)
WireguardEnabledCondition = wireguard_ns.class_(
"WireguardEnabledCondition", automation.Condition
)
WireguardEnableAction = wireguard_ns.class_("WireguardEnableAction", automation.Action)
WireguardDisableAction = wireguard_ns.class_(
"WireguardDisableAction", automation.Action
)
def _wireguard_key(value):
@@ -163,47 +153,17 @@ async def to_code(config):
await cg.register_component(var, config)
@automation.register_condition(
"wireguard.peer_online",
WireguardPeerOnlineCondition,
cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)}),
WIREGUARD_AUTOMATION_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)})
automation.register_apply_condition(
"wireguard.peer_online", WIREGUARD_AUTOMATION_SCHEMA, "is_peer_up()"
)
async def wireguard_peer_up_to_code(config, condition_id, template_arg, args):
var = cg.new_Pvariable(condition_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@automation.register_condition(
"wireguard.enabled",
WireguardEnabledCondition,
cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)}),
automation.register_apply_condition(
"wireguard.enabled", WIREGUARD_AUTOMATION_SCHEMA, "is_enabled()"
)
async def wireguard_enabled_to_code(config, condition_id, template_arg, args):
var = cg.new_Pvariable(condition_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@automation.register_action(
"wireguard.enable",
WireguardEnableAction,
cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)}),
synchronous=True,
automation.register_apply_action(
"wireguard.enable", WIREGUARD_AUTOMATION_SCHEMA, automation.ApplyCall("enable()")
)
async def wireguard_enable_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@automation.register_action(
"wireguard.disable",
WireguardDisableAction,
cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)}),
synchronous=True,
automation.register_apply_action(
"wireguard.disable", WIREGUARD_AUTOMATION_SCHEMA, automation.ApplyCall("disable()")
)
async def wireguard_disable_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
-26
View File
@@ -4,7 +4,6 @@
#include <ctime>
#include <initializer_list>
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/time/real_time_clock.h"
@@ -164,30 +163,5 @@ static constexpr size_t MASK_KEY_BUFFER_SIZE = 12;
/// Strip most part of the key only for secure printing
void mask_key_to(char *buffer, size_t len, const char *key);
/// Condition to check if remote peer is online.
template<typename... Ts>
class WireguardPeerOnlineCondition final : public Condition<Ts...>, public Parented<Wireguard> {
public:
bool check(const Ts &...x) override { return this->parent_->is_peer_up(); }
};
/// Condition to check if Wireguard component is enabled.
template<typename... Ts> class WireguardEnabledCondition final : public Condition<Ts...>, public Parented<Wireguard> {
public:
bool check(const Ts &...x) override { return this->parent_->is_enabled(); }
};
/// Action to enable Wireguard component.
template<typename... Ts> class WireguardEnableAction final : public Action<Ts...>, public Parented<Wireguard> {
public:
void play(const Ts &...x) override { this->parent_->enable(); }
};
/// Action to disable Wireguard component.
template<typename... Ts> class WireguardDisableAction final : public Action<Ts...>, public Parented<Wireguard> {
public:
void play(const Ts &...x) override { this->parent_->disable(); }
};
} // namespace esphome::wireguard
#endif