[snapshot] Use register_apply_action for snapshot.take (#19696)

This commit is contained in:
J. Nick Koston
2026-09-25 20:33:21 -04:00
committed by GitHub
parent c8d3f43712
commit d71a6e662b
3 changed files with 14 additions and 40 deletions
+5 -18
View File
@@ -12,9 +12,9 @@ from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import CORE, ID
from esphome.core import CORE
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@clydebarrow"]
@@ -24,31 +24,18 @@ CONF_FILENAME = "filename"
snapshot_ns = cg.esphome_ns.namespace("snapshot")
Snapshot = snapshot_ns.class_("Snapshot")
SnapshotAction = snapshot_ns.class_("SnapshotAction", automation.Action)
@automation.register_action(
automation.register_apply_action(
"snapshot.take",
SnapshotAction,
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(Snapshot),
cv.Optional(CONF_FILENAME): cv.templatable(cv.string),
cv.Optional(CONF_FILENAME, default=""): cv.templatable(cv.string),
}
),
synchronous=True,
automation.ApplyField(CONF_FILENAME, "take_snapshot_or_log", cg.std_string),
)
async def snapshot_take_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])
if (filename := config.get(CONF_FILENAME)) is not None:
cg.add(var.set_filename(await cg.templatable(filename, args, cg.std_string)))
return var
@dataclass
+5 -2
View File
@@ -192,8 +192,11 @@ bool write_snapshot_file(const uint8_t *pixels, int width, int height, size_t ro
} // namespace
// helper function since ESP_LOGW is disallowed in a header file
void Snapshot::log_action_failed() { ESP_LOGW(TAG, "snapshot.take did not write a file"); }
void Snapshot::take_snapshot_or_log(const char *filename) {
if (!this->take_snapshot(filename[0] != '\0' ? filename : nullptr)) {
ESP_LOGW(TAG, "snapshot.take did not write a file");
}
}
bool Snapshot::take_snapshot(const char *filename) {
const int width = this->snapshot_width();
+4 -20
View File
@@ -1,7 +1,6 @@
#pragma once
#ifdef USE_HOST
#include "esphome/core/automation.h"
#include <cstddef>
#include <cstdint>
@@ -34,8 +33,10 @@ class Snapshot {
/// already there is never written over. Returns true if a file was written.
bool take_snapshot(const char *filename);
/// Log that an action-triggered snapshot did not write a file.
static void log_action_failed();
/// Take a snapshot for an automation: an empty name means a generated one, and a snapshot
/// that wrote nothing is logged.
void take_snapshot_or_log(const char *filename);
void take_snapshot_or_log(const std::string &filename) { this->take_snapshot_or_log(filename.c_str()); }
protected:
/// Width of the picture in pixels.
@@ -50,23 +51,6 @@ class Snapshot {
const char *snapshot_prefix_{"snapshot"};
};
template<typename... Ts> class SnapshotAction final : public Action<Ts...>, public Parented<Snapshot> {
public:
TEMPLATABLE_VALUE(std::string, filename)
protected:
void play(const Ts &...x) override {
bool ok;
if (this->filename_.has_value()) {
ok = this->parent_->take_snapshot(this->filename_.value(x...).c_str());
} else {
ok = this->parent_->take_snapshot(nullptr);
}
if (!ok)
this->parent_->log_action_failed();
}
};
} // namespace esphome::snapshot
#endif