[it8951] Use register_apply_action for it8951.update (#19691)

This commit is contained in:
J. Nick Koston
2026-09-25 21:10:10 -04:00
committed by GitHub
parent a61746045b
commit 2dc7a64e52
4 changed files with 9 additions and 43 deletions
+5 -19
View File
@@ -36,8 +36,7 @@ from esphome.const import (
CONF_UPDATE_INTERVAL, CONF_UPDATE_INTERVAL,
CONF_WIDTH, CONF_WIDTH,
) )
from esphome.core import ID from esphome.cpp_generator import RawExpression
from esphome.cpp_generator import MockObj, RawExpression, TemplateArgsType
from esphome.final_validate import full_config from esphome.final_validate import full_config
from esphome.types import ConfigType from esphome.types import ConfigType
@@ -61,7 +60,6 @@ VCOM_REGISTER_OPTIONS = (VCOM_REGISTER_DEFAULT, VCOM_REGISTER_ALT)
it8951_ns = cg.esphome_ns.namespace("it8951") it8951_ns = cg.esphome_ns.namespace("it8951")
IT8951Display = it8951_ns.class_("IT8951Display", display.Display, spi.SPIDevice) IT8951Display = it8951_ns.class_("IT8951Display", display.Display, spi.SPIDevice)
IT8951UpdateAction = it8951_ns.class_("IT8951UpdateAction", automation.Action)
# Hardware waveform modes exposed to YAML. Strings are mapped to the C++ # Hardware waveform modes exposed to YAML. Strings are mapped to the C++
# UpdateMode enum so the runtime can store the mode as a uint16_t rather # UpdateMode enum so the runtime can store the mode as a uint16_t rather
@@ -80,6 +78,7 @@ UPDATE_MODE_OPTIONS = {
"A2": UpdateMode.UPDATE_MODE_A2, "A2": UpdateMode.UPDATE_MODE_A2,
"FAST": UpdateMode.UPDATE_MODE_DU, "FAST": UpdateMode.UPDATE_MODE_DU,
"FULL": UpdateMode.UPDATE_MODE_GC16, "FULL": UpdateMode.UPDATE_MODE_GC16,
"DEFAULT": UpdateMode.UPDATE_MODE_NONE,
} }
# Maps the YAML mode string directly to the C++ UpdateMode enum value, so the # Maps the YAML mode string directly to the C++ UpdateMode enum value, so the
# config option and the it8951.update action share one validator. # config option and the it8951.update action share one validator.
@@ -419,26 +418,13 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_transform(RawExpression(str(transform_value)))) cg.add(var.set_transform(RawExpression(str(transform_value))))
@automation.register_action( automation.register_apply_action(
"it8951.update", "it8951.update",
IT8951UpdateAction,
automation.maybe_simple_id( automation.maybe_simple_id(
{ {
cv.Required(CONF_ID): cv.use_id(IT8951Display), cv.Required(CONF_ID): cv.use_id(IT8951Display),
cv.Optional(CONF_MODE): cv.templatable(update_mode), cv.Optional(CONF_MODE, default="DEFAULT"): cv.templatable(update_mode),
} }
), ),
synchronous=True, automation.ApplyField(CONF_MODE, "update_mode", UpdateMode),
) )
async def it8951_update_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
display_var = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, display_var)
if mode := config.get(CONF_MODE):
mode = await cg.templatable(mode, args, UpdateMode)
cg.add(var.set_mode(mode))
return var
+3 -3
View File
@@ -777,12 +777,12 @@ void IT8951Display::update() {
} }
void IT8951Display::update_mode(UpdateMode mode) { void IT8951Display::update_mode(UpdateMode mode) {
if (!this->is_ready())
return;
if (mode == UPDATE_MODE_NONE) { if (mode == UPDATE_MODE_NONE) {
ESP_LOGW(TAG, "Unknown update mode"); this->update();
return; return;
} }
if (!this->is_ready())
return;
this->start_update_(mode); this->start_update_(mode);
} }
-21
View File
@@ -6,7 +6,6 @@
#include "esphome/components/display/display.h" #include "esphome/components/display/display.h"
#include "esphome/components/spi/spi.h" #include "esphome/components/spi/spi.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
@@ -350,24 +349,4 @@ class IT8951Display : public Display,
uint8_t dev_info_attempts_{0}; uint8_t dev_info_attempts_{0};
}; };
// --- Automation action ---
template<typename... Ts> class IT8951UpdateAction : public Action<Ts...> {
public:
explicit IT8951UpdateAction(IT8951Display *display) : display_(display) {}
TEMPLATABLE_VALUE(UpdateMode, mode)
protected:
void play(const Ts &...x) override {
if (!this->display_->is_ready())
return;
if (this->mode_.has_value()) {
this->display_->update_mode(this->mode_.value(x...));
} else {
this->display_->update();
}
}
IT8951Display *display_;
};
} // namespace esphome::it8951 } // namespace esphome::it8951
@@ -59,6 +59,7 @@ display:
- platform: it8951 - platform: it8951
spi_id: spi_bus spi_id: spi_bus
model: seeed-reterminal-e1003 model: seeed-reterminal-e1003
update_mode: DEFAULT
cs_pin: cs_pin:
allow_other_uses: true allow_other_uses: true
number: GPIO5 number: GPIO5