diff --git a/esphome/components/store_yaml/__init__.py b/esphome/components/store_yaml/__init__.py new file mode 100644 index 0000000000..c65f7bdf7e --- /dev/null +++ b/esphome/components/store_yaml/__init__.py @@ -0,0 +1,45 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome import automation, yaml_util +from esphome.core import CORE + +from esphome.const import ( + CONF_ID, +) + +CODEOWNERS = ["@gabest11"] +CONF_SHOW_SECRETS = "show_secrets" + +store_yaml_ns = cg.esphome_ns.namespace("store_yaml") +StoreYamlComponent = store_yaml_ns.class_("StoreYamlComponent", cg.Component) + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(StoreYamlComponent), + cv.Optional(CONF_SHOW_SECRETS, default=False): cv.boolean, + } +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + store_yaml = await cg.register_component(var, config) + 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)) + + +DumpAction = store_yaml_ns.class_("DumpAction", automation.Action) + +DUMP_ACTION_SCHEMA = automation.maybe_simple_id( + {cv.GenerateID(): cv.use_id(StoreYamlComponent)} +) + + +@automation.register_action( + "store_yaml.dump", DumpAction, DUMP_ACTION_SCHEMA +) +async def dump_yaml_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 new file mode 100644 index 0000000000..75108ebee6 --- /dev/null +++ b/esphome/components/store_yaml/store_yaml.cpp @@ -0,0 +1,30 @@ +#include "store_yaml.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace store_yaml { + +static const char *const TAG = "store_yaml"; + +void StoreYamlComponent::set(const std::string &yaml) { this->yaml_ = yaml; } + +std::string StoreYamlComponent::get() const { return this->yaml_; } + +void StoreYamlComponent::dump() const { + const char *s = this->yaml_.c_str(); + while (*s) { + const char *e = s; + while (*e && *e++ != '\n') + ; + const char *tmp = e; + if (e > s) { + if (e[-1] == '\n') + e--; + ESP_LOGI(TAG, "%s", std::string(s, e - s).c_str()); + } + s = tmp; + } +} + +} // namespace store_yaml +} // namespace esphome diff --git a/esphome/components/store_yaml/store_yaml.h b/esphome/components/store_yaml/store_yaml.h new file mode 100644 index 0000000000..f8f23b5aee --- /dev/null +++ b/esphome/components/store_yaml/store_yaml.h @@ -0,0 +1,23 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" + +namespace esphome { +namespace store_yaml { +class StoreYamlComponent : public Component { + std::string yaml_; + + public: + void set(const std::string &yaml); + std::string get() const; + void dump() const; +}; + +template class DumpAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->dump(); } +}; + +} // namespace store_yaml +} // namespace esphome