mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 12:30:28 +02:00
Number and Template Number updates (#2036)
Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
@@ -9,8 +9,8 @@ namespace number {
|
||||
#define LOG_NUMBER(prefix, type, obj) \
|
||||
if ((obj) != nullptr) { \
|
||||
ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, type, (obj)->get_name().c_str()); \
|
||||
if (!(obj)->get_icon().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
|
||||
if (!(obj)->traits.get_icon().empty()) { \
|
||||
ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->traits.get_icon().c_str()); \
|
||||
} \
|
||||
}
|
||||
|
||||
@@ -19,72 +19,56 @@ class Number;
|
||||
class NumberCall {
|
||||
public:
|
||||
explicit NumberCall(Number *parent) : parent_(parent) {}
|
||||
NumberCall &set_value(float value);
|
||||
void perform();
|
||||
|
||||
const optional<float> &get_value() const;
|
||||
NumberCall &set_value(float value) {
|
||||
value_ = value;
|
||||
return *this;
|
||||
}
|
||||
const optional<float> &get_value() const { return value_; }
|
||||
|
||||
protected:
|
||||
Number *const parent_;
|
||||
optional<float> value_;
|
||||
};
|
||||
|
||||
class NumberTraits {
|
||||
public:
|
||||
void set_min_value(float min_value) { min_value_ = min_value; }
|
||||
float get_min_value() const { return min_value_; }
|
||||
void set_max_value(float max_value) { max_value_ = max_value; }
|
||||
float get_max_value() const { return max_value_; }
|
||||
void set_step(float step) { step_ = step; }
|
||||
float get_step() const { return step_; }
|
||||
void set_icon(std::string icon) { icon_ = std::move(icon); }
|
||||
const std::string &get_icon() const { return icon_; }
|
||||
|
||||
protected:
|
||||
float min_value_ = NAN;
|
||||
float max_value_ = NAN;
|
||||
float step_ = NAN;
|
||||
std::string icon_;
|
||||
};
|
||||
|
||||
/** Base-class for all numbers.
|
||||
*
|
||||
* A number can use publish_state to send out a new value.
|
||||
*/
|
||||
class Number : public Nameable {
|
||||
public:
|
||||
explicit Number();
|
||||
explicit Number(const std::string &name);
|
||||
|
||||
/** Manually set the icon of this number. By default the number's default defined by icon() is used.
|
||||
*
|
||||
* @param icon The icon, for example "mdi:flash". "" to disable.
|
||||
*/
|
||||
void set_icon(const std::string &icon);
|
||||
/// Get the Home Assistant Icon. Uses the manual override if specified or the default value instead.
|
||||
std::string get_icon();
|
||||
|
||||
/// Getter-syntax for .state.
|
||||
float get_state() const;
|
||||
|
||||
/// Get the accuracy in decimals. Based on the step value.
|
||||
int8_t get_accuracy_decimals();
|
||||
|
||||
/** Publish the current state to the front-end.
|
||||
*/
|
||||
void publish_state(float state);
|
||||
|
||||
NumberCall make_call();
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
/// Add a callback that will be called every time the state changes.
|
||||
void add_on_state_callback(std::function<void(float)> &&callback);
|
||||
|
||||
/** This member variable stores the last state.
|
||||
*
|
||||
* On startup, when no state is available yet, this is NAN (not-a-number) and the validity
|
||||
* can be checked using has_state().
|
||||
*
|
||||
* This is exposed through a member variable for ease of use in esphome lambdas.
|
||||
*/
|
||||
float state;
|
||||
|
||||
void publish_state(float state);
|
||||
|
||||
NumberCall make_call() { return NumberCall(this); }
|
||||
void set(float value) { make_call().set_value(value).perform(); }
|
||||
|
||||
void add_on_state_callback(std::function<void(float)> &&callback);
|
||||
|
||||
NumberTraits traits;
|
||||
|
||||
/// Return whether this number has gotten a full state yet.
|
||||
bool has_state() const;
|
||||
|
||||
/// Return with which interval the number is polled. Return 0 for non-polling mode.
|
||||
virtual uint32_t update_interval();
|
||||
|
||||
void set_min_value(float min_value) { this->min_value_ = min_value; }
|
||||
void set_max_value(float max_value) { this->max_value_ = max_value; }
|
||||
void set_step(float step) { this->step_ = step; }
|
||||
|
||||
float get_min_value() const { return this->min_value_; }
|
||||
float get_max_value() const { return this->max_value_; }
|
||||
float get_step() const { return this->step_; }
|
||||
bool has_state() const { return has_state_; }
|
||||
|
||||
protected:
|
||||
friend class NumberCall;
|
||||
@@ -95,17 +79,12 @@ class Number : public Nameable {
|
||||
*
|
||||
* @param value The value as validated by the NumberCall.
|
||||
*/
|
||||
virtual void set(float value) = 0;
|
||||
virtual void control(float value) = 0;
|
||||
|
||||
uint32_t hash_base() override;
|
||||
|
||||
CallbackManager<void(float)> state_callback_;
|
||||
/// Override the icon advertised to Home Assistant, otherwise number's icon will be used.
|
||||
optional<std::string> icon_;
|
||||
bool has_state_{false};
|
||||
float step_{1.0};
|
||||
float min_value_{0};
|
||||
float max_value_{100};
|
||||
};
|
||||
|
||||
} // namespace number
|
||||
|
||||
Reference in New Issue
Block a user