mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 01:08:03 +01:00
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
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:
parent
71496574e9
commit
c49f7293fe
13 changed files with 98 additions and 0 deletions
|
@ -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
|
||||
|
|
31
esphome/components/switch/binary_sensor/__init__.py
Normal file
31
esphome/components/switch/binary_sensor/__init__.py
Normal 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))
|
|
@ -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
|
|
@ -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
|
11
tests/components/switch/common.yaml
Normal file
11
tests/components/switch/common.yaml
Normal 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
|
2
tests/components/switch/test.bk72xx-ard.yaml
Normal file
2
tests/components/switch/test.bk72xx-ard.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp32-ard.yaml
Normal file
2
tests/components/switch/test.esp32-ard.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp32-c3-ard.yaml
Normal file
2
tests/components/switch/test.esp32-c3-ard.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp32-c3-idf.yaml
Normal file
2
tests/components/switch/test.esp32-c3-idf.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp32-idf.yaml
Normal file
2
tests/components/switch/test.esp32-idf.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp32-s3-idf.yaml
Normal file
2
tests/components/switch/test.esp32-s3-idf.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.esp8266-ard.yaml
Normal file
2
tests/components/switch/test.esp8266-ard.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
2
tests/components/switch/test.rp2040-ard.yaml
Normal file
2
tests/components/switch/test.rp2040-ard.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
packages:
|
||||
common: !include common.yaml
|
Loading…
Reference in a new issue