From a8ab035b59c1594f59c80f04f33fbf1eae4c1be5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Oct 2026 16:33:05 -1000 Subject: [PATCH] [lock] Filter on_lock and on_unlock with when (#20858) --- esphome/components/lock/__init__.py | 10 ++++++---- esphome/components/lock/automation.h | 22 ---------------------- 2 files changed, 6 insertions(+), 26 deletions(-) delete mode 100644 esphome/components/lock/automation.h diff --git a/esphome/components/lock/__init__.py b/esphome/components/lock/__init__.py index fb9a79f81fe..af12fc61b28 100644 --- a/esphome/components/lock/__init__.py +++ b/esphome/components/lock/__init__.py @@ -32,8 +32,6 @@ LockCall = lock_ns.class_("LockCall") LockPublishAction = lock_ns.class_("LockPublishAction", automation.Action) -LockStateForwarder = lock_ns.class_("LockStateForwarder") - LockState = lock_ns.enum("LockState") LOCK_STATES = LockState.options( @@ -90,12 +88,16 @@ _CALLBACK_AUTOMATIONS = ( automation.CallbackAutomation( CONF_ON_LOCK, "add_on_state_callback", - forwarder=LockStateForwarder.template(LockState.LOCK_STATE_LOCKED), + params=[(LockState, "state")], + forward=[], + when="state == lock::LOCK_STATE_LOCKED", ), automation.CallbackAutomation( CONF_ON_UNLOCK, "add_on_state_callback", - forwarder=LockStateForwarder.template(LockState.LOCK_STATE_UNLOCKED), + params=[(LockState, "state")], + forward=[], + when="state == lock::LOCK_STATE_UNLOCKED", ), ) diff --git a/esphome/components/lock/automation.h b/esphome/components/lock/automation.h deleted file mode 100644 index e578a056439..00000000000 --- a/esphome/components/lock/automation.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "esphome/components/lock/lock.h" -#include "esphome/core/automation.h" -#include "esphome/core/component.h" - -namespace esphome::lock { - -/// Callback forwarder that triggers an Automation<> only when a specific lock state is entered. -/// Pointer-sized (single Automation* field) to fit inline in Callback::ctx_. -template struct LockStateForwarder { - Automation<> *automation; - void operator()(LockState state) const { - if (state == State) - this->automation->trigger(); - } -}; - -static_assert(sizeof(LockStateForwarder) <= sizeof(void *)); -static_assert(std::is_trivially_copyable_v>); - -} // namespace esphome::lock