From 5776e70d7c464f271db51cd41823f7833b8dcfc9 Mon Sep 17 00:00:00 2001 From: Peter Kuehne Date: Tue, 14 Jul 2020 16:59:03 +0100 Subject: [PATCH] Bug/fix internal flag in binary sensor (#1136) --- esphome/__main__.py | 7 ++++++ esphome/components/binary_sensor/__init__.py | 2 +- .../binary_sensor/test_binary_sensor.py | 24 +++++++++++++++++++ .../binary_sensor/test_binary_sensor.yaml | 18 ++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/component_tests/binary_sensor/test_binary_sensor.py create mode 100644 tests/component_tests/binary_sensor/test_binary_sensor.yaml diff --git a/esphome/__main__.py b/esphome/__main__.py index 27262355d6..a961df8ada 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -131,6 +131,11 @@ def wrap_to_code(name, comp): def write_cpp(config): + generate_cpp_contents(config) + return write_cpp_file() + + +def generate_cpp_contents(config): _LOGGER.info("Generating C++ source...") for name, component, conf in iter_components(CORE.config): @@ -140,6 +145,8 @@ def write_cpp(config): CORE.flush_tasks() + +def write_cpp_file(): writer.write_platformio_project() code_s = indent(CORE.cpp_main_section) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 7c78c3a369..2b5dd302c4 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -224,7 +224,7 @@ BINARY_SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend({ def setup_binary_sensor_core_(var, config): cg.add(var.set_name(config[CONF_NAME])) if CONF_INTERNAL in config: - cg.add(var.set_internal(CONF_INTERNAL)) + cg.add(var.set_internal(config[CONF_INTERNAL])) if CONF_DEVICE_CLASS in config: cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) if CONF_INVERTED in config: diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.py b/tests/component_tests/binary_sensor/test_binary_sensor.py new file mode 100644 index 0000000000..8f3cd3aeda --- /dev/null +++ b/tests/component_tests/binary_sensor/test_binary_sensor.py @@ -0,0 +1,24 @@ +""" Tests for the binary sensor component """ + +from esphome.core import CORE +from esphome.config import read_config +from esphome.__main__ import generate_cpp_contents + + +def test_binary_sensor_config_value_internal_set(): + """ + Test that the "internal" config value is correctly set + """ + # Given + CORE.config_path = "tests/component_tests/binary_sensor/test_binary_sensor.yaml" + CORE.config = read_config({}) + + # When + generate_cpp_contents(CORE.config) + # print(CORE.cpp_main_section) + + # Then + assert "bs_1->set_internal(true);" in CORE.cpp_main_section + assert "bs_2->set_internal(false);" in CORE.cpp_main_section + + CORE.reset() diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.yaml b/tests/component_tests/binary_sensor/test_binary_sensor.yaml new file mode 100644 index 0000000000..912ae115eb --- /dev/null +++ b/tests/component_tests/binary_sensor/test_binary_sensor.yaml @@ -0,0 +1,18 @@ +esphome: + name: test + platform: ESP8266 + board: d1_mini_lite + +binary_sensor: + - platform: gpio + id: bs_1 + name: "test bs1" + internal: true + pin: + number: D0 + - platform: gpio + id: bs_2 + name: "test bs2" + internal: false + pin: + number: D1