esphome/esphome/components/select/select.h
Paul Monigatti 471b82f727
EntityBase Refactor (#2418)
* Renamed Nameable to EntityBase (cpp)

* Renamed NAMEABLE_SCHEMA to ENTITY_BASE_SCHEMA (Python)

* Renamed cg.Nameable to cg.EntityBase (Python)

* Remove redundant use of CONF_NAME from esp32_touch

* Remove redundant use of CONF_NAME from mcp3008

* Updated test

* Moved EntityBase from Component.h and Component.cpp

* Added icon property to EntityBase

* Added CONF_ICON to ENTITY_BASE_SCHEMA and added setup_entity function to cpp_helpers

* Added MQTT component getters for icon and disabled_by_default

* Lint

* Removed icon field from MQTT components

* Code generation now uses setup_entity to setENTITY_BASE_SCHEMA fields

* Removed unused import

* Added cstdint include

* Optimisation: don't set icon if it is empty

* Remove icon from NumberTraits and SelectTraits

* Removed unused import

* Integration and Total Daily Energy sensors now inherit icons from their parents during code generation

* Minor comment correction

* Removed redundant icon-handling code from sensor, switch, and text_sensor

* Update esphome/components/tsl2591/tsl2591.h

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>

* Added icon property to binary sensor, climate, cover, and fan component tests

* Added icons for Binary Sensor, Climate, Cover, Fan, and Light  to API

* Consolidated EntityBase fields in MQTT components

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
2021-10-10 10:37:05 +02:00

85 lines
2.1 KiB
C++

#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); }
const 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_; }
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