Skip to content

Commit

Permalink
Don't raise events until fully initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Jan 24, 2021
1 parent ccf11f9 commit 883914a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/shelly_hap_sensor_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ Status SensorBase::Init() {

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

handler_id_ =
in_->AddHandler(std::bind(&SensorBase::InputEventHandler, this, _1, _2));

if (cfg_->in_mode == (int) InMode::kLevel) {
SetInternalState(in_->GetState());
}

handler_id_ =
in_->AddHandler(std::bind(&SensorBase::InputEventHandler, this, _1, _2));

return Status::OK();
}

Expand Down Expand Up @@ -150,7 +150,10 @@ void SensorBase::SetInternalState(bool state) {
last_ev_ts_ = mgos_uptime();
}
state_ = state;
chars_[1]->RaiseEvent();
// May happen during init, we don't want to raise events until initialized.
if (handler_id_ != Input::kInvalidHandlerID) {
chars_[1]->RaiseEvent();
}
}
if (state && cfg_->in_mode == (int) InMode::kPulse) {
auto_off_timer_.Reset(cfg_->idle_time * 1000, 0);
Expand Down
5 changes: 4 additions & 1 deletion src/shelly_hap_stateless_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ void StatelessSwitch::RaiseEvent(uint8_t ev) {
last_ev_ = ev;
last_ev_ts_ = mgos_uptime();
LOG(LL_INFO, ("Input %d: HAP event (mode %d): %d", id(), cfg_->in_mode, ev));
chars_[1]->RaiseEvent();
// May happen during init, we don't want to raise events until initialized.
if (handler_id_ != Input::kInvalidHandlerID) {
chars_[1]->RaiseEvent();
}
}

} // namespace hap
Expand Down

0 comments on commit 883914a

Please sign in to comment.