diff --git a/esphome/codegen.py b/esphome/codegen.py index 883d5f8636..30f9ce6d2f 100644 --- a/esphome/codegen.py +++ b/esphome/codegen.py @@ -9,7 +9,7 @@ # pylint: disable=unused-import from esphome.cpp_generator import ( # noqa - Expression, RawExpression, TemplateArguments, + Expression, RawExpression, RawStatement, TemplateArguments, StructInitializer, ArrayInitializer, safe_exp, Statement, progmem_array, statement, variable, Pvariable, new_Pvariable, add, add_global, add_library, add_build_flag, add_define, diff --git a/esphome/components/ble_ibeacon/__init__.py b/esphome/components/ble_ibeacon/__init__.py deleted file mode 100644 index dac5b7c46f..0000000000 --- a/esphome/components/ble_ibeacon/__init__.py +++ /dev/null @@ -1,18 +0,0 @@ -import esphome.config_validation as cv -import esphome.codegen as cg -from esphome.components import esp32_ble_tracker -from esphome.const import CONF_ID - -DEPENDENCIES = ['esp32_ble_tracker'] -ble_ibeacon_ns = cg.esphome_ns.namespace('ble_ibeacon') -IBeaconListener = ble_ibeacon_ns.class_('IBeaconListener', esp32_ble_tracker.ESPBTDeviceListener) - - -CONFIG_SCHEMA = cv.Schema({ - cv.GenerateID(): cv.declare_id(IBeaconListener), -}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) - - -def to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - yield esp32_ble_tracker.register_ble_device(var, config) diff --git a/esphome/components/custom/sensor/__init__.py b/esphome/components/custom/sensor/__init__.py index 24dcdac997..726a65f9cb 100644 --- a/esphome/components/custom/sensor/__init__.py +++ b/esphome/components/custom/sensor/__init__.py @@ -21,5 +21,4 @@ def to_code(config): var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_SENSORS]): sens = cg.Pvariable(conf[CONF_ID], var.get_sensor(i)) - cg.add(sens.set_name(conf[CONF_NAME])) yield sensor.register_sensor(sens, conf) diff --git a/esphome/components/custom/switch/__init__.py b/esphome/components/custom/switch/__init__.py index b0da14b4f1..6fcb5d6678 100644 --- a/esphome/components/custom/switch/__init__.py +++ b/esphome/components/custom/switch/__init__.py @@ -24,6 +24,5 @@ def to_code(config): rhs = CustomSwitchConstructor(template_) var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_SWITCHES]): - switch_ = cg.new_Pvariable(conf[CONF_ID], var.get_switch(i)) - cg.add(switch_.set_name(conf[CONF_NAME])) + switch_ = cg.Pvariable(conf[CONF_ID], var.get_switch(i)) yield switch.register_switch(switch_, conf) diff --git a/esphome/components/custom/text_sensor/__init__.py b/esphome/components/custom/text_sensor/__init__.py index 40b8be8d76..42bcff04bb 100644 --- a/esphome/components/custom/text_sensor/__init__.py +++ b/esphome/components/custom/text_sensor/__init__.py @@ -24,6 +24,5 @@ def to_code(config): var = cg.variable(config[CONF_ID], rhs) for i, conf in enumerate(config[CONF_TEXT_SENSORS]): - text = cg.new_Pvariable(conf[CONF_ID], var.get_text_sensor(i)) - cg.add(text.set_name(conf[CONF_NAME])) + text = cg.Pvariable(conf[CONF_ID], var.get_text_sensor(i)) yield text_sensor.register_text_sensor(text, conf) diff --git a/esphome/components/esp32_ble_tracker/__init__.py b/esphome/components/esp32_ble_tracker/__init__.py index 05ca748a99..5a4862f733 100644 --- a/esphome/components/esp32_ble_tracker/__init__.py +++ b/esphome/components/esp32_ble_tracker/__init__.py @@ -4,7 +4,7 @@ from esphome.const import CONF_ID, CONF_SCAN_INTERVAL, ESP_PLATFORM_ESP32 from esphome.core import coroutine ESP_PLATFORMS = [ESP_PLATFORM_ESP32] -AUTO_LOAD = ['xiaomi_ble', 'ble_ibeacon'] +AUTO_LOAD = ['xiaomi_ble'] CONF_ESP32_BLE_ID = 'esp32_ble_id' esp32_ble_tracker_ns = cg.esphome_ns.namespace('esp32_ble_tracker') diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h index a2c68063e7..f1bcada621 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h @@ -33,6 +33,7 @@ class ESPBTUUID { class ESPBLEiBeacon { public: + ESPBLEiBeacon() { memset(&this->beacon_data_, 0, sizeof(this->beacon_data_)); } ESPBLEiBeacon(const uint8_t *data); static optional from_manufacturer_data(const std::string &data); @@ -98,9 +99,7 @@ class ESPBTDeviceListener { public: virtual void on_scan_end() {} virtual bool parse_device(const ESPBTDevice &device) = 0; - void set_parent(ESP32BLETracker *parent) { - parent_ = parent; - } + void set_parent(ESP32BLETracker *parent) { parent_ = parent; } protected: ESP32BLETracker *parent_{nullptr}; diff --git a/esphome/core_config.py b/esphome/core_config.py index 9a636a28da..8d2f83c4f1 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -152,7 +152,7 @@ def add_includes(includes): for include in includes: path = CORE.relative_config_path(include) res = os.path.relpath(path, CORE.relative_build_path('src')).replace(os.path.sep, '/') - cg.add_global(cg.RawExpression(u'#include "{}"'.format(res))) + cg.add_global(cg.RawStatement(u'#include "{}"'.format(res))) @coroutine_with_priority(100.0) diff --git a/script/lint-cpp b/script/lint-cpp index c38351b606..b1766d8a28 100755 --- a/script/lint-cpp +++ b/script/lint-cpp @@ -12,5 +12,5 @@ fi set -x -script/clang-tidy.py -c --fix +script/clang-tidy.py -c --fix --all-headers script/clang-format.py -c -i diff --git a/tests/custom.h b/tests/custom.h new file mode 100644 index 0000000000..be1e91fb94 --- /dev/null +++ b/tests/custom.h @@ -0,0 +1,38 @@ + +class CustomSensor : public Component, public Sensor { + public: + void loop() override { + publish_state(42.0); + } +}; + +class CustomTextSensor : public Component, public TextSensor { + public: + void loop() override { + publish_state("Hello World"); + } +}; + +class CustomBinarySensor : public Component, public BinarySensor { + public: + void loop() override { + publish_state(false); + } +}; + +class CustomSwitch : public Switch { + protected: + void write_state(bool state) override { + ESP_LOGD("custom_switch", "Setting %s", ONOFF(state)); + } +}; + +class CustomComponent : public PollingComponent { + public: + void setup() override { + ESP_LOGD("custom_component", "Setup"); + } + void update() override { + ESP_LOGD("custom_component", "Update"); + } +}; diff --git a/tests/test3.yaml b/tests/test3.yaml index 3e61e78acf..2eedb25fc5 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -7,6 +7,8 @@ esphome: - wait_until: - api.connected - wifi.connected + includes: + - custom.h substitutions: devicename: test3 @@ -65,6 +67,7 @@ ota: logger: hardware_uart: UART1 level: DEBUG + esp8266_store_log_strings_in_flash: false web_server: @@ -124,7 +127,8 @@ sensor: gain: 60x - platform: custom lambda: |- - auto s = new sensor::Sensor(); + auto s = new CustomSensor(); + App.register_component(s); return {s}; sensors: - id: custom_sensor @@ -187,9 +191,10 @@ binary_sensor: name: TTP229 LSF Test - platform: custom lambda: |- - auto s = new binary_sensor::BinarySensor(); + auto s = new CustomBinarySensor(); + App.register_component(s); return {s}; - sensors: + binary_sensors: - id: custom_binary_sensor name: Custom Binary Sensor @@ -225,6 +230,14 @@ text_sensor: - platform: homeassistant entity_id: sensor.hello_world2 id: ha_hello_world2 + - platform: custom + lambda: |- + auto s = new CustomTextSensor(); + App.register_component(s); + return {s}; + text_sensors: + - id: custom_text_sensor + name: Custom Text Sensor script: - id: my_script @@ -249,12 +262,18 @@ switch: interlock: *interlock - platform: custom lambda: |- - auto s = new switch::Switch(); + auto s = new CustomSwitch(); return {s}; - sensors: + switches: - id: custom_switch name: Custom Switch +custom_component: + lambda: |- + auto s = new CustomComponent(); + s->set_update_interval(15000); + return {s}; + stepper: - platform: uln2003 id: my_stepper