mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 02:28:28 +02:00
Select enhancement (#3423)
Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
#include <utility>
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "select_call.h"
|
||||
#include "select_traits.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace select {
|
||||
@@ -17,33 +17,6 @@ namespace select {
|
||||
} \
|
||||
}
|
||||
|
||||
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.
|
||||
@@ -51,18 +24,23 @@ class SelectTraits {
|
||||
class Select : public EntityBase {
|
||||
public:
|
||||
std::string state;
|
||||
SelectTraits traits;
|
||||
|
||||
void publish_state(const std::string &state);
|
||||
|
||||
/// Return whether this select has gotten a full state yet.
|
||||
bool has_state() const { return has_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);
|
||||
// Methods that provide an API to index-based access.
|
||||
size_t size() const;
|
||||
optional<size_t> index_of(const std::string &option) const;
|
||||
optional<size_t> active_index() const;
|
||||
optional<std::string> at(size_t index) const;
|
||||
|
||||
SelectTraits traits;
|
||||
|
||||
/// Return whether this select has gotten a full state yet.
|
||||
bool has_state() const { return has_state_; }
|
||||
void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);
|
||||
|
||||
protected:
|
||||
friend class SelectCall;
|
||||
@@ -77,7 +55,7 @@ class Select : public EntityBase {
|
||||
|
||||
uint32_t hash_base() override;
|
||||
|
||||
CallbackManager<void(std::string)> state_callback_;
|
||||
CallbackManager<void(std::string, size_t)> state_callback_;
|
||||
bool has_state_{false};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user