Add optimistic config flag to modbus select. (#3267)

This commit is contained in:
stegm 2022-03-09 20:40:43 +01:00 committed by Jesse Hills
parent efa8f0730d
commit 06a3505698
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
3 changed files with 8 additions and 1 deletions

View file

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import select from esphome.components import select
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA, CONF_OPTIMISTIC
from esphome.jsonschema import jschema_composite from esphome.jsonschema import jschema_composite
from .. import ( from .. import (
@ -79,6 +79,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean, cv.Optional(CONF_FORCE_NEW_RANGE, default=False): cv.boolean,
cv.Required(CONF_OPTIONSMAP): ensure_option_map(), cv.Required(CONF_OPTIONSMAP): ensure_option_map(),
cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean, cv.Optional(CONF_USE_WRITE_MULTIPLE, default=False): cv.boolean,
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
cv.Optional(CONF_LAMBDA): cv.returning_lambda, cv.Optional(CONF_LAMBDA): cv.returning_lambda,
cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda, cv.Optional(CONF_WRITE_LAMBDA): cv.returning_lambda,
}, },
@ -112,6 +113,7 @@ async def to_code(config):
cg.add(parent.add_sensor_item(var)) cg.add(parent.add_sensor_item(var))
cg.add(var.set_parent(parent)) cg.add(var.set_parent(parent))
cg.add(var.set_use_write_mutiple(config[CONF_USE_WRITE_MULTIPLE])) cg.add(var.set_use_write_mutiple(config[CONF_USE_WRITE_MULTIPLE]))
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
if CONF_LAMBDA in config: if CONF_LAMBDA in config:
template_ = await cg.process_lambda( template_ = await cg.process_lambda(

View file

@ -80,6 +80,9 @@ void ModbusSelect::control(const std::string &value) {
} }
parent_->queue_command(write_cmd); parent_->queue_command(write_cmd);
if (this->optimistic_)
this->publish_state(value);
} }
} // namespace modbus_controller } // namespace modbus_controller

View file

@ -32,6 +32,7 @@ class ModbusSelect : public Component, public select::Select, public SensorItem
void set_parent(ModbusController *const parent) { this->parent_ = parent; } void set_parent(ModbusController *const parent) { this->parent_ = parent; }
void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; } void set_use_write_mutiple(bool use_write_multiple) { this->use_write_multiple_ = use_write_multiple; }
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
void set_template(transform_func_t &&f) { this->transform_func_ = f; } void set_template(transform_func_t &&f) { this->transform_func_ = f; }
void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; } void set_write_template(write_transform_func_t &&f) { this->write_transform_func_ = f; }
@ -43,6 +44,7 @@ class ModbusSelect : public Component, public select::Select, public SensorItem
std::vector<int64_t> mapping_; std::vector<int64_t> mapping_;
ModbusController *parent_; ModbusController *parent_;
bool use_write_multiple_{false}; bool use_write_multiple_{false};
bool optimistic_{false};
optional<transform_func_t> transform_func_; optional<transform_func_t> transform_func_;
optional<write_transform_func_t> write_transform_func_; optional<write_transform_func_t> write_transform_func_;
}; };