diff options
-rw-r--r-- | components/select/__init__.py | 98 | ||||
-rw-r--r-- | components/select/automation.h | 33 | ||||
-rw-r--r-- | components/select/select.cpp | 43 | ||||
-rw-r--r-- | components/select/select.h | 93 |
4 files changed, 0 insertions, 267 deletions
diff --git a/components/select/__init__.py b/components/select/__init__.py deleted file mode 100644 index f0888a7..0000000 --- a/components/select/__init__.py +++ /dev/null @@ -1,98 +0,0 @@ -from typing import List -import esphome.codegen as cg -import esphome.config_validation as cv -from esphome import automation -from esphome.components import mqtt -from esphome.const import ( - CONF_ID, - CONF_ON_VALUE, - CONF_OPTION, - CONF_TRIGGER_ID, - CONF_MQTT_ID, -) -from esphome.core import CORE, coroutine_with_priority -from esphome.cpp_helpers import setup_entity - -CODEOWNERS = ["@esphome/core"] -IS_PLATFORM_COMPONENT = True - -select_ns = cg.esphome_ns.namespace("select") -Select = select_ns.class_("Select", cg.EntityBase) -SelectPtr = Select.operator("ptr") - -# Triggers -SelectStateTrigger = select_ns.class_( - "SelectStateTrigger", automation.Trigger.template(cg.float_) -) - -# Actions -SelectSetAction = select_ns.class_("SelectSetAction", automation.Action) - -icon = cv.icon - -SELECT_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend( - { - cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTSelectComponent), - cv.GenerateID(): cv.declare_id(Select), - cv.Optional(CONF_ON_VALUE): automation.validate_automation( - { - cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SelectStateTrigger), - } - ), - cv.Optional("forced_hash"): cv.int_, - } -) - - -async def setup_select_core_(var, config, *, options: List[str]): - await setup_entity(var, config) - - cg.add(var.traits.set_options(options)) - - if "forced_hash" in config: - cg.add(var.set_forced_hash(config["forced_hash"])) - - for conf in config.get(CONF_ON_VALUE, []): - trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - await automation.build_automation(trigger, [(cg.std_string, "x")], conf) - - if CONF_MQTT_ID in config: - mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) - await mqtt.register_mqtt_component(mqtt_, config) - - -async def register_select(var, config, *, options: List[str]): - if not CORE.has_id(config[CONF_ID]): - var = cg.Pvariable(config[CONF_ID], var) - cg.add(cg.App.register_select(var)) - await setup_select_core_(var, config, options=options) - - -async def new_select(config, *, options: List[str]): - var = cg.new_Pvariable(config[CONF_ID]) - await register_select(var, config, options=options) - return var - - -@coroutine_with_priority(40.0) -async def to_code(config): - cg.add_define("USE_SELECT") - cg.add_global(select_ns.using) - - [email protected]_action( - "select.set", - SelectSetAction, - cv.Schema( - { - cv.Required(CONF_ID): cv.use_id(Select), - cv.Required(CONF_OPTION): cv.templatable(cv.string_strict), - } - ), -) -async def select_set_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - template_ = await cg.templatable(config[CONF_OPTION], args, cg.std_string) - cg.add(var.set_option(template_)) - return var diff --git a/components/select/automation.h b/components/select/automation.h deleted file mode 100644 index 1e0bfed..0000000 --- a/components/select/automation.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "esphome/core/automation.h" -#include "esphome/core/component.h" -#include "select.h" - -namespace esphome { -namespace select { - -class SelectStateTrigger : public Trigger<std::string> { - public: - explicit SelectStateTrigger(Select *parent) { - parent->add_on_state_callback([this](const std::string &value) { this->trigger(value); }); - } -}; - -template<typename... Ts> class SelectSetAction : public Action<Ts...> { - public: - SelectSetAction(Select *select) : select_(select) {} - TEMPLATABLE_VALUE(std::string, option) - - void play(Ts... x) override { - auto call = this->select_->make_call(); - call.set_option(this->option_.value(x...)); - call.perform(); - } - - protected: - Select *select_; -}; - -} // namespace select -} // namespace esphome diff --git a/components/select/select.cpp b/components/select/select.cpp deleted file mode 100644 index 14f4d92..0000000 --- a/components/select/select.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "select.h" -#include "esphome/core/log.h" - -namespace esphome { -namespace select { - -static const char *const TAG = "select"; - -void SelectCall::perform() { - ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str()); - if (!this->option_.has_value()) { - ESP_LOGW(TAG, "No value set for SelectCall"); - return; - } - - const auto &traits = this->parent_->traits; - auto value = *this->option_; - auto options = traits.get_options(); - - if (std::find(options.begin(), options.end(), value) == options.end()) { - ESP_LOGW(TAG, " Option %s is not a valid option.", value.c_str()); - return; - } - - ESP_LOGD(TAG, " Option: %s", (*this->option_).c_str()); - this->parent_->control(*this->option_); -} - -void Select::publish_state(const std::string &state) { - this->has_state_ = true; - this->state = state; - ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), state.c_str()); - this->state_callback_.call(state); -} - -void Select::add_on_state_callback(std::function<void(std::string)> &&callback) { - this->state_callback_.add(std::move(callback)); -} - -uint32_t Select::hash_base() { return 2812997003UL; } - -} // namespace select -} // namespace esphome diff --git a/components/select/select.h b/components/select/select.h deleted file mode 100644 index 0d1c4f8..0000000 --- a/components/select/select.h +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#include <set> -#include <utility> -#include "esphome/core/component.h" -#include "esphome/core/entity_base.h" -#include "esphome/core/helpers.h" - -namespace esphome { -namespace select { - -#define LOG_SELECT(prefix, type, obj) \ - if ((obj) != nullptr) { \ - ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \ - if (!(obj)->get_icon().empty()) { \ - ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \ - } \ - } - -class Select; - -class SelectCall { - public: - explicit SelectCall(Select *parent) : parent_(parent) {} - void perform(); - - SelectCall &set_option(const std::string &option) { - option_ = option; - return *this; - } - const optional<std::string> &get_option() const { return option_; } - - protected: - Select *const parent_; - optional<std::string> option_; -}; - -class SelectTraits { - public: - void set_options(std::vector<std::string> options) { this->options_ = std::move(options); } - std::vector<std::string> get_options() const { return this->options_; } - - protected: - std::vector<std::string> options_; -}; - -/** Base-class for all selects. - * - * A select can use publish_state to send out a new value. - */ -class Select : public EntityBase { - public: - std::string state; - - void publish_state(const std::string &state); - - SelectCall make_call() { return SelectCall(this); } - void set(const std::string &value) { make_call().set_option(value).perform(); } - - void add_on_state_callback(std::function<void(std::string)> &&callback); - - SelectTraits traits; - - /// Return whether this select has gotten a full state yet. - bool has_state() const { return has_state_; } - - - bool has_forced_hash = false; - uint32_t forced_hash = 0; - void set_forced_hash(uint32_t hash_value) { - forced_hash = hash_value; - has_forced_hash = true; - } - - protected: - friend class SelectCall; - - /** Set the value of the select, this is a virtual method that each select integration must implement. - * - * This method is called by the SelectCall. - * - * @param value The value as validated by the SelectCall. - */ - virtual void control(const std::string &value) = 0; - - uint32_t hash_base() override; - - CallbackManager<void(std::string)> state_callback_; - bool has_state_{false}; -}; - -} // namespace select -} // namespace esphome |