mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-03 19:38:30 +02:00
Actions to enable and disable WireGuard connection (#5690)
This commit is contained in:
@@ -10,6 +10,7 @@ from esphome.const import (
|
||||
)
|
||||
from esphome.components import time
|
||||
from esphome.core import TimePeriod
|
||||
from esphome import automation
|
||||
|
||||
CONF_NETMASK = "netmask"
|
||||
CONF_PRIVATE_KEY = "private_key"
|
||||
@@ -30,6 +31,16 @@ _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)
|
||||
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):
|
||||
@@ -112,3 +123,47 @@ async def to_code(config):
|
||||
cg.add_library("droscy/esp_wireguard", "0.3.2")
|
||||
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"wireguard.peer_online",
|
||||
WireguardPeerOnlineCondition,
|
||||
cv.Schema({cv.GenerateID(): cv.use_id(Wireguard)}),
|
||||
)
|
||||
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)}),
|
||||
)
|
||||
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)}),
|
||||
)
|
||||
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)}),
|
||||
)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user