[Sprinkler] Initialize timers early to avoid crash (#5499)

This commit is contained in:
Ilia Sotnikov 2023-10-09 06:37:48 +03:00 committed by GitHub
parent 412a866de8
commit c65d78f568
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 15 deletions

View file

@ -571,18 +571,12 @@ async def sprinkler_simple_action_to_code(config, action_id, template_arg, args)
async def to_code(config):
for sprinkler_controller in config:
var = cg.new_Pvariable(sprinkler_controller[CONF_ID])
if CONF_NAME in sprinkler_controller:
cg.add(var.set_name(sprinkler_controller[CONF_NAME]))
else:
if len(sprinkler_controller[CONF_VALVES]) > 1:
name = sprinkler_controller[CONF_MAIN_SWITCH][CONF_NAME]
else:
name = sprinkler_controller[CONF_VALVES][0][CONF_VALVE_SWITCH][
CONF_NAME
]
cg.add(var.set_name(name))
name = sprinkler_controller[CONF_VALVES][0][CONF_VALVE_SWITCH][CONF_NAME]
name = sprinkler_controller.get(CONF_NAME, name)
var = cg.new_Pvariable(sprinkler_controller[CONF_ID], name)
await cg.register_component(var, sprinkler_controller)

View file

@ -386,12 +386,17 @@ SprinklerValveOperator *SprinklerValveRunRequest::valve_operator() { return this
SprinklerValveRunRequestOrigin SprinklerValveRunRequest::request_is_from() { return this->origin_; }
void Sprinkler::setup() {
Sprinkler::Sprinkler() {}
Sprinkler::Sprinkler(const std::string &name) {
// The `name` is needed to set timers up, hence non-default constructor
// replaces `set_name()` method previously existed
this->name_ = name;
this->timer_.push_back({this->name_ + "sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)});
this->timer_.push_back({this->name_ + "vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)});
this->all_valves_off_(true);
}
void Sprinkler::setup() { this->all_valves_off_(true); }
void Sprinkler::loop() {
for (auto &p : this->pump_) {
p.loop();

View file

@ -204,12 +204,12 @@ class SprinklerValveRunRequest {
class Sprinkler : public Component {
public:
Sprinkler();
Sprinkler(const std::string &name);
void setup() override;
void loop() override;
void dump_config() override;
void set_name(const std::string &name) { this->name_ = name; }
/// add a valve to the controller
void add_valve(SprinklerControllerSwitch *valve_sw, SprinklerControllerSwitch *enable_sw = nullptr);