From b3400a1308e90c4e588b130ed983a534ec308500 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Fri, 9 May 2025 18:19:03 +1200 Subject: [PATCH] [lock] Tidy up template publish action and lockstate locations (#8729) --- esphome/components/lock/__init__.py | 14 +++++++- esphome/components/lock/automation.h | 15 ++------ esphome/components/template/lock/__init__.py | 35 ++++++++----------- esphome/components/template/lock/automation.h | 18 ++++++++++ tests/components/lock/common.yaml | 4 +-- 5 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 esphome/components/template/lock/automation.h diff --git a/esphome/components/lock/__init__.py b/esphome/components/lock/__init__.py index 6925861b52..764bd91f76 100644 --- a/esphome/components/lock/__init__.py +++ b/esphome/components/lock/__init__.py @@ -31,6 +31,18 @@ LockCondition = lock_ns.class_("LockCondition", Condition) LockLockTrigger = lock_ns.class_("LockLockTrigger", automation.Trigger.template()) LockUnlockTrigger = lock_ns.class_("LockUnlockTrigger", automation.Trigger.template()) +LockState = lock_ns.enum("LockState") + +LOCK_STATES = { + "LOCKED": LockState.LOCK_STATE_LOCKED, + "UNLOCKED": LockState.LOCK_STATE_UNLOCKED, + "JAMMED": LockState.LOCK_STATE_JAMMED, + "LOCKING": LockState.LOCK_STATE_LOCKING, + "UNLOCKING": LockState.LOCK_STATE_UNLOCKING, +} + +validate_lock_state = cv.enum(LOCK_STATES, upper=True) + LOCK_SCHEMA = ( cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA) .extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA) @@ -79,7 +91,7 @@ async def register_lock(var, config): LOCK_ACTION_SCHEMA = maybe_simple_id( { - cv.Required(CONF_ID): cv.use_id(Lock), + cv.GenerateID(CONF_ID): cv.use_id(Lock), } ) diff --git a/esphome/components/lock/automation.h b/esphome/components/lock/automation.h index 74cfbe2ef6..8cb3b64ffe 100644 --- a/esphome/components/lock/automation.h +++ b/esphome/components/lock/automation.h @@ -1,8 +1,8 @@ #pragma once -#include "esphome/core/component.h" -#include "esphome/core/automation.h" #include "esphome/components/lock/lock.h" +#include "esphome/core/automation.h" +#include "esphome/core/component.h" namespace esphome { namespace lock { @@ -72,16 +72,5 @@ class LockUnlockTrigger : public Trigger<> { } }; -template class LockPublishAction : public Action { - public: - LockPublishAction(Lock *a_lock) : lock_(a_lock) {} - TEMPLATABLE_VALUE(LockState, state) - - void play(Ts... x) override { this->lock_->publish_state(this->state_.value(x...)); } - - protected: - Lock *lock_; -}; - } // namespace lock } // namespace esphome diff --git a/esphome/components/template/lock/__init__.py b/esphome/components/template/lock/__init__.py index 2dcb90e038..43a633aedf 100644 --- a/esphome/components/template/lock/__init__.py +++ b/esphome/components/template/lock/__init__.py @@ -17,17 +17,11 @@ from .. import template_ns TemplateLock = template_ns.class_("TemplateLock", lock.Lock, cg.Component) -LockState = lock.lock_ns.enum("LockState") - -LOCK_STATES = { - "LOCKED": LockState.LOCK_STATE_LOCKED, - "UNLOCKED": LockState.LOCK_STATE_UNLOCKED, - "JAMMED": LockState.LOCK_STATE_JAMMED, - "LOCKING": LockState.LOCK_STATE_LOCKING, - "UNLOCKING": LockState.LOCK_STATE_UNLOCKING, -} - -validate_lock_state = cv.enum(LOCK_STATES, upper=True) +TemplateLockPublishAction = template_ns.class_( + "TemplateLockPublishAction", + automation.Action, + cg.Parented.template(TemplateLock), +) def validate(config): @@ -66,7 +60,7 @@ async def to_code(config): if CONF_LAMBDA in config: template_ = await cg.process_lambda( - config[CONF_LAMBDA], [], return_type=cg.optional.template(LockState) + config[CONF_LAMBDA], [], return_type=cg.optional.template(lock.LockState) ) cg.add(var.set_state_lambda(template_)) if CONF_UNLOCK_ACTION in config: @@ -88,17 +82,18 @@ async def to_code(config): @automation.register_action( "lock.template.publish", - lock.LockPublishAction, - cv.Schema( + TemplateLockPublishAction, + cv.maybe_simple_value( { - cv.Required(CONF_ID): cv.use_id(lock.Lock), - cv.Required(CONF_STATE): cv.templatable(validate_lock_state), - } + cv.GenerateID(): cv.use_id(TemplateLock), + cv.Required(CONF_STATE): cv.templatable(lock.validate_lock_state), + }, + key=CONF_STATE, ), ) async def lock_template_publish_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - template_ = await cg.templatable(config[CONF_STATE], args, LockState) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + template_ = await cg.templatable(config[CONF_STATE], args, lock.LockState) cg.add(var.set_state(template_)) return var diff --git a/esphome/components/template/lock/automation.h b/esphome/components/template/lock/automation.h new file mode 100644 index 0000000000..6124546592 --- /dev/null +++ b/esphome/components/template/lock/automation.h @@ -0,0 +1,18 @@ +#pragma once + +#include "template_lock.h" + +#include "esphome/core/automation.h" + +namespace esphome { +namespace template_ { + +template class TemplateLockPublishAction : public Action, public Parented { + public: + TEMPLATABLE_VALUE(lock::LockState, state) + + void play(Ts... x) override { this->parent_->publish_state(this->state_.value(x...)); } +}; + +} // namespace template_ +} // namespace esphome diff --git a/tests/components/lock/common.yaml b/tests/components/lock/common.yaml index 82297a3da4..67da46653c 100644 --- a/tests/components/lock/common.yaml +++ b/tests/components/lock/common.yaml @@ -27,9 +27,7 @@ lock: id: test_lock1 state: !lambda "return LOCK_STATE_UNLOCKED;" on_lock: - - lock.template.publish: - id: test_lock1 - state: !lambda "return LOCK_STATE_LOCKED;" + - lock.template.publish: LOCKED - platform: output name: Generic Output Lock id: test_lock2