Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/miot/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
MiotSwitch = miot_ns.class_("MiotSwitch", switch.Switch, cg.Component)

CONFIG_SCHEMA = (
switch.switch_schema(MiotSwitch)
switch.switch_schema(MiotSwitch, default_restore_mode="DISABLED")
.extend(
{
cv.GenerateID(CONF_MIOT_ID): cv.use_id(Miot),
Expand Down
12 changes: 12 additions & 0 deletions components/miot/switch/miot_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ namespace miot {
static const char *const TAG = "miot.switch";

void MiotSwitch::setup() {
this->restore_state_ = this->get_initial_state_with_restore_mode();
this->restore_pending_ = this->restore_state_.has_value();

this->parent_->register_listener(this->siid_, this->piid_, this->poll_, mvtString, [this](const MiotValue &value) {
bool v = value.as_string == true_value_;
ESP_LOGV(TAG, "MCU reported switch %" PRIu32 ":%" PRIu32 " is: %s", this->siid_, this->piid_, ONOFF(v));

if (this->restore_pending_) {
this->restore_pending_ = false;
if (this->restore_state_.value() != v) {
this->write_state(this->restore_state_.value());
return;
}
}

this->publish_state(v);
});
}
Expand Down
2 changes: 2 additions & 0 deletions components/miot/switch/miot_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class MiotSwitch : public switch_::Switch, public Component {
bool poll_{true};
std::string true_value_;
std::string false_value_;
optional<bool> restore_state_{};
bool restore_pending_{false};
};

} // namespace miot
Expand Down