mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[tm1651] Use register_apply_action for the five tm1651 actions (#19647)
This commit is contained in:
@@ -9,8 +9,6 @@ from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_LEVEL,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@mrtoy-me"]
|
||||
@@ -21,11 +19,6 @@ tm1651_ns = cg.esphome_ns.namespace("tm1651")
|
||||
TM1651Brightness = tm1651_ns.enum("TM1651Brightness")
|
||||
TM1651Display = tm1651_ns.class_("TM1651Display", cg.Component)
|
||||
|
||||
SetBrightnessAction = tm1651_ns.class_("SetBrightnessAction", automation.Action)
|
||||
SetLevelAction = tm1651_ns.class_("SetLevelAction", automation.Action)
|
||||
SetLevelPercentAction = tm1651_ns.class_("SetLevelPercentAction", automation.Action)
|
||||
TurnOnAction = tm1651_ns.class_("TurnOnAction", automation.Action)
|
||||
TurnOffAction = tm1651_ns.class_("TurnOffAction", automation.Action)
|
||||
|
||||
TM1651_BRIGHTNESS_OPTIONS = {
|
||||
1: TM1651Brightness.TM1651_DARKEST,
|
||||
@@ -66,92 +59,31 @@ BINARY_OUTPUT_ACTION_SCHEMA = maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"tm1651.set_brightness",
|
||||
SetBrightnessAction,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||
cv.Required(CONF_BRIGHTNESS): cv.templatable(validate_brightness),
|
||||
},
|
||||
key=CONF_BRIGHTNESS,
|
||||
for _name, _key, _validator, _method in (
|
||||
("tm1651.set_brightness", CONF_BRIGHTNESS, validate_brightness, "set_brightness"),
|
||||
("tm1651.set_level", CONF_LEVEL, validate_level, "set_level"),
|
||||
(
|
||||
"tm1651.set_level_percent",
|
||||
CONF_LEVEL_PERCENT,
|
||||
validate_level_percent,
|
||||
"set_level_percent",
|
||||
),
|
||||
synchronous=True,
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||
cv.Required(_key): cv.templatable(_validator),
|
||||
},
|
||||
key=_key,
|
||||
),
|
||||
automation.ApplyField(_key, _method, cg.uint8),
|
||||
)
|
||||
|
||||
automation.register_apply_action(
|
||||
"tm1651.turn_off", BINARY_OUTPUT_ACTION_SCHEMA, automation.ApplyCall("turn_off()")
|
||||
)
|
||||
async def tm1651_set_brightness_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_BRIGHTNESS], args, cg.uint8)
|
||||
cg.add(var.set_brightness(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"tm1651.set_level",
|
||||
SetLevelAction,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||
cv.Required(CONF_LEVEL): cv.templatable(validate_level),
|
||||
},
|
||||
key=CONF_LEVEL,
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
async def tm1651_set_level_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_LEVEL], args, cg.uint8)
|
||||
cg.add(var.set_level(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"tm1651.set_level_percent",
|
||||
SetLevelPercentAction,
|
||||
cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||
cv.Required(CONF_LEVEL_PERCENT): cv.templatable(validate_level_percent),
|
||||
},
|
||||
key=CONF_LEVEL_PERCENT,
|
||||
),
|
||||
synchronous=True,
|
||||
)
|
||||
async def tm1651_set_level_percent_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_LEVEL_PERCENT], args, cg.uint8)
|
||||
cg.add(var.set_level_percent(template_))
|
||||
return var
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"tm1651.turn_off",
|
||||
TurnOffAction,
|
||||
BINARY_OUTPUT_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"tm1651.turn_on",
|
||||
TurnOnAction,
|
||||
BINARY_OUTPUT_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.register_apply_action(
|
||||
"tm1651.turn_on", BINARY_OUTPUT_ACTION_SCHEMA, automation.ApplyCall("turn_on()")
|
||||
)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
@@ -56,44 +55,4 @@ class TM1651Display final : public Component {
|
||||
uint8_t level_{0};
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetBrightnessAction final : public Action<Ts...>, public Parented<TM1651Display> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint8_t, brightness)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto brightness = this->brightness_.value(x...);
|
||||
this->parent_->set_brightness(brightness);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetLevelAction final : public Action<Ts...>, public Parented<TM1651Display> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint8_t, level)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto level = this->level_.value(x...);
|
||||
this->parent_->set_level(level);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetLevelPercentAction final : public Action<Ts...>, public Parented<TM1651Display> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint8_t, level_percent)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto level_percent = this->level_percent_.value(x...);
|
||||
this->parent_->set_level_percent(level_percent);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class TurnOnAction final : public Action<Ts...>, public Parented<TM1651Display> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->turn_on(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class TurnOffAction final : public Action<Ts...>, public Parented<TM1651Display> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->turn_off(); }
|
||||
};
|
||||
|
||||
} // namespace esphome::tm1651
|
||||
|
||||
Reference in New Issue
Block a user