From d52e425ba28d4875bd3ffe7303cd16245e6690c5 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 22 Mar 2023 20:35:16 +1300 Subject: [PATCH 1/3] 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 Co-authored-by: Keith Burzinski --- esphome/components/sprinkler/__init__.py | 25 +++++++++++++--------- esphome/components/sprinkler/sprinkler.cpp | 21 ++++++------------ esphome/components/sprinkler/sprinkler.h | 15 ++++++------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/esphome/components/sprinkler/__init__.py b/esphome/components/sprinkler/__init__.py index cf3f471234..d49b1ba381 100644 --- a/esphome/components/sprinkler/__init__.py +++ b/esphome/components/sprinkler/__init__.py @@ -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: diff --git a/esphome/components/sprinkler/sprinkler.cpp b/esphome/components/sprinkler/sprinkler.cpp index d73d8d8fbf..6169185d60 100644 --- a/esphome/components/sprinkler/sprinkler.cpp +++ b/esphome/components/sprinkler/sprinkler.cpp @@ -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) { diff --git a/esphome/components/sprinkler/sprinkler.h b/esphome/components/sprinkler/sprinkler.h index 1cde60321d..7952c4533f 100644 --- a/esphome/components/sprinkler/sprinkler.h +++ b/esphome/components/sprinkler/sprinkler.h @@ -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 valve_op_{2}; /// Valve control timers - std::vector 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 timer_{}; /// Other Sprinkler instances we should be aware of (used to check if pumps are in use) std::vector other_controllers_; From a1eb3b8475c92f4477d64fc3cce5e4e3275d33c4 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 22 Mar 2023 20:56:02 +1300 Subject: [PATCH 2/3] Swap curly brackets for round on LockGuard (#4610) --- esphome/core/helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index a107b1b849..8950cc3b8a 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -547,7 +547,7 @@ class Mutex { */ class LockGuard { public: - LockGuard(Mutex &mutex) : mutex_{mutex} { mutex_.lock(); } + LockGuard(Mutex &mutex) : mutex_(mutex) { mutex_.lock(); } ~LockGuard() { mutex_.unlock(); } private: From e13eaf6706b094c35b00ede1d86ab2d716456c1f Mon Sep 17 00:00:00 2001 From: guillempages Date: Wed, 22 Mar 2023 09:05:09 +0100 Subject: [PATCH 3/3] Fix animation resizing (#4608) Animation resizing in RGB24 format is causing an error "Image cannot be resized to a bigger size". Other image types do not show the issue, and the only difference is the "image.thumbnail" call. Removed the call and tested; the animation is shown with the desired size. --- esphome/components/animation/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/esphome/components/animation/__init__.py b/esphome/components/animation/__init__.py index ce9f057496..68c3eee132 100644 --- a/esphome/components/animation/__init__.py +++ b/esphome/components/animation/__init__.py @@ -76,8 +76,6 @@ async def to_code(config): pos = 0 for frameIndex in range(frames): image.seek(frameIndex) - if CONF_RESIZE in config: - image.thumbnail(config[CONF_RESIZE]) frame = image.convert("RGB") if CONF_RESIZE in config: frame = frame.resize([width, height])