diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 40f95d72f9..d66b79a2d2 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -22,6 +22,7 @@ from esphome.const import ( CONF_ON_PRESS, CONF_ON_RELEASE, CONF_ON_STATE, + CONF_PUBLISH_INITIAL_STATE, CONF_STATE, CONF_TIMING, CONF_TRIGGER_ID, @@ -326,6 +327,7 @@ BINARY_SENSOR_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).ex cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id( mqtt.MQTTBinarySensorComponent ), + cv.Optional(CONF_PUBLISH_INITIAL_STATE): cv.boolean, cv.Optional(CONF_DEVICE_CLASS): validate_device_class, cv.Optional(CONF_FILTERS): validate_filters, cv.Optional(CONF_ON_PRESS): automation.validate_automation( @@ -418,6 +420,8 @@ async def setup_binary_sensor_core_(var, config): if CONF_DEVICE_CLASS in config: cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) + if CONF_PUBLISH_INITIAL_STATE in config: + cg.add(var.set_publish_initial_state(config[CONF_PUBLISH_INITIAL_STATE])) if CONF_INVERTED in config: cg.add(var.set_inverted(config[CONF_INVERTED])) if CONF_FILTERS in config: diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 07217eebed..1f837e3ac1 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -37,7 +37,7 @@ void BinarySensor::send_state_internal(bool state, bool is_initial) { } this->has_state_ = true; this->state = state; - if (!is_initial) { + if (!is_initial || this->publish_initial_state_) { this->state_callback_.call(state); } } diff --git a/esphome/components/binary_sensor/binary_sensor.h b/esphome/components/binary_sensor/binary_sensor.h index 87d87b8eb4..b0c39a2c9b 100644 --- a/esphome/components/binary_sensor/binary_sensor.h +++ b/esphome/components/binary_sensor/binary_sensor.h @@ -58,6 +58,8 @@ class BinarySensor : public EntityBase { void add_filter(Filter *filter); void add_filters(const std::vector &filters); + void set_publish_initial_state(bool publish_initial_state) { this->publish_initial_state_ = publish_initial_state; } + // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) void send_state_internal(bool state, bool is_initial); @@ -80,6 +82,7 @@ class BinarySensor : public EntityBase { optional device_class_{}; ///< Stores the override of the device class Filter *filter_list_{nullptr}; bool has_state_{false}; + bool publish_initial_state_{false}; Deduplicator publish_dedup_; }; diff --git a/esphome/const.py b/esphome/const.py index df3654639d..9e83c63fd5 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -533,6 +533,7 @@ CONF_PRESSURE = "pressure" CONF_PRIORITY = "priority" CONF_PROJECT = "project" CONF_PROTOCOL = "protocol" +CONF_PUBLISH_INITIAL_STATE = "publish_initial_state" CONF_PULL_MODE = "pull_mode" CONF_PULLDOWN = "pulldown" CONF_PULLUP = "pullup"