mirror of
https://github.com/esphome/esphome.git
synced 2026-09-22 20:48:43 +00:00
[cover] Use bitmask template parameter for ControlAction/CoverPublishAction
Apply the bitmask pattern from LightControlAction (#16039) to cover::ControlAction (3 fields: stop, position, tilt) and cover::CoverPublishAction (3 fields: position, tilt, current_operation). Unused fields are elided via [[no_unique_address]] and skipped at compile time in play() via if constexpr. Codegen for cover.control: and cover.template.publish: builds the bitmask from the YAML keys present. CONF_STATE and CONF_POSITION both map to the same position bit (they are mutually exclusive YAML keys for the same C++ field). Per-instance: 16-28 B depending on which fields are set, down from ~28 B baseline.
This commit is contained in:
@@ -299,7 +299,21 @@ COVER_CONTROL_ACTION_SCHEMA = cv.Schema(
|
||||
)
|
||||
async def cover_control_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
# Bit positions must match COVER_CONTROL_FIELDS in automation.h.
|
||||
# CONF_STATE and CONF_POSITION both map to set_position (bit 1).
|
||||
field_mask = 0
|
||||
if CONF_STOP in config:
|
||||
field_mask |= 1 << 0
|
||||
if CONF_STATE in config or CONF_POSITION in config:
|
||||
field_mask |= 1 << 1
|
||||
if CONF_TILT in config:
|
||||
field_mask |= 1 << 2
|
||||
|
||||
control_template_arg = cg.TemplateArguments(
|
||||
cg.RawExpression(f"static_cast<uint16_t>({field_mask})"), *template_arg
|
||||
)
|
||||
var = cg.new_Pvariable(action_id, control_template_arg, paren)
|
||||
if (stop := config.get(CONF_STOP)) is not None:
|
||||
template_ = await cg.templatable(stop, args, cg.bool_)
|
||||
cg.add(var.set_stop(template_))
|
||||
|
||||
@@ -46,49 +46,86 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
||||
Cover *cover_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ControlAction : public Action<Ts...> {
|
||||
// Unique Empty<Tag> per field so [[no_unique_address]] is guaranteed to coalesce.
|
||||
namespace cover_action_detail {
|
||||
template<int Tag> struct Empty {};
|
||||
} // namespace cover_action_detail
|
||||
|
||||
// X-macro: (type, field_name, bit_index). Order/bits must match
|
||||
// cover_control_to_code's FIELDS table in __init__.py.
|
||||
#define COVER_CONTROL_FIELDS(X) \
|
||||
X(bool, stop, 0) \
|
||||
X(float, position, 1) \
|
||||
X(float, tilt, 2)
|
||||
|
||||
template<uint16_t Fields, typename... Ts> class ControlAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit ControlAction(Cover *cover) : cover_(cover) {}
|
||||
|
||||
TEMPLATABLE_VALUE(bool, stop)
|
||||
TEMPLATABLE_VALUE(float, position)
|
||||
TEMPLATABLE_VALUE(float, tilt)
|
||||
#define COVER_FIELD_SETTER_(type, name, idx) \
|
||||
template<typename V> void set_##name(V value) requires((Fields & (1 << (idx))) != 0) { this->name##_ = value; }
|
||||
#define COVER_FIELD_APPLY_(type, name, idx) \
|
||||
if constexpr ((Fields & (1 << (idx))) != 0) \
|
||||
call.set_##name(this->name##_.value(x...));
|
||||
#define COVER_FIELD_DECL_(type, name, idx) \
|
||||
[[no_unique_address]] std::conditional_t<(Fields & (1 << (idx))) != 0, TemplatableFn<type, Ts...>, \
|
||||
cover_action_detail::Empty<(idx)>> \
|
||||
name##_{};
|
||||
|
||||
COVER_CONTROL_FIELDS(COVER_FIELD_SETTER_)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
auto call = this->cover_->make_call();
|
||||
if (this->stop_.has_value())
|
||||
call.set_stop(this->stop_.value(x...));
|
||||
if (this->position_.has_value())
|
||||
call.set_position(this->position_.value(x...));
|
||||
if (this->tilt_.has_value())
|
||||
call.set_tilt(this->tilt_.value(x...));
|
||||
COVER_CONTROL_FIELDS(COVER_FIELD_APPLY_)
|
||||
call.perform();
|
||||
}
|
||||
|
||||
protected:
|
||||
Cover *cover_;
|
||||
COVER_CONTROL_FIELDS(COVER_FIELD_DECL_)
|
||||
};
|
||||
#undef COVER_CONTROL_FIELDS
|
||||
|
||||
template<typename... Ts> class CoverPublishAction : public Action<Ts...> {
|
||||
// X-macro: (type, field_name, bit_index). Order/bits must match
|
||||
// cover_template_publish_to_code's FIELDS table in template/cover/__init__.py.
|
||||
#define COVER_PUBLISH_FIELDS(X) \
|
||||
X(float, position, 0) \
|
||||
X(float, tilt, 1) \
|
||||
X(CoverOperation, current_operation, 2)
|
||||
|
||||
template<uint16_t Fields, typename... Ts> class CoverPublishAction : public Action<Ts...> {
|
||||
public:
|
||||
CoverPublishAction(Cover *cover) : cover_(cover) {}
|
||||
TEMPLATABLE_VALUE(float, position)
|
||||
TEMPLATABLE_VALUE(float, tilt)
|
||||
TEMPLATABLE_VALUE(CoverOperation, current_operation)
|
||||
|
||||
#define COVER_PUBLISH_SETTER_(type, name, idx) \
|
||||
template<typename V> void set_##name(V value) requires((Fields & (1 << (idx))) != 0) { this->name##_ = value; }
|
||||
#define COVER_PUBLISH_APPLY_(type, name, idx) \
|
||||
if constexpr ((Fields & (1 << (idx))) != 0) \
|
||||
this->cover_->name = this->name##_.value(x...);
|
||||
#define COVER_PUBLISH_DECL_(type, name, idx) \
|
||||
[[no_unique_address]] std::conditional_t<(Fields & (1 << (idx))) != 0, TemplatableFn<type, Ts...>, \
|
||||
cover_action_detail::Empty<(idx) + 8>> \
|
||||
name##_{};
|
||||
|
||||
COVER_PUBLISH_FIELDS(COVER_PUBLISH_SETTER_)
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
if (this->position_.has_value())
|
||||
this->cover_->position = this->position_.value(x...);
|
||||
if (this->tilt_.has_value())
|
||||
this->cover_->tilt = this->tilt_.value(x...);
|
||||
if (this->current_operation_.has_value())
|
||||
this->cover_->current_operation = this->current_operation_.value(x...);
|
||||
COVER_PUBLISH_FIELDS(COVER_PUBLISH_APPLY_)
|
||||
this->cover_->publish_state();
|
||||
}
|
||||
|
||||
protected:
|
||||
Cover *cover_;
|
||||
COVER_PUBLISH_FIELDS(COVER_PUBLISH_DECL_)
|
||||
|
||||
#undef COVER_PUBLISH_DECL_
|
||||
#undef COVER_PUBLISH_APPLY_
|
||||
#undef COVER_PUBLISH_SETTER_
|
||||
#undef COVER_FIELD_DECL_
|
||||
#undef COVER_FIELD_APPLY_
|
||||
#undef COVER_FIELD_SETTER_
|
||||
};
|
||||
#undef COVER_PUBLISH_FIELDS
|
||||
|
||||
template<bool OPEN, typename... Ts> class CoverPositionCondition : public Condition<Ts...> {
|
||||
public:
|
||||
|
||||
@@ -128,7 +128,21 @@ async def to_code(config):
|
||||
)
|
||||
async def cover_template_publish_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
# Bit positions must match COVER_PUBLISH_FIELDS in cover/automation.h.
|
||||
# CONF_STATE and CONF_POSITION both map to set_position (bit 0).
|
||||
field_mask = 0
|
||||
if CONF_STATE in config or CONF_POSITION in config:
|
||||
field_mask |= 1 << 0
|
||||
if CONF_TILT in config:
|
||||
field_mask |= 1 << 1
|
||||
if CONF_CURRENT_OPERATION in config:
|
||||
field_mask |= 1 << 2
|
||||
|
||||
publish_template_arg = cg.TemplateArguments(
|
||||
cg.RawExpression(f"static_cast<uint16_t>({field_mask})"), *template_arg
|
||||
)
|
||||
var = cg.new_Pvariable(action_id, publish_template_arg, paren)
|
||||
if CONF_STATE in config:
|
||||
template_ = await cg.templatable(config[CONF_STATE], args, cg.float_)
|
||||
cg.add(var.set_position(template_))
|
||||
|
||||
Reference in New Issue
Block a user