1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-06 02:40:29 +01:00

[lvgl] Implement switch restore (#8481)

This commit is contained in:
Clyde Stubbs 2025-03-30 20:09:19 +11:00 committed by GitHub
parent ffc233d99d
commit c42343be3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 24 deletions

View File

@ -1,7 +1,9 @@
import esphome.codegen as cg
from esphome.components.switch import Switch, new_switch, switch_schema
from esphome.components.switch import Switch, register_switch, switch_schema
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.cpp_generator import MockObj
from esphome.cpp_types import Component
from ..defines import CONF_WIDGET, literal
from ..lvcode import (
@ -18,7 +20,7 @@ from ..lvcode import (
from ..types import LV_EVENT, LV_STATE, lv_pseudo_button_t, lvgl_ns
from ..widgets import get_widgets, wait_for_widgets
LVGLSwitch = lvgl_ns.class_("LVGLSwitch", Switch)
LVGLSwitch = lvgl_ns.class_("LVGLSwitch", Switch, Component)
CONFIG_SCHEMA = switch_schema(LVGLSwitch).extend(
{
cv.Required(CONF_WIDGET): cv.use_id(lv_pseudo_button_t),
@ -27,21 +29,24 @@ CONFIG_SCHEMA = switch_schema(LVGLSwitch).extend(
async def to_code(config):
switch = await new_switch(config)
widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LambdaContext(EVENT_ARG) as checked_ctx:
checked_ctx.add(switch.publish_state(widget.get_value()))
switch_id = MockObj(config[CONF_ID], "->")
v = literal("v")
async with LambdaContext([(cg.bool_, "v")]) as control:
with LvConditional(MockObj("v")) as cond:
with LvConditional(v) as cond:
widget.add_state(LV_STATE.CHECKED)
cond.else_()
widget.clear_state(LV_STATE.CHECKED)
lv.event_send(widget.obj, API_EVENT, cg.nullptr)
control.add(switch.publish_state(literal("v")))
control.add(switch_id.publish_state(v))
switch = cg.new_Pvariable(config[CONF_ID], await control.get_lambda())
await cg.register_component(switch, config)
await register_switch(switch, config)
async with LambdaContext(EVENT_ARG) as checked_ctx:
checked_ctx.add(switch.publish_state(widget.get_value()))
async with LvContext() as ctx:
lv_add(switch.set_control_lambda(await control.get_lambda()))
ctx.add(
lvgl_static.add_event_cb(
widget.obj,

View File

@ -10,26 +10,15 @@
namespace esphome {
namespace lvgl {
class LVGLSwitch : public switch_::Switch {
class LVGLSwitch : public switch_::Switch, public Component {
public:
void set_control_lambda(std::function<void(bool)> state_lambda) {
this->state_lambda_ = std::move(state_lambda);
if (this->initial_state_.has_value()) {
this->state_lambda_(this->initial_state_.value());
this->initial_state_.reset();
}
}
LVGLSwitch(std::function<void(bool)> state_lambda) : state_lambda_(std::move(state_lambda)) {}
void setup() override { this->write_state(this->get_initial_state_with_restore_mode().value_or(false)); }
protected:
void write_state(bool value) override {
if (this->state_lambda_ != nullptr) {
this->state_lambda_(value);
} else {
this->initial_state_ = value;
}
}
void write_state(bool value) override { this->state_lambda_(value); }
std::function<void(bool)> state_lambda_{};
optional<bool> initial_state_{};
};
} // namespace lvgl