mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Template sensors always publish on update interval (#2224)
Co-authored-by: Chris Nussbaum <chris.nussbaum@protolabs.com>
This commit is contained in:
		| @@ -7,13 +7,14 @@ namespace template_ { | |||||||
| static const char *const TAG = "template.sensor"; | static const char *const TAG = "template.sensor"; | ||||||
|  |  | ||||||
| void TemplateSensor::update() { | void TemplateSensor::update() { | ||||||
|   if (!this->f_.has_value()) |   if (this->f_.has_value()) { | ||||||
|     return; |  | ||||||
|  |  | ||||||
|     auto val = (*this->f_)(); |     auto val = (*this->f_)(); | ||||||
|     if (val.has_value()) { |     if (val.has_value()) { | ||||||
|       this->publish_state(*val); |       this->publish_state(*val); | ||||||
|     } |     } | ||||||
|  |   } else if (!isnan(this->get_raw_state())) { | ||||||
|  |     this->publish_state(this->get_raw_state()); | ||||||
|  |   } | ||||||
| } | } | ||||||
| float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; } | float TemplateSensor::get_setup_priority() const { return setup_priority::HARDWARE; } | ||||||
| void TemplateSensor::set_template(std::function<optional<float>()> &&f) { this->f_ = f; } | void TemplateSensor::set_template(std::function<optional<float>()> &&f) { this->f_ = f; } | ||||||
|   | |||||||
| @@ -7,13 +7,14 @@ namespace template_ { | |||||||
| static const char *const TAG = "template.text_sensor"; | static const char *const TAG = "template.text_sensor"; | ||||||
|  |  | ||||||
| void TemplateTextSensor::update() { | void TemplateTextSensor::update() { | ||||||
|   if (!this->f_.has_value()) |   if (this->f_.has_value()) { | ||||||
|     return; |  | ||||||
|  |  | ||||||
|     auto val = (*this->f_)(); |     auto val = (*this->f_)(); | ||||||
|     if (val.has_value()) { |     if (val.has_value()) { | ||||||
|       this->publish_state(*val); |       this->publish_state(*val); | ||||||
|     } |     } | ||||||
|  |   } else if (this->has_state()) { | ||||||
|  |     this->publish_state(this->state); | ||||||
|  |   } | ||||||
| } | } | ||||||
| float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; } | float TemplateTextSensor::get_setup_priority() const { return setup_priority::HARDWARE; } | ||||||
| void TemplateTextSensor::set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; } | void TemplateTextSensor::set_template(std::function<optional<std::string>()> &&f) { this->f_ = f; } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user