From 0c548f77fa34484626afab64118f10593047f444 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Sep 2026 20:24:57 +0100 Subject: [PATCH] [bm8563] Use register_apply_action for read_time, write_time and start_timer (#19665) --- esphome/components/bm8563/bm8563.h | 20 ------- esphome/components/bm8563/time.py | 53 +++++-------------- tests/components/bm8563/common-actions.yaml | 7 +++ .../bm8563/test-actions.esp8266-ard.yaml | 4 ++ 4 files changed, 24 insertions(+), 60 deletions(-) create mode 100644 tests/components/bm8563/common-actions.yaml create mode 100644 tests/components/bm8563/test-actions.esp8266-ard.yaml diff --git a/esphome/components/bm8563/bm8563.h b/esphome/components/bm8563/bm8563.h index 5ca9714091..b9b7a2c327 100644 --- a/esphome/components/bm8563/bm8563.h +++ b/esphome/components/bm8563/bm8563.h @@ -34,24 +34,4 @@ class BM8563 final : public time::RealTimeClock, public i2c::I2CDevice { uint8_t byte_to_bcd2_(uint8_t value); }; -template class WriteAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->write_time(); } -}; - -template class ReadAction final : public Action, public Parented { - public: - void play(const Ts &...x) override { this->parent_->read_time(); } -}; - -template class TimerAction final : public Action, public Parented { - public: - TEMPLATABLE_VALUE(uint32_t, duration) - - void play(const Ts &...x) override { - auto duration = this->duration_.value(x...); - this->parent_->start_timer(duration); - } -}; - } // namespace esphome::bm8563 diff --git a/esphome/components/bm8563/time.py b/esphome/components/bm8563/time.py index 3a15d0eb1c..c28976eb87 100644 --- a/esphome/components/bm8563/time.py +++ b/esphome/components/bm8563/time.py @@ -3,8 +3,6 @@ import esphome.codegen as cg from esphome.components import i2c, time import esphome.config_validation as cv from esphome.const import CONF_DURATION, CONF_ID -from esphome.core import ID -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType DEPENDENCIES = ["i2c"] @@ -13,9 +11,6 @@ I2C_ADDR = 0x51 bm8563_ns = cg.esphome_ns.namespace("bm8563") BM8563 = bm8563_ns.class_("BM8563", time.RealTimeClock, i2c.I2CDevice) -WriteAction = bm8563_ns.class_("WriteAction", automation.Action) -ReadAction = bm8563_ns.class_("ReadAction", automation.Action) -TimerAction = bm8563_ns.class_("TimerAction", automation.Action) CONFIG_SCHEMA = ( time.TIME_SCHEMA.extend( @@ -28,51 +23,29 @@ CONFIG_SCHEMA = ( ) -automation.register_parented_action( - "bm8563.write_time", - WriteAction, - automation.maybe_simple_id( - { - cv.GenerateID(): cv.use_id(BM8563), - } - ), - synchronous=True, +BM8563_ACTION_SCHEMA = automation.maybe_simple_id( + { + cv.GenerateID(): cv.use_id(BM8563), + } ) +automation.register_apply_action( + "bm8563.write_time", BM8563_ACTION_SCHEMA, automation.ApplyCall("write_time()") +) -@automation.register_action( +automation.register_apply_action( + "bm8563.read_time", BM8563_ACTION_SCHEMA, automation.ApplyCall("read_time()") +) + +automation.register_apply_action( "bm8563.start_timer", - TimerAction, cv.Schema( { cv.GenerateID(): cv.use_id(BM8563), cv.Required(CONF_DURATION): cv.templatable(cv.positive_time_period_seconds), } ), - synchronous=True, -) -async def bm8563_start_timer_to_code( - config: ConfigType, - action_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - var = cg.new_Pvariable(action_id, template_arg) - await cg.register_parented(var, config[CONF_ID]) - template_ = await cg.templatable(config[CONF_DURATION], args, cg.uint32) - cg.add(var.set_duration(template_)) - return var - - -automation.register_parented_action( - "bm8563.read_time", - ReadAction, - automation.maybe_simple_id( - { - cv.GenerateID(): cv.use_id(BM8563), - } - ), - synchronous=True, + automation.ApplyField(CONF_DURATION, "start_timer", cg.uint32), ) diff --git a/tests/components/bm8563/common-actions.yaml b/tests/components/bm8563/common-actions.yaml new file mode 100644 index 0000000000..c8175e3177 --- /dev/null +++ b/tests/components/bm8563/common-actions.yaml @@ -0,0 +1,7 @@ +button: + - platform: template + name: BM8563 Actions + on_press: + - bm8563.read_time: + - bm8563.start_timer: + duration: !lambda return 60; diff --git a/tests/components/bm8563/test-actions.esp8266-ard.yaml b/tests/components/bm8563/test-actions.esp8266-ard.yaml new file mode 100644 index 0000000000..eaabbc63fe --- /dev/null +++ b/tests/components/bm8563/test-actions.esp8266-ard.yaml @@ -0,0 +1,4 @@ +packages: + i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml + bm8563: !include common.yaml + actions: !include common-actions.yaml