From 78e620aae6154ff00ad757824fe6d161a89975b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Poczkodi?= Date: Wed, 6 Nov 2024 19:51:06 +0100 Subject: [PATCH] option to show in dump_config --- esphome/components/store_yaml/__init__.py | 13 ++++++++----- esphome/components/store_yaml/store_yaml.cpp | 18 ++++++++++++++---- esphome/components/store_yaml/store_yaml.h | 13 ++++++++----- tests/components/store_yaml/common.yaml | 10 +++++----- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/esphome/components/store_yaml/__init__.py b/esphome/components/store_yaml/__init__.py index c65f7bdf7e..2b30a8d807 100644 --- a/esphome/components/store_yaml/__init__.py +++ b/esphome/components/store_yaml/__init__.py @@ -8,6 +8,7 @@ from esphome.const import ( ) CODEOWNERS = ["@gabest11"] +CONF_SHOW_IN_LOGCONFIG = "show_in_logconfig" CONF_SHOW_SECRETS = "show_secrets" store_yaml_ns = cg.esphome_ns.namespace("store_yaml") @@ -16,6 +17,7 @@ StoreYamlComponent = store_yaml_ns.class_("StoreYamlComponent", cg.Component) CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(StoreYamlComponent), + cv.Optional(CONF_SHOW_IN_LOGCONFIG, default=True): cv.boolean, cv.Optional(CONF_SHOW_SECRETS, default=False): cv.boolean, } ) @@ -24,22 +26,23 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) store_yaml = await cg.register_component(var, config) + cg.add(store_yaml.set_show_in_logconfig(config[CONF_SHOW_IN_LOGCONFIG])) yaml = yaml_util.load_yaml(CORE.config_path) dump = yaml_util.dump(yaml, show_secrets=config[CONF_SHOW_SECRETS]) - cg.add(store_yaml.set(dump)) + cg.add(store_yaml.set_yaml(dump)) -DumpAction = store_yaml_ns.class_("DumpAction", automation.Action) +LogAction = store_yaml_ns.class_("LogAction", automation.Action) -DUMP_ACTION_SCHEMA = automation.maybe_simple_id( +LOG_ACTION_SCHEMA = automation.maybe_simple_id( {cv.GenerateID(): cv.use_id(StoreYamlComponent)} ) @automation.register_action( - "store_yaml.dump", DumpAction, DUMP_ACTION_SCHEMA + "store_yaml.log", LogAction, LOG_ACTION_SCHEMA ) -async def dump_yaml_action_to_code(config, action_id, template_arg, args): +async def log_action_to_code(config, action_id, template_arg, args): var = cg.new_Pvariable(action_id, template_arg) await cg.register_parented(var, config[CONF_ID]) return var diff --git a/esphome/components/store_yaml/store_yaml.cpp b/esphome/components/store_yaml/store_yaml.cpp index 75108ebee6..5d2ebd5afe 100644 --- a/esphome/components/store_yaml/store_yaml.cpp +++ b/esphome/components/store_yaml/store_yaml.cpp @@ -6,11 +6,16 @@ namespace store_yaml { static const char *const TAG = "store_yaml"; -void StoreYamlComponent::set(const std::string &yaml) { this->yaml_ = yaml; } +void StoreYamlComponent::dump_config() { + if (this->show_) { + ESP_LOGCONFIG(TAG, "YAML:"); + this->log(true); + } +} -std::string StoreYamlComponent::get() const { return this->yaml_; } +std::string StoreYamlComponent::get_yaml() const { return this->yaml_; } -void StoreYamlComponent::dump() const { +void StoreYamlComponent::log(bool dump_config) const { const char *s = this->yaml_.c_str(); while (*s) { const char *e = s; @@ -20,7 +25,12 @@ void StoreYamlComponent::dump() const { if (e > s) { if (e[-1] == '\n') e--; - ESP_LOGI(TAG, "%s", std::string(s, e - s).c_str()); + std::string row = std::string(s, e - s); + if (dump_config) { + ESP_LOGCONFIG(TAG, "%s", row.c_str()); + } else { + ESP_LOGI(TAG, "%s", row.c_str()); + } } s = tmp; } diff --git a/esphome/components/store_yaml/store_yaml.h b/esphome/components/store_yaml/store_yaml.h index f8f23b5aee..c437f28b01 100644 --- a/esphome/components/store_yaml/store_yaml.h +++ b/esphome/components/store_yaml/store_yaml.h @@ -6,17 +6,20 @@ namespace esphome { namespace store_yaml { class StoreYamlComponent : public Component { + bool show_{false}; std::string yaml_; public: - void set(const std::string &yaml); - std::string get() const; - void dump() const; + void dump_config() override; + void set_show_in_logconfig(bool show) { this->show_ = show; } + void set_yaml(const std::string &yaml) { this->yaml_ = yaml; } + std::string get_yaml() const; + void log(bool dump_config = false) const; }; -template class DumpAction : public Action, public Parented { +template class LogAction : public Action, public Parented { public: - void play(Ts... x) override { this->parent_->dump(); } + void play(Ts... x) override { this->parent_->log(); } }; } // namespace store_yaml diff --git a/tests/components/store_yaml/common.yaml b/tests/components/store_yaml/common.yaml index 1a79f66c2d..3083f008c0 100644 --- a/tests/components/store_yaml/common.yaml +++ b/tests/components/store_yaml/common.yaml @@ -1,9 +1,9 @@ api: services: - - service: 'store_yaml_dump' + - service: 'store_yaml_log' then: - - store_yaml.dump - -store_yaml: - show_secrets: True + - store_yaml.log +store_yaml: + show_in_logconfig: True + show_secrets: True