mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Fix home assistant binary sensor initial state (#632)
* Fix home assistant binary sensor initial state * Fix send state log message * fix new_state local name * lint * Trigger Co-authored-by: Guillermo Ruffino <guillermo.ruffino@pampatech.net>
This commit is contained in:
		
				
					committed by
					
						 Otto Winter
						Otto Winter
					
				
			
			
				
	
			
			
			
						parent
						
							dc9f304d94
						
					
				
				
					commit
					7abe8875bd
				
			| @@ -30,7 +30,11 @@ void BinarySensor::publish_initial_state(bool state) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| void BinarySensor::send_state_internal(bool state, bool is_initial) { | void BinarySensor::send_state_internal(bool state, bool is_initial) { | ||||||
|   ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), state ? "ON" : "OFF"); |   if (is_initial) { | ||||||
|  |     ESP_LOGD(TAG, "'%s': Sending initial state %s", this->get_name().c_str(), ONOFF(state)); | ||||||
|  |   } else { | ||||||
|  |     ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), ONOFF(state)); | ||||||
|  |   } | ||||||
|   this->has_state_ = true; |   this->has_state_ = true; | ||||||
|   this->state = state; |   this->state = state; | ||||||
|   if (!is_initial) { |   if (!is_initial) { | ||||||
|   | |||||||
| @@ -16,14 +16,16 @@ void HomeassistantBinarySensor::setup() { | |||||||
|         ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str()); |         ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str()); | ||||||
|         break; |         break; | ||||||
|       case PARSE_ON: |       case PARSE_ON: | ||||||
|         ESP_LOGD(TAG, "'%s': Got state ON", this->entity_id_.c_str()); |  | ||||||
|         this->publish_state(true); |  | ||||||
|         break; |  | ||||||
|       case PARSE_OFF: |       case PARSE_OFF: | ||||||
|         ESP_LOGD(TAG, "'%s': Got state OFF", this->entity_id_.c_str()); |         bool new_state = val == PARSE_ON; | ||||||
|         this->publish_state(false); |         ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state)); | ||||||
|  |         if (this->initial_) | ||||||
|  |           this->publish_initial_state(new_state); | ||||||
|  |         else | ||||||
|  |           this->publish_state(new_state); | ||||||
|         break; |         break; | ||||||
|     } |     } | ||||||
|  |     this->initial_ = false; | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| void HomeassistantBinarySensor::dump_config() { | void HomeassistantBinarySensor::dump_config() { | ||||||
|   | |||||||
| @@ -15,6 +15,7 @@ class HomeassistantBinarySensor : public binary_sensor::BinarySensor, public Com | |||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   std::string entity_id_; |   std::string entity_id_; | ||||||
|  |   bool initial_{true}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace homeassistant | }  // namespace homeassistant | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user