binary_sensor for switch state (#7819)
Some checks are pending
CI / CI Status (push) Blocked by required conditions
CI / Check flake8 (push) Blocked by required conditions
CI / Check pylint (push) Blocked by required conditions
CI / Create common environment (push) Waiting to run
CI / Check black (push) Blocked by required conditions
CI / Check pyupgrade (push) Blocked by required conditions
CI / Run script/ci-custom (push) Blocked by required conditions
CI / Run pytest (push) Blocked by required conditions
CI / Check clang-format (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 IDF (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP8266 (push) Blocked by required conditions
CI / list-components (push) Blocked by required conditions
CI / Component test (push) Blocked by required conditions
CI / Split components for testing into 20 groups maximum (push) Blocked by required conditions
CI / Test split components (push) Blocked by required conditions
YAML lint / yamllint (push) Waiting to run

This commit is contained in:
Samuel Sieb 2024-11-24 21:24:23 -10:00 committed by GitHub
parent 71496574e9
commit c49f7293fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 98 additions and 0 deletions

View file

@ -408,6 +408,7 @@ esphome/components/substitutions/* @esphome/core
esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931
esphome/components/switch/* @esphome/core
esphome/components/switch/binary_sensor/* @ssieb
esphome/components/t6615/* @tylermenezes
esphome/components/tc74/* @sethgirvan
esphome/components/tca9548a/* @andreashergert1984

View file

@ -0,0 +1,31 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_SOURCE_ID
from .. import Switch, switch_ns
CODEOWNERS = ["@ssieb"]
SwitchBinarySensor = switch_ns.class_(
"SwitchBinarySensor", binary_sensor.BinarySensor, cg.Component
)
CONFIG_SCHEMA = (
binary_sensor.binary_sensor_schema(SwitchBinarySensor)
.extend(
{
cv.Required(CONF_SOURCE_ID): cv.use_id(Switch),
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config)
source = await cg.get_variable(config[CONF_SOURCE_ID])
cg.add(var.set_source(source))

View file

@ -0,0 +1,17 @@
#include "switch_binary_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace switch_ {
static const char *const TAG = "switch.binary_sensor";
void SwitchBinarySensor::setup() {
source_->add_on_state_callback([this](bool value) { this->publish_state(value); });
this->publish_state(source_->state);
}
void SwitchBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Switch Binary Sensor", this); }
} // namespace switch_
} // namespace esphome

View file

@ -0,0 +1,22 @@
#pragma once
#include "../switch.h"
#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome {
namespace switch_ {
class SwitchBinarySensor : public binary_sensor::BinarySensor, public Component {
public:
void set_source(Switch *source) { source_ = source; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
protected:
Switch *source_;
};
} // namespace switch_
} // namespace esphome

View file

@ -0,0 +1,11 @@
binary_sensor:
- platform: switch
id: some_binary_sensor
name: "Template Switch State"
source_id: the_switch
switch:
- platform: template
name: "Template Switch"
id: the_switch
optimistic: true

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View file

@ -0,0 +1,2 @@
packages:
common: !include common.yaml