mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
add option to publish initial state of binary sensors (#3636)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
04f4dd8a22
commit
6a4e0cf667
4 changed files with 9 additions and 1 deletions
|
@ -22,6 +22,7 @@ from esphome.const import (
|
||||||
CONF_ON_PRESS,
|
CONF_ON_PRESS,
|
||||||
CONF_ON_RELEASE,
|
CONF_ON_RELEASE,
|
||||||
CONF_ON_STATE,
|
CONF_ON_STATE,
|
||||||
|
CONF_PUBLISH_INITIAL_STATE,
|
||||||
CONF_STATE,
|
CONF_STATE,
|
||||||
CONF_TIMING,
|
CONF_TIMING,
|
||||||
CONF_TRIGGER_ID,
|
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(
|
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(
|
||||||
mqtt.MQTTBinarySensorComponent
|
mqtt.MQTTBinarySensorComponent
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_PUBLISH_INITIAL_STATE): cv.boolean,
|
||||||
cv.Optional(CONF_DEVICE_CLASS): validate_device_class,
|
cv.Optional(CONF_DEVICE_CLASS): validate_device_class,
|
||||||
cv.Optional(CONF_FILTERS): validate_filters,
|
cv.Optional(CONF_FILTERS): validate_filters,
|
||||||
cv.Optional(CONF_ON_PRESS): automation.validate_automation(
|
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:
|
if CONF_DEVICE_CLASS in config:
|
||||||
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS]))
|
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:
|
if CONF_INVERTED in config:
|
||||||
cg.add(var.set_inverted(config[CONF_INVERTED]))
|
cg.add(var.set_inverted(config[CONF_INVERTED]))
|
||||||
if CONF_FILTERS in config:
|
if CONF_FILTERS in config:
|
||||||
|
|
|
@ -37,7 +37,7 @@ void BinarySensor::send_state_internal(bool state, bool is_initial) {
|
||||||
}
|
}
|
||||||
this->has_state_ = true;
|
this->has_state_ = true;
|
||||||
this->state = state;
|
this->state = state;
|
||||||
if (!is_initial) {
|
if (!is_initial || this->publish_initial_state_) {
|
||||||
this->state_callback_.call(state);
|
this->state_callback_.call(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,8 @@ class BinarySensor : public EntityBase {
|
||||||
void add_filter(Filter *filter);
|
void add_filter(Filter *filter);
|
||||||
void add_filters(const std::vector<Filter *> &filters);
|
void add_filters(const std::vector<Filter *> &filters);
|
||||||
|
|
||||||
|
void set_publish_initial_state(bool publish_initial_state) { this->publish_initial_state_ = publish_initial_state; }
|
||||||
|
|
||||||
// ========== INTERNAL METHODS ==========
|
// ========== INTERNAL METHODS ==========
|
||||||
// (In most use cases you won't need these)
|
// (In most use cases you won't need these)
|
||||||
void send_state_internal(bool state, bool is_initial);
|
void send_state_internal(bool state, bool is_initial);
|
||||||
|
@ -80,6 +82,7 @@ class BinarySensor : public EntityBase {
|
||||||
optional<std::string> device_class_{}; ///< Stores the override of the device class
|
optional<std::string> device_class_{}; ///< Stores the override of the device class
|
||||||
Filter *filter_list_{nullptr};
|
Filter *filter_list_{nullptr};
|
||||||
bool has_state_{false};
|
bool has_state_{false};
|
||||||
|
bool publish_initial_state_{false};
|
||||||
Deduplicator<bool> publish_dedup_;
|
Deduplicator<bool> publish_dedup_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -533,6 +533,7 @@ CONF_PRESSURE = "pressure"
|
||||||
CONF_PRIORITY = "priority"
|
CONF_PRIORITY = "priority"
|
||||||
CONF_PROJECT = "project"
|
CONF_PROJECT = "project"
|
||||||
CONF_PROTOCOL = "protocol"
|
CONF_PROTOCOL = "protocol"
|
||||||
|
CONF_PUBLISH_INITIAL_STATE = "publish_initial_state"
|
||||||
CONF_PULL_MODE = "pull_mode"
|
CONF_PULL_MODE = "pull_mode"
|
||||||
CONF_PULLDOWN = "pulldown"
|
CONF_PULLDOWN = "pulldown"
|
||||||
CONF_PULLUP = "pullup"
|
CONF_PULLUP = "pullup"
|
||||||
|
|
Loading…
Reference in a new issue