Skip to content

Commit 883914a

Browse files
committed
Don't raise events until fully initialized
1 parent ccf11f9 commit 883914a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/shelly_hap_sensor_base.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ Status SensorBase::Init() {
4949

5050
AddNameChar(svc_.iid + 1, cfg_->name);
5151

52-
handler_id_ =
53-
in_->AddHandler(std::bind(&SensorBase::InputEventHandler, this, _1, _2));
54-
5552
if (cfg_->in_mode == (int) InMode::kLevel) {
5653
SetInternalState(in_->GetState());
5754
}
5855

56+
handler_id_ =
57+
in_->AddHandler(std::bind(&SensorBase::InputEventHandler, this, _1, _2));
58+
5959
return Status::OK();
6060
}
6161

@@ -150,7 +150,10 @@ void SensorBase::SetInternalState(bool state) {
150150
last_ev_ts_ = mgos_uptime();
151151
}
152152
state_ = state;
153-
chars_[1]->RaiseEvent();
153+
// May happen during init, we don't want to raise events until initialized.
154+
if (handler_id_ != Input::kInvalidHandlerID) {
155+
chars_[1]->RaiseEvent();
156+
}
154157
}
155158
if (state && cfg_->in_mode == (int) InMode::kPulse) {
156159
auto_off_timer_.Reset(cfg_->idle_time * 1000, 0);

src/shelly_hap_stateless_switch.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ void StatelessSwitch::RaiseEvent(uint8_t ev) {
164164
last_ev_ = ev;
165165
last_ev_ts_ = mgos_uptime();
166166
LOG(LL_INFO, ("Input %d: HAP event (mode %d): %d", id(), cfg_->in_mode, ev));
167-
chars_[1]->RaiseEvent();
167+
// May happen during init, we don't want to raise events until initialized.
168+
if (handler_id_ != Input::kInvalidHandlerID) {
169+
chars_[1]->RaiseEvent();
170+
}
168171
}
169172

170173
} // namespace hap

0 commit comments

Comments
 (0)