From adf2a463fd60c818aeeeee520f1cf7ac19095da8 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 31 Oct 2019 21:03:57 +0100 Subject: [PATCH] Fix some binary_sensor not having an initial state (#819) Fixes https://github.com/home-assistant/home-assistant/issues/28384 --- esphome/components/binary_sensor/__init__.py | 1 + esphome/components/binary_sensor/binary_sensor.h | 7 ++++++- esphome/components/ble_presence/ble_presence_device.h | 2 +- esphome/components/nextion/nextion.h | 2 +- esphome/components/rdm6300/rdm6300.h | 2 +- esphome/components/remote_base/remote_base.h | 4 ++-- esphome/components/status/status_binary_sensor.cpp | 2 +- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index c391e12895..c082e2e9af 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -23,6 +23,7 @@ IS_PLATFORM_COMPONENT = True binary_sensor_ns = cg.esphome_ns.namespace('binary_sensor') BinarySensor = binary_sensor_ns.class_('BinarySensor', cg.Nameable) +BinarySensorInitiallyOff = binary_sensor_ns.class_('BinarySensorInitiallyOff', BinarySensor) BinarySensorPtr = BinarySensor.operator('ptr') # Triggers diff --git a/esphome/components/binary_sensor/binary_sensor.h b/esphome/components/binary_sensor/binary_sensor.h index 577e87258c..f91c93c424 100644 --- a/esphome/components/binary_sensor/binary_sensor.h +++ b/esphome/components/binary_sensor/binary_sensor.h @@ -67,7 +67,7 @@ class BinarySensor : public Nameable { void send_state_internal(bool state, bool is_initial); /// Return whether this binary sensor has outputted a state. - bool has_state() const; + virtual bool has_state() const; virtual bool is_status_binary_sensor() const; @@ -86,5 +86,10 @@ class BinarySensor : public Nameable { Deduplicator publish_dedup_; }; +class BinarySensorInitiallyOff : public BinarySensor { + public: + bool has_state() const override { return true; } +}; + } // namespace binary_sensor } // namespace esphome diff --git a/esphome/components/ble_presence/ble_presence_device.h b/esphome/components/ble_presence/ble_presence_device.h index 9d7ec83bc7..e721db7dcd 100644 --- a/esphome/components/ble_presence/ble_presence_device.h +++ b/esphome/components/ble_presence/ble_presence_device.h @@ -9,7 +9,7 @@ namespace esphome { namespace ble_presence { -class BLEPresenceDevice : public binary_sensor::BinarySensor, +class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff, public esp32_ble_tracker::ESPBTDeviceListener, public Component { public: diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 92b41a88af..bd37e241e9 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -394,7 +394,7 @@ class Nextion : public PollingComponent, public uart::UARTDevice { bool wait_for_ack_{true}; }; -class NextionTouchComponent : public binary_sensor::BinarySensor { +class NextionTouchComponent : public binary_sensor::BinarySensorInitiallyOff { public: void set_page_id(uint8_t page_id) { page_id_ = page_id; } void set_component_id(uint8_t component_id) { component_id_ = component_id; } diff --git a/esphome/components/rdm6300/rdm6300.h b/esphome/components/rdm6300/rdm6300.h index a67b6e7ce8..13df400754 100644 --- a/esphome/components/rdm6300/rdm6300.h +++ b/esphome/components/rdm6300/rdm6300.h @@ -28,7 +28,7 @@ class RDM6300Component : public Component, public uart::UARTDevice { uint32_t last_id_{0}; }; -class RDM6300BinarySensor : public binary_sensor::BinarySensor { +class RDM6300BinarySensor : public binary_sensor::BinarySensorInitiallyOff { public: void set_id(uint32_t id) { id_ = id; } diff --git a/esphome/components/remote_base/remote_base.h b/esphome/components/remote_base/remote_base.h index 6035e2fd57..36be25add7 100644 --- a/esphome/components/remote_base/remote_base.h +++ b/esphome/components/remote_base/remote_base.h @@ -267,11 +267,11 @@ class RemoteReceiverBase : public RemoteComponentBase { uint8_t tolerance_{25}; }; -class RemoteReceiverBinarySensorBase : public binary_sensor::BinarySensor, +class RemoteReceiverBinarySensorBase : public binary_sensor::BinarySensorInitiallyOff, public Component, public RemoteReceiverListener { public: - explicit RemoteReceiverBinarySensorBase() : BinarySensor() {} + explicit RemoteReceiverBinarySensorBase() : BinarySensorInitiallyOff() {} void dump_config() override; virtual bool matches(RemoteReceiveData src) = 0; bool on_receive(RemoteReceiveData src) override { diff --git a/esphome/components/status/status_binary_sensor.cpp b/esphome/components/status/status_binary_sensor.cpp index 7fbeb8c171..90ac1faad7 100644 --- a/esphome/components/status/status_binary_sensor.cpp +++ b/esphome/components/status/status_binary_sensor.cpp @@ -30,7 +30,7 @@ void StatusBinarySensor::loop() { this->publish_state(status); } -void StatusBinarySensor::setup() { this->publish_state(false); } +void StatusBinarySensor::setup() { this->publish_initial_state(false); } void StatusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Status Binary Sensor", this); } } // namespace status