Remove EntityBase from sprinkler (#4606)

* Remove EntityBase form sprinkler

* remove unneeded method

* Set name correctly
Move some timers to `setup` that rely on this->name_

* Fix SprinklerControllerSwitch setup

* Update esphome/components/sprinkler/__init__.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>

---------

Co-authored-by: Samuel Sieb <samuel-github@sieb.net>
Co-authored-by: Keith Burzinski <kbx81x@gmail.com>
This commit is contained in:
Jesse Hills 2023-03-22 20:35:16 +13:00 committed by GitHub
parent d70e7da0ef
commit d52e425ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 34 deletions

View file

@ -330,6 +330,7 @@ SPRINKLER_VALVE_SCHEMA = cv.Schema(
SPRINKLER_CONTROLLER_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(Sprinkler),
cv.Optional(CONF_NAME): cv.string,
cv.Optional(CONF_AUTO_ADVANCE_SWITCH): cv.maybe_simple_value(
switch.switch_schema(
SprinklerControllerSwitch, entity_category=ENTITY_CATEGORY_CONFIG
@ -424,7 +425,8 @@ SPRINKLER_CONTROLLER_SCHEMA = cv.Schema(
): cv.positive_time_period_seconds,
cv.Required(CONF_VALVES): cv.ensure_list(SPRINKLER_VALVE_SCHEMA),
}
).extend(cv.ENTITY_BASE_SCHEMA)
).extend(cv.COMPONENT_SCHEMA)
CONFIG_SCHEMA = cv.All(
cv.ensure_list(SPRINKLER_CONTROLLER_SCHEMA),
@ -559,16 +561,19 @@ async def sprinkler_simple_action_to_code(config, action_id, template_arg, args)
async def to_code(config):
for sprinkler_controller in config:
if len(sprinkler_controller[CONF_VALVES]) > 1:
var = cg.new_Pvariable(
sprinkler_controller[CONF_ID],
sprinkler_controller[CONF_MAIN_SWITCH][CONF_NAME],
)
var = cg.new_Pvariable(sprinkler_controller[CONF_ID])
if CONF_NAME in sprinkler_controller:
cg.add(var.set_name(sprinkler_controller[CONF_NAME]))
else:
var = cg.new_Pvariable(
sprinkler_controller[CONF_ID],
sprinkler_controller[CONF_VALVES][0][CONF_VALVE_SWITCH][CONF_NAME],
)
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))
await cg.register_component(var, sprinkler_controller)
if len(sprinkler_controller[CONF_VALVES]) > 1:

View file

@ -138,15 +138,7 @@ float SprinklerControllerSwitch::get_setup_priority() const { return setup_prior
Trigger<> *SprinklerControllerSwitch::get_turn_on_trigger() const { return this->turn_on_trigger_; }
Trigger<> *SprinklerControllerSwitch::get_turn_off_trigger() const { return this->turn_off_trigger_; }
void SprinklerControllerSwitch::setup() {
this->state = this->get_initial_state_with_restore_mode().value_or(false);
if (this->state) {
this->turn_on();
} else {
this->turn_off();
}
}
void SprinklerControllerSwitch::setup() { this->state = this->get_initial_state_with_restore_mode().value_or(false); }
void SprinklerControllerSwitch::dump_config() { LOG_SWITCH("", "Sprinkler Switch", this); }
@ -378,10 +370,11 @@ SprinklerValveOperator *SprinklerValveRunRequest::valve_operator() { return this
SprinklerValveRunRequestOrigin SprinklerValveRunRequest::request_is_from() { return this->origin_; }
Sprinkler::Sprinkler() {}
Sprinkler::Sprinkler(const std::string &name) : EntityBase(name) {}
void Sprinkler::setup() { this->all_valves_off_(true); }
void Sprinkler::setup() {
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::loop() {
for (auto &p : this->pump_) {
@ -1218,8 +1211,6 @@ SprinklerSwitch *Sprinkler::valve_pump_switch_by_pump_index(size_t pump_index) {
return nullptr;
}
uint32_t Sprinkler::hash_base() { return 3129891955UL; }
bool Sprinkler::valve_is_enabled_(const size_t valve_number) {
if (this->is_a_valid_valve(valve_number)) {
if (this->valve_[valve_number].enable_switch != nullptr) {

View file

@ -201,15 +201,14 @@ class SprinklerValveRunRequest {
SprinklerValveRunRequestOrigin origin_{USER};
};
class Sprinkler : public Component, public EntityBase {
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);
@ -423,8 +422,6 @@ class Sprinkler : public Component, public EntityBase {
SprinklerSwitch *valve_pump_switch_by_pump_index(size_t pump_index);
protected:
uint32_t hash_base() override;
/// returns true if valve number is enabled
bool valve_is_enabled_(size_t valve_number);
@ -528,6 +525,8 @@ class Sprinkler : public Component, public EntityBase {
uint32_t start_delay_{0};
uint32_t stop_delay_{0};
std::string name_;
/// Sprinkler controller state
SprinklerState state_{IDLE};
@ -574,9 +573,7 @@ class Sprinkler : public Component, public EntityBase {
std::vector<SprinklerValveOperator> valve_op_{2};
/// Valve control timers
std::vector<SprinklerTimer> timer_{
{this->name_ + "sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)},
{this->name_ + "vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)}};
std::vector<SprinklerTimer> timer_{};
/// Other Sprinkler instances we should be aware of (used to check if pumps are in use)
std::vector<Sprinkler *> other_controllers_;