1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-13 00:32:20 +01:00

Number and Template Number updates (#2036)

Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Jesse Hills
2021-07-20 08:22:49 +12:00
committed by GitHub
parent 2e49fd7b48
commit 71d9d64a02
9 changed files with 148 additions and 153 deletions

View File

@@ -1,3 +1,4 @@
from typing import Optional
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
@@ -66,12 +67,18 @@ NUMBER_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend(
)
async def setup_number_core_(var, config):
async def setup_number_core_(
var, config, *, min_value: float, max_value: float, step: Optional[float]
):
cg.add(var.set_name(config[CONF_NAME]))
if CONF_INTERNAL in config:
cg.add(var.set_internal(config[CONF_INTERNAL]))
cg.add(var.set_icon(config[CONF_ICON]))
cg.add(var.traits.set_icon(config[CONF_ICON]))
cg.add(var.traits.set_min_value(min_value))
cg.add(var.traits.set_max_value(max_value))
if step is not None:
cg.add(var.traits.set_step(step))
for conf in config.get(CONF_ON_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
@@ -92,16 +99,24 @@ async def setup_number_core_(var, config):
await mqtt.register_mqtt_component(mqtt_, config)
async def register_number(var, config):
async def register_number(
var, config, *, min_value: float, max_value: float, step: Optional[float] = None
):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_number(var))
await setup_number_core_(var, config)
await setup_number_core_(
var, config, min_value=min_value, max_value=max_value, step=step
)
async def new_number(config):
async def new_number(
config, *, min_value: float, max_value: float, step: Optional[float] = None
):
var = cg.new_Pvariable(config[CONF_ID])
await register_number(var, config)
await register_number(
var, config, min_value=min_value, max_value=max_value, step=step
)
return var

View File

@@ -8,67 +8,38 @@ static const char *const TAG = "number";
void NumberCall::perform() {
ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
if (this->value_.has_value()) {
auto value = *this->value_;
uint8_t accuracy = this->parent_->get_accuracy_decimals();
float min_value = this->parent_->get_min_value();
if (value < min_value) {
ESP_LOGW(TAG, " Value %s must not be less than minimum %s", value_accuracy_to_string(value, accuracy).c_str(),
value_accuracy_to_string(min_value, accuracy).c_str());
this->value_.reset();
return;
}
float max_value = this->parent_->get_max_value();
if (value > max_value) {
ESP_LOGW(TAG, " Value %s must not be larger than maximum %s", value_accuracy_to_string(value, accuracy).c_str(),
value_accuracy_to_string(max_value, accuracy).c_str());
this->value_.reset();
return;
}
ESP_LOGD(TAG, " Value: %s", value_accuracy_to_string(*this->value_, accuracy).c_str());
this->parent_->set(*this->value_);
if (!this->value_.has_value() || isnan(*this->value_)) {
ESP_LOGW(TAG, "No value set for NumberCall");
return;
}
const auto &traits = this->parent_->traits;
auto value = *this->value_;
float min_value = traits.get_min_value();
if (value < min_value) {
ESP_LOGW(TAG, " Value %f must not be less than minimum %f", value, min_value);
return;
}
float max_value = traits.get_max_value();
if (value > max_value) {
ESP_LOGW(TAG, " Value %f must not be greater than maximum %f", value, max_value);
return;
}
ESP_LOGD(TAG, " Value: %f", *this->value_);
this->parent_->control(*this->value_);
}
NumberCall &NumberCall::set_value(float value) {
this->value_ = value;
return *this;
}
const optional<float> &NumberCall::get_value() const { return this->value_; }
NumberCall Number::make_call() { return NumberCall(this); }
void Number::publish_state(float state) {
this->has_state_ = true;
this->state = state;
ESP_LOGD(TAG, "'%s': Sending state %.5f", this->get_name().c_str(), state);
ESP_LOGD(TAG, "'%s': Sending state %f", this->get_name().c_str(), state);
this->state_callback_.call(state);
}
uint32_t Number::update_interval() { return 0; }
Number::Number(const std::string &name) : Nameable(name), state(NAN) {}
Number::Number() : Number("") {}
void Number::add_on_state_callback(std::function<void(float)> &&callback) {
this->state_callback_.add(std::move(callback));
}
void Number::set_icon(const std::string &icon) { this->icon_ = icon; }
std::string Number::get_icon() { return *this->icon_; }
int8_t Number::get_accuracy_decimals() {
// use printf %g to find number of digits based on step
char buf[32];
sprintf(buf, "%.5g", this->step_);
std::string str{buf};
size_t dot_pos = str.find('.');
if (dot_pos == std::string::npos)
return 0;
return str.length() - dot_pos - 1;
}
float Number::get_state() const { return this->state; }
bool Number::has_state() const { return this->has_state_; }
uint32_t Number::hash_base() { return 2282307003UL; }

View File

@@ -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