From a1b6a91642dd326ba87815d37718b04b527f200e Mon Sep 17 00:00:00 2001 From: Aalian Khan <39784853+AalianKhan@users.noreply.github.com> Date: Mon, 13 May 2019 14:51:21 -0400 Subject: [PATCH 01/22] Update nextion.h --- esphome/components/nextion/nextion.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index d8fe3762c9..91aaecb96a 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -23,6 +23,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the text of a component to a static string. * @param component The component name. * @param text The static text to set. + * Example: `it.set_component_text("textview", "Hello World!");` + * Component named `textview` `txt` value has been changed to `Hello World`. */ void set_component_text(const char *component, const char *text); /** @@ -30,18 +32,25 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param component The component name. * @param format The printf-style format string. * @param ... The arguments to the format. + * Example: `it.set_component_text_printf("textview", "The uptime is: %.1f", id(uptime_sensor).state);` + * Component named `textview` has been changed to `The uptime is:` Then the value of `uptime_sensor`. + * For example when `uptime_sensor` = 506, then, `The uptime is: 506` will be displayed. */ void set_component_text_printf(const char *component, const char *format, ...) __attribute__((format(printf, 3, 4))); /** * Set the integer value of a component * @param component The component name. * @param value The value to set. + * Example: `it.set_component_value("gauge", 50);` + * Component named `gauge` has changed the `val`to 50. */ void set_component_value(const char *component, int value); /** * Set the picture of an image component. * @param component The component name. * @param value The picture name. + * Example: `it.set_component_picture("pic", "4");` + * The picture component named `pic`, has changed the image which has the ID `4`, Which was set in the Nextion editor. */ void set_component_picture(const char *component, const char *picture) { this->send_command_printf("%s.val=%s", component, picture); @@ -50,12 +59,18 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the background color of a component. * @param component The component name. * @param color The color (as a string). + * Example: `it.set_component_background_color("button", "17013"); + * The background color of component named `button`, has been changed to blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void set_component_background_color(const char *component, const char *color); /** * Set the pressed background color of a component. * @param component The component name. * @param color The color (as a string). + * Example: `it.set_component_pressed_background_color("button", "17013"` + * The pressed background color of the component named `button` has been changed to blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ void set_component_pressed_background_color(const char *component, const char *color); /** From 6fff2e5957369b6b8928e13eeb2654b2e4840975 Mon Sep 17 00:00:00 2001 From: Aalian Khan <39784853+AalianKhan@users.noreply.github.com> Date: Mon, 13 May 2019 21:24:45 -0400 Subject: [PATCH 02/22] Added more examples --- esphome/components/nextion/nextion.h | 42 ++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 91aaecb96a..b5bc86ec62 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -68,8 +68,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the pressed background color of a component. * @param component The component name. * @param color The color (as a string). - * Example: `it.set_component_pressed_background_color("button", "17013"` - * The pressed background color of the component named `button` has been changed to blue. + * Example: `it.set_component_pressed_background_color("button", "17013"); + * The pressed background color of the component named `button` has been changed to blue. So when the button is pressed, the background color will change to blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ void set_component_pressed_background_color(const char *component, const char *color); @@ -77,12 +77,18 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the font color of a component. * @param component The component name. * @param color The color (as a string). + * Example: `it.set_component_font_color("textview", "17013); + * The font color of component named `textview` has been changed to blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ void set_component_font_color(const char *component, const char *color); /** * Set the pressed font color of a component. * @param component The component name. * @param color The color (as a string). + * Example: `it.set_component_pressed_font_color("button", "17013");` + * The pressed font color for component named button has been changed to blue. So when `button` is pressed, the font color will change to blue + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ void set_component_pressed_font_color(const char *component, const char *color); /** @@ -90,12 +96,16 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param component The component name. * @param x The x coordinate. * @param y The y coordinate. + * Example: `it.set_component_coordinates("pic", 55, 100);` + * The component named `pic` has moved to the x coordinate `55` and y coordinate `100`. */ void set_component_coordinates(const char *component, int x, int y); /** * Set the font id for a component. * @param component The component name. * @param font_id The ID of the font (number). + * Example: `it.set_component_font("textview", "3");` + * Changes the font of the component named `textveiw`. Font IDs are set in the Nextion Editor. */ void set_component_font(const char *component, uint8_t font_id); #ifdef USE_TIME @@ -109,26 +119,36 @@ class Nextion : public PollingComponent, public uart::UARTDevice { /** * Show the page with a given name. * @param page The name of the page. + * Example: `it.goto_page("main");` + * Switches to the page named `main`. Pages are named in the Nextion Editor. */ void goto_page(const char *page); /** * Hide a component. * @param component The component name. + * Example: `hide_component("button");` + * Hides the component named `button`. */ void hide_component(const char *component); /** * Show a component. * @param component The component name. + * Example: `show_component("button");` + * Shows the component named `button`. */ void show_component(const char *component); /** * Enable touch for a component. * @param component The component name. + * Example: `enable_component_touch("button");` + * Enables touch for component named `button`. */ void enable_component_touch(const char *component); /** * Disable touch for a component. * @param component The component name. + * Examlple: `disable_component_touch("button");` + * Disables touch for component named `button`. */ void disable_component_touch(const char *component); /** @@ -143,6 +163,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param picture_id The picture id. * @param x1 The x coordinate. * @param y1 The y coordniate. + * Example: `display_picture(2, 15, 25);` + * Displays the picture who has the id `2` at the x coordinates `15` and y coordinates `25`. */ void display_picture(int picture_id, int x_start, int y_start); /** @@ -152,6 +174,9 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param width The width to draw. * @param height The height to draw. * @param color The color to draw with (as a string). + * Example: `fill_area(50, 50, 100, 100, "17013");` + * Fills an area that starts at x coordiante `50` and y coordinate `50` with a height of `100` and width of `100` with the color of blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ void fill_area(int x1, int y1, int width, int height, const char *color); /** @@ -161,6 +186,9 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param x2 The ending x coordinate. * @param y2 The ending y coordinate. * @param color The color to draw with (as a string). + * Example: `it.line(50, 50, 75, 75, "17013");` + * Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate `75` with the color of blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void line(int x1, int y1, int x2, int y2, const char *color); /** @@ -170,6 +198,9 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param width The width of the rectangle. * @param height The height of the rectangle. * @param color The color to draw with (as a string). + * Example: `it.rectangle(25, 35, 40, 50, "17013");` + * Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a length of `50` with color of blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void rectangle(int x1, int y1, int width, int height, const char *color); /** @@ -186,17 +217,24 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param center_y The center y coordinate. * @param radius The circle radius. * @param color The color to draw with (as a string). + * Example: `it.filled_cricle(25, 25, 10, "17013");` + * Makes a filled circle at the x cordinates `25` and y coordinate `25` with a radius of `10` with a color of blue. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void filled_circle(int center_x, int center_y, int radius, const char *color); /** Set the brightness of the backlight. * * @param brightness The brightness, from 0 to 100. + * Example: `it.set_backlight_brightness(30);` + * Changes the brightness of the display to 30%. */ void set_backlight_brightness(uint8_t brightness); /** * Set the touch sleep timeout of the display. * @param timeout Timeout in seconds. + * Example: `it.set_touch_sleep_timeout(30);` + * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up `thup`. */ void set_touch_sleep_timeout(uint16_t timeout); From a323679771a49949fa3720dcd3be378300bd5117 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 16 May 2019 18:34:51 +0200 Subject: [PATCH 03/22] Revert upgrade ESP8266 core Binary size increased by 30kb --- .gitlab-ci.yml | 4 ++-- docker/Dockerfile | 2 +- esphome/components/ota/ota_component.cpp | 1 - esphome/core_config.py | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index accc647c28..94116bcee1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,11 +41,11 @@ stages: - | if [[ "${IS_HASSIO}" == "YES" ]]; then - BUILD_FROM=esphome/esphome-hassio-base-${BUILD_ARCH}:1.6.0 + BUILD_FROM=esphome/esphome-hassio-base-${BUILD_ARCH}:1.5.1 BUILD_TO=esphome/esphome-hassio-${BUILD_ARCH} DOCKERFILE=docker/Dockerfile.hassio else - BUILD_FROM=esphome/esphome-base-${BUILD_ARCH}:1.6.0 + BUILD_FROM=esphome/esphome-base-${BUILD_ARCH}:1.5.1 if [[ "${BUILD_ARCH}" == "amd64" ]]; then BUILD_TO=esphome/esphome else diff --git a/docker/Dockerfile b/docker/Dockerfile index f70d3e35a5..f844fa741e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILD_FROM=esphome/esphome-base-amd64:1.6.0 +ARG BUILD_FROM=esphome/esphome-base-amd64:1.5.1 FROM ${BUILD_FROM} COPY . . diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index b69393c998..ee2d67c85e 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -4,7 +4,6 @@ #include "esphome/core/helpers.h" #include "esphome/core/application.h" #include "esphome/core/util.h" -//#include "esphome/components/status_led.h" #include #include diff --git a/esphome/core_config.py b/esphome/core_config.py index 71942c05e0..5230f723af 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -48,7 +48,7 @@ PLATFORMIO_ESP8266_LUT = { '2.4.1': 'espressif8266@1.7.3', '2.4.0': 'espressif8266@1.6.0', '2.3.0': 'espressif8266@1.5.0', - 'RECOMMENDED': 'espressif8266@2.1.0', + 'RECOMMENDED': 'espressif8266@1.8.0', 'LATEST': 'espressif8266', 'DEV': ARDUINO_VERSION_ESP8266_DEV, } From 999c1a5357196b88b7c57ea581b36f72fa96f746 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 21 May 2019 12:23:38 +0200 Subject: [PATCH 04/22] Warn if not registered properly --- esphome/components/binary_sensor/__init__.py | 1 + esphome/components/fan/__init__.py | 1 + esphome/components/template/cover/__init__.py | 2 +- esphome/components/time/__init__.py | 1 + esphome/components/uart/switch/__init__.py | 2 +- esphome/config.py | 3 +++ esphome/core.py | 9 +++++++++ esphome/core_config.py | 8 +++++--- esphome/cpp_helpers.py | 9 ++++++++- 9 files changed, 30 insertions(+), 6 deletions(-) diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 70a6ce0c58..9f92207b19 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -254,6 +254,7 @@ def setup_binary_sensor_core_(var, config): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var, timings) if CONF_INVALID_COOLDOWN in conf: cg.add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN])) + yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) for conf in config.get(CONF_ON_STATE, []): diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index ffddf83acc..f3bc91440e 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -63,6 +63,7 @@ def register_fan(var, config): if not CORE.has_id(config[CONF_ID]): var = cg.Pvariable(config[CONF_ID], var) cg.add(cg.App.register_fan(var)) + yield cg.register_component(var, config) yield setup_fan_core_(var, config) diff --git a/esphome/components/template/cover/__init__.py b/esphome/components/template/cover/__init__.py index 33b3b62870..808318ac81 100644 --- a/esphome/components/template/cover/__init__.py +++ b/esphome/components/template/cover/__init__.py @@ -7,7 +7,7 @@ from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_CURRENT_OP CONF_STATE, CONF_STOP_ACTION from .. import template_ns -TemplateCover = template_ns.class_('TemplateCover', cover.Cover) +TemplateCover = template_ns.class_('TemplateCover', cover.Cover, cg.Component) TemplateCoverRestoreMode = template_ns.enum('TemplateCoverRestoreMode') RESTORE_MODES = { diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index 82dc750486..a81255a254 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -285,6 +285,7 @@ def setup_time_core_(time_var, config): days_of_week = conf.get(CONF_DAYS_OF_WEEK, [x for x in range(1, 8)]) cg.add(trigger.add_days_of_week(days_of_week)) + yield cg.register_component(trigger, conf) yield automation.build_automation(trigger, [], conf) diff --git a/esphome/components/uart/switch/__init__.py b/esphome/components/uart/switch/__init__.py index 35e7877cc3..dae63a2add 100644 --- a/esphome/components/uart/switch/__init__.py +++ b/esphome/components/uart/switch/__init__.py @@ -8,7 +8,7 @@ from .. import uart_ns DEPENDENCIES = ['uart'] -UARTSwitch = uart_ns.class_('UARTSwitch', switch.Switch, uart.UARTDevice) +UARTSwitch = uart_ns.class_('UARTSwitch', switch.Switch, uart.UARTDevice, cg.Component) def validate_data(value): diff --git a/esphome/config.py b/esphome/config.py index ad86811f19..ac22db37ad 100644 --- a/esphome/config.py +++ b/esphome/config.py @@ -322,6 +322,7 @@ def iter_ids(config, path=None): def do_id_pass(result): # type: (Config) -> None from esphome.cpp_generator import MockObjClass + from esphome.cpp_types import Component declare_ids = [] # type: List[Tuple[core.ID, ConfigPath]] searching_ids = [] # type: List[Tuple[core.ID, ConfigPath]] @@ -340,6 +341,8 @@ def do_id_pass(result): # type: (Config) -> None # Resolve default ids after manual IDs for id, _ in declare_ids: id.resolve([v[0].id for v in declare_ids]) + if isinstance(id.type, MockObjClass) and id.type.inherits_from(Component): + CORE.component_ids.add(id.id) # Check searched IDs for id, path in searching_ids: diff --git a/esphome/core.py b/esphome/core.py index f2bc4fdf2d..7aaf6b2c70 100644 --- a/esphome/core.py +++ b/esphome/core.py @@ -505,6 +505,8 @@ class EsphomeCore(object): self.active_coroutines = {} # type: Dict[int, Any] # A set of strings of names of loaded integrations, used to find namespace ID conflicts self.loaded_integrations = set() + # A set of component IDs to track what Component subclasses are declared + self.component_ids = set() def reset(self): self.dashboard = False @@ -525,6 +527,7 @@ class EsphomeCore(object): self.defines = set() self.active_coroutines = {} self.loaded_integrations = set() + self.component_ids = set() @property def address(self): # type: () -> str @@ -626,6 +629,12 @@ class EsphomeCore(object): _LOGGER.warning(u"Please file a bug report with your configuration.") if self.active_coroutines: raise EsphomeError() + if self.component_ids: + comps = u', '.join(u"'{}'".format(x) for x in self.component_ids) + _LOGGER.warning(u"Components %s were never registered. Please create a bug report", + comps) + _LOGGER.warning(u"with your configuration.") + raise EsphomeError() self.active_coroutines.clear() def add(self, expression): diff --git a/esphome/core_config.py b/esphome/core_config.py index 5230f723af..a2663b8db8 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -18,9 +18,11 @@ from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS _LOGGER = logging.getLogger(__name__) BUILD_FLASH_MODES = ['qio', 'qout', 'dio', 'dout'] -StartupTrigger = cg.esphome_ns.StartupTrigger -ShutdownTrigger = cg.esphome_ns.ShutdownTrigger -LoopTrigger = cg.esphome_ns.LoopTrigger +StartupTrigger = cg.esphome_ns.class_('StartupTrigger', cg.Component, automation.Trigger.template()) +ShutdownTrigger = cg.esphome_ns.class_('ShutdownTrigger', cg.Component, + automation.Trigger.template()) +LoopTrigger = cg.esphome_ns.class_('LoopTrigger', cg.Component, + automation.Trigger.template()) VERSION_REGEX = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$') diff --git a/esphome/cpp_helpers.py b/esphome/cpp_helpers.py index 8e310ac44b..fd79feec1c 100644 --- a/esphome/cpp_helpers.py +++ b/esphome/cpp_helpers.py @@ -1,8 +1,9 @@ from esphome.const import CONF_INVERTED, CONF_MODE, CONF_NUMBER, CONF_SETUP_PRIORITY, \ CONF_UPDATE_INTERVAL, CONF_TYPE_ID -from esphome.core import coroutine, ID +from esphome.core import coroutine, ID, CORE from esphome.cpp_generator import RawExpression, add, get_variable from esphome.cpp_types import App, GPIOPin +from esphome.py_compat import text_type @coroutine @@ -34,6 +35,12 @@ def register_component(var, config): :param var: The variable representing the component. :param config: The configuration for the component. """ + id_ = text_type(var.base) + if id_ not in CORE.component_ids: + raise ValueError(u"Component ID {} was not declared to inherit from Component, " + u"or was registered twice. Please create a bug report with your " + u"configuration.".format(id_)) + CORE.component_ids.remove(id_) if CONF_SETUP_PRIORITY in config: add(var.set_setup_priority(config[CONF_SETUP_PRIORITY])) if CONF_UPDATE_INTERVAL in config: From bd45f6bd8e7398477b04210a36b492e844cd6a5b Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 24 May 2019 16:20:28 +0200 Subject: [PATCH 05/22] Register components properly --- esphome/components/bang_bang/climate.py | 2 +- esphome/components/climate/climate_mode.cpp | 8 ++++---- esphome/components/deep_sleep/deep_sleep_component.cpp | 2 +- .../components/homeassistant/binary_sensor/__init__.py | 3 ++- esphome/components/homeassistant/sensor/__init__.py | 3 ++- esphome/components/mqtt/mqtt_climate.cpp | 8 ++++---- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/esphome/components/bang_bang/climate.py b/esphome/components/bang_bang/climate.py index 88828ef44f..4ef811c55d 100644 --- a/esphome/components/bang_bang/climate.py +++ b/esphome/components/bang_bang/climate.py @@ -7,7 +7,7 @@ from esphome.const import CONF_AWAY_CONFIG, CONF_COOL_ACTION, \ CONF_ID, CONF_IDLE_ACTION, CONF_SENSOR bang_bang_ns = cg.esphome_ns.namespace('bang_bang') -BangBangClimate = bang_bang_ns.class_('BangBangClimate', climate.Climate) +BangBangClimate = bang_bang_ns.class_('BangBangClimate', climate.Climate, cg.Component) BangBangClimateTargetTempConfig = bang_bang_ns.struct('BangBangClimateTargetTempConfig') CONFIG_SCHEMA = cv.All(climate.CLIMATE_SCHEMA.extend({ diff --git a/esphome/components/climate/climate_mode.cpp b/esphome/components/climate/climate_mode.cpp index 32d42b706f..07b97f4f33 100644 --- a/esphome/components/climate/climate_mode.cpp +++ b/esphome/components/climate/climate_mode.cpp @@ -6,13 +6,13 @@ namespace climate { const char *climate_mode_to_string(ClimateMode mode) { switch (mode) { case CLIMATE_MODE_OFF: - return "off"; + return "OFF"; case CLIMATE_MODE_AUTO: - return "auto"; + return "AUTO"; case CLIMATE_MODE_COOL: - return "cool"; + return "COOL"; case CLIMATE_MODE_HEAT: - return "heat"; + return "HEAT"; default: return "UNKNOWN"; } diff --git a/esphome/components/deep_sleep/deep_sleep_component.cpp b/esphome/components/deep_sleep/deep_sleep_component.cpp index ed14f3c824..5f6aaf24cd 100644 --- a/esphome/components/deep_sleep/deep_sleep_component.cpp +++ b/esphome/components/deep_sleep/deep_sleep_component.cpp @@ -19,7 +19,7 @@ void DeepSleepComponent::setup() { void DeepSleepComponent::dump_config() { ESP_LOGCONFIG(TAG, "Setting up Deep Sleep..."); if (this->sleep_duration_.has_value()) { - ESP_LOGCONFIG(TAG, " Sleep Duration: %llu ms", *this->sleep_duration_ / 1000); + ESP_LOGCONFIG(TAG, " Sleep Duration: %u ms", *this->sleep_duration_ / 1000); } if (this->run_duration_.has_value()) { ESP_LOGCONFIG(TAG, " Run Duration: %u ms", *this->run_duration_); diff --git a/esphome/components/homeassistant/binary_sensor/__init__.py b/esphome/components/homeassistant/binary_sensor/__init__.py index b78836f18f..88e2f2fcb2 100644 --- a/esphome/components/homeassistant/binary_sensor/__init__.py +++ b/esphome/components/homeassistant/binary_sensor/__init__.py @@ -6,7 +6,8 @@ from .. import homeassistant_ns DEPENDENCIES = ['api'] HomeassistantBinarySensor = homeassistant_ns.class_('HomeassistantBinarySensor', - binary_sensor.BinarySensor) + binary_sensor.BinarySensor, + cg.Component) CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(HomeassistantBinarySensor), diff --git a/esphome/components/homeassistant/sensor/__init__.py b/esphome/components/homeassistant/sensor/__init__.py index cd5e4a74e2..577efca79b 100644 --- a/esphome/components/homeassistant/sensor/__init__.py +++ b/esphome/components/homeassistant/sensor/__init__.py @@ -6,7 +6,8 @@ from .. import homeassistant_ns DEPENDENCIES = ['api'] -HomeassistantSensor = homeassistant_ns.class_('HomeassistantSensor', sensor.Sensor) +HomeassistantSensor = homeassistant_ns.class_('HomeassistantSensor', sensor.Sensor, + cg.Component) CONFIG_SCHEMA = sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 1).extend({ cv.GenerateID(): cv.declare_id(HomeassistantSensor), diff --git a/esphome/components/mqtt/mqtt_climate.cpp b/esphome/components/mqtt/mqtt_climate.cpp index 590a6db7b4..8085fbf0f2 100644 --- a/esphome/components/mqtt/mqtt_climate.cpp +++ b/esphome/components/mqtt/mqtt_climate.cpp @@ -24,12 +24,12 @@ void MQTTClimateComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryC JsonArray &modes = root.createNestedArray("modes"); // sort array for nice UI in HA if (traits.supports_mode(CLIMATE_MODE_AUTO)) - modes.add(climate_mode_to_string(CLIMATE_MODE_AUTO)); - modes.add(climate_mode_to_string(CLIMATE_MODE_OFF)); + modes.add("auto"); + modes.add("off"); if (traits.supports_mode(CLIMATE_MODE_COOL)) - modes.add(climate_mode_to_string(CLIMATE_MODE_COOL)); + modes.add("cool"); if (traits.supports_mode(CLIMATE_MODE_HEAT)) - modes.add(climate_mode_to_string(CLIMATE_MODE_HEAT)); + modes.add("heat"); if (traits.get_supports_two_point_target_temperature()) { // temperature_low_command_topic From aa7389432ed740bfaff7352f3c4d1db3ea4c261b Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 24 May 2019 16:34:01 +0200 Subject: [PATCH 06/22] Fixes --- esphome/components/ethernet/__init__.py | 3 ++- esphome/components/status/status_binary_sensor.cpp | 9 +++------ esphome/components/status/status_binary_sensor.h | 3 --- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py index 50a0d99d32..ce7422d05b 100644 --- a/esphome/components/ethernet/__init__.py +++ b/esphome/components/ethernet/__init__.py @@ -67,7 +67,7 @@ CONFIG_SCHEMA = cv.All(cv.Schema({ cv.Optional(CONF_USE_ADDRESS): cv.string_strict, cv.Optional('hostname'): cv.invalid("The hostname option has been removed in 1.11.0"), -}), validate) +}).extend(cv.COMPONENT_SCHEMA), validate) def manual_ip(config): @@ -84,6 +84,7 @@ def manual_ip(config): @coroutine_with_priority(60.0) def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) cg.add(var.set_phy_addr(config[CONF_PHY_ADDR])) cg.add(var.set_mdc_pin(config[CONF_MDC_PIN])) diff --git a/esphome/components/status/status_binary_sensor.cpp b/esphome/components/status/status_binary_sensor.cpp index 5485c6ebcd..d004c70b95 100644 --- a/esphome/components/status/status_binary_sensor.cpp +++ b/esphome/components/status/status_binary_sensor.cpp @@ -18,19 +18,16 @@ void StatusBinarySensor::loop() { bool status = network_is_connected(); #ifdef USE_MQTT if (mqtt::global_mqtt_client != nullptr) { - status = mqtt::global_mqtt_client->is_connected(); + status = status && mqtt::global_mqtt_client->is_connected(); } #endif #ifdef USE_API if (api::global_api_server != nullptr) { - status = api::global_api_server->is_connected(); + status = status && api::global_api_server->is_connected(); } #endif - if (this->last_status_ != status) { - this->publish_state(status); - this->last_status_ = status; - } + this->publish_state(status); } void StatusBinarySensor::setup() { this->publish_state(false); } void StatusBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "Status Binary Sensor", this); } diff --git a/esphome/components/status/status_binary_sensor.h b/esphome/components/status/status_binary_sensor.h index fa121291a1..08aa0fb32f 100644 --- a/esphome/components/status/status_binary_sensor.h +++ b/esphome/components/status/status_binary_sensor.h @@ -16,9 +16,6 @@ class StatusBinarySensor : public binary_sensor::BinarySensor, public Component float get_setup_priority() const override { return setup_priority::DATA; } bool is_status_binary_sensor() const override { return true; } - - protected: - bool last_status_{false}; }; } // namespace status From 422754ed63ac82072a69d5fb6146d3399d4bac84 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 24 May 2019 17:20:06 +0200 Subject: [PATCH 07/22] Lint --- esphome/components/deep_sleep/deep_sleep_component.cpp | 3 ++- script/clang-format | 4 ++-- script/clang-tidy | 4 ++-- script/helpers.py | 10 ++++++++++ script/lint-python | 8 +++++--- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/esphome/components/deep_sleep/deep_sleep_component.cpp b/esphome/components/deep_sleep/deep_sleep_component.cpp index 5f6aaf24cd..217c0cbf0d 100644 --- a/esphome/components/deep_sleep/deep_sleep_component.cpp +++ b/esphome/components/deep_sleep/deep_sleep_component.cpp @@ -19,7 +19,8 @@ void DeepSleepComponent::setup() { void DeepSleepComponent::dump_config() { ESP_LOGCONFIG(TAG, "Setting up Deep Sleep..."); if (this->sleep_duration_.has_value()) { - ESP_LOGCONFIG(TAG, " Sleep Duration: %u ms", *this->sleep_duration_ / 1000); + uint32_t duration = *this->sleep_duration_ / 1000; + ESP_LOGCONFIG(TAG, " Sleep Duration: %u ms", duration); } if (this->run_duration_.has_value()) { ESP_LOGCONFIG(TAG, " Run Duration: %u ms", *this->run_duration_); diff --git a/script/clang-format b/script/clang-format index d5792c783e..89a5acd746 100755 --- a/script/clang-format +++ b/script/clang-format @@ -13,7 +13,7 @@ import threading import click sys.path.append(os.path.dirname(__file__)) -from helpers import basepath, get_output, walk_files, filter_changed +from helpers import basepath, get_output, git_ls_files, filter_changed is_py2 = sys.version[0] == '2' @@ -83,7 +83,7 @@ def main(): return 1 files = [] - for path in walk_files(basepath): + for path in git_ls_files(): filetypes = ('.cpp', '.h', '.tcc') ext = os.path.splitext(path)[1] if ext in filetypes: diff --git a/script/clang-tidy b/script/clang-tidy index 124008a873..39df87df22 100755 --- a/script/clang-tidy +++ b/script/clang-tidy @@ -18,7 +18,7 @@ import threading sys.path.append(os.path.dirname(__file__)) from helpers import basepath, shlex_quote, get_output, build_compile_commands, \ - build_all_include, temp_header_file, walk_files, filter_changed + build_all_include, temp_header_file, git_ls_files, filter_changed is_py2 = sys.version[0] == '2' @@ -100,7 +100,7 @@ def main(): build_compile_commands() files = [] - for path in walk_files(basepath): + for path in git_ls_files(): filetypes = ('.cpp',) ext = os.path.splitext(path)[1] if ext in filetypes: diff --git a/script/helpers.py b/script/helpers.py index 8b37dce570..243dfde49e 100644 --- a/script/helpers.py +++ b/script/helpers.py @@ -126,3 +126,13 @@ def filter_changed(files): for c in files: print(" {}".format(c)) return files + + +def git_ls_files(): + command = ['git', 'ls-files', '-s'] + proc = subprocess.Popen(command, stdout=subprocess.PIPE) + output, err = proc.communicate() + lines = [x.split() for x in output.decode('utf-8').splitlines()] + return { + s[3].strip(): int(s[0]) for s in lines + } diff --git a/script/lint-python b/script/lint-python index cb702bf362..415efa0ebf 100755 --- a/script/lint-python +++ b/script/lint-python @@ -9,7 +9,7 @@ import re import sys sys.path.append(os.path.dirname(__file__)) -from helpers import basepath, get_output, walk_files, filter_changed +from helpers import get_output, git_ls_files, filter_changed def main(): @@ -21,10 +21,10 @@ def main(): args = parser.parse_args() files = [] - for path in walk_files(basepath): + for path in git_ls_files(): filetypes = ('.py',) ext = os.path.splitext(path)[1] - if ext in filetypes: + if ext in filetypes and path.startswith('esphome'): path = os.path.relpath(path, os.getcwd()) files.append(path) # Match against re @@ -35,6 +35,8 @@ def main(): files = filter_changed(files) files.sort() + if not files: + sys.exit(0) errors = collections.defaultdict(list) cmd = ['flake8'] + files From 16c2929bb401d4665155354751ff148751f16980 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 24 May 2019 19:32:59 +0200 Subject: [PATCH 08/22] Fix parse_float accepting invalid input --- esphome/core/helpers.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 769a7825da..c65ca919ba 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -257,7 +257,7 @@ std::string to_string(long double val) { optional parse_float(const std::string &str) { char *end; float value = ::strtof(str.c_str(), &end); - if (end == nullptr) + if (end == nullptr || end != str.end().base()) return {}; return value; } From 12ff280d3be3a12d295ace8548154d91eb319bc2 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 24 May 2019 23:08:04 +0200 Subject: [PATCH 09/22] Lint --- tests/custom.h | 24 ++++++------------------ tests/livingroom32.cpp | 6 ++---- tests/livingroom8266.cpp | 4 +--- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/tests/custom.h b/tests/custom.h index 0c30437b8c..7a5edbbf61 100644 --- a/tests/custom.h +++ b/tests/custom.h @@ -3,38 +3,26 @@ class CustomSensor : public Component, public Sensor { public: - void loop() override { - publish_state(42.0); - } + void loop() override { publish_state(42.0); } }; class CustomTextSensor : public Component, public TextSensor { public: - void loop() override { - publish_state("Hello World"); - } + void loop() override { publish_state("Hello World"); } }; class CustomBinarySensor : public Component, public BinarySensor { public: - void loop() override { - publish_state(false); - } + 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)); - } + 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"); - } + void setup() override { ESP_LOGD("custom_component", "Setup"); } + void update() override { ESP_LOGD("custom_component", "Update"); } }; diff --git a/tests/livingroom32.cpp b/tests/livingroom32.cpp index af4a713b6a..7005ec95e0 100644 --- a/tests/livingroom32.cpp +++ b/tests/livingroom32.cpp @@ -11,7 +11,7 @@ void setup() { App.init_ota()->start_safe_mode(); // LEDC is only available on ESP32! for the ESP8266, take a look at App.make_esp8266_pwm_output(). - auto *red = App.make_ledc_output(32); // on pin 32 + auto *red = App.make_ledc_output(32); // on pin 32 auto *green = App.make_ledc_output(33); auto *blue = App.make_ledc_output(34); App.make_rgb_light("Livingroom Light", red, green, blue); @@ -23,6 +23,4 @@ void setup() { App.setup(); } -void loop() { - App.loop(); -} +void loop() { App.loop(); } diff --git a/tests/livingroom8266.cpp b/tests/livingroom8266.cpp index cd43395d75..ad017ce73b 100644 --- a/tests/livingroom8266.cpp +++ b/tests/livingroom8266.cpp @@ -29,6 +29,4 @@ void setup() { App.setup(); } -void loop() { - App.loop(); -} +void loop() { App.loop(); } From df50b95e5aa936d37c8f939316863ee083852671 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 25 May 2019 09:02:18 +0200 Subject: [PATCH 10/22] Fix LEDC missing frequency/bit_depth Fixes https://github.com/esphome/issues/issues/338#issuecomment-495810954 --- esphome/components/ledc/output.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/esphome/components/ledc/output.py b/esphome/components/ledc/output.py index e5cb073ca9..c507465ff9 100644 --- a/esphome/components/ledc/output.py +++ b/esphome/components/ledc/output.py @@ -71,3 +71,5 @@ def to_code(config): yield output.register_output(var, config) if CONF_CHANNEL in config: cg.add(var.set_channel(config[CONF_CHANNEL])) + cg.add(var.set_frequency(config[CONF_FREQUENCY])) + cg.add(var.set_bit_depth(config[CONF_BIT_DEPTH])) From 9ed06444e168bb754f81ba3cfc2dbd34fd48c7d2 Mon Sep 17 00:00:00 2001 From: Pavel Pletenev Date: Sun, 26 May 2019 11:28:46 +0300 Subject: [PATCH 11/22] Suggested fix for empty domain (#555) * Suggested fix for empty domain Signed-off-by: delphi * Added proposed changes Signed-off-by: delphi --- esphome/config.py | 4 +++- tests/test2.yaml | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/esphome/config.py b/esphome/config.py index ac22db37ad..debd261b41 100644 --- a/esphome/config.py +++ b/esphome/config.py @@ -450,7 +450,9 @@ def validate_config(config): result.remove_output_path([domain], domain) # Ensure conf is a list - if not isinstance(conf, list) and conf: + if not conf: + result[domain] = conf = [] + elif not isinstance(conf, list): result[domain] = conf = [conf] for i, p_config in enumerate(conf): diff --git a/tests/test2.yaml b/tests/test2.yaml index 2b7ddebf3c..e3a9b0da85 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -238,3 +238,6 @@ interval: interval: 5s then: - logger.log: "Interval Run" + +display: + From d8963ea25a148ee4ca5aabce00d8224cf7b3d236 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 26 May 2019 10:31:12 +0200 Subject: [PATCH 12/22] Fix Switch Condition Fixes https://github.com/esphome/issues/issues/322 --- esphome/components/switch/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/switch/__init__.py b/esphome/components/switch/__init__.py index 6425a364a1..3870631e13 100644 --- a/esphome/components/switch/__init__.py +++ b/esphome/components/switch/__init__.py @@ -83,13 +83,13 @@ def switch_toggle_to_code(config, action_id, template_arg, args): @automation.register_condition('switch.is_on', SwitchCondition, SWITCH_ACTION_SCHEMA) def switch_is_on_to_code(config, condition_id, template_arg, args): paren = yield cg.get_variable(config[CONF_ID]) - yield cg.new_Pvariable(condition_id, template_arg, paren) + yield cg.new_Pvariable(condition_id, template_arg, paren, True) @automation.register_condition('switch.is_off', SwitchCondition, SWITCH_ACTION_SCHEMA) def switch_is_off_to_code(config, condition_id, template_arg, args): paren = yield cg.get_variable(config[CONF_ID]) - yield cg.new_Pvariable(condition_id, template_arg, paren) + yield cg.new_Pvariable(condition_id, template_arg, paren, False) @coroutine_with_priority(100.0) From 510e53de708f75e7866fc87965d4e565f4a8a7e1 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 26 May 2019 10:31:29 +0200 Subject: [PATCH 13/22] Add core version 2.5.2 --- esphome/components/pulse_width/sensor.py | 2 +- esphome/components/status/status_binary_sensor.cpp | 1 + esphome/const.py | 1 + esphome/core_config.py | 5 +++-- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/esphome/components/pulse_width/sensor.py b/esphome/components/pulse_width/sensor.py index 0227adffce..8328da2ac0 100644 --- a/esphome/components/pulse_width/sensor.py +++ b/esphome/components/pulse_width/sensor.py @@ -10,7 +10,7 @@ PulseWidthSensor = pulse_width_ns.class_('PulseWidthSensor', sensor.Sensor, cg.P CONFIG_SCHEMA = sensor.sensor_schema(UNIT_SECOND, ICON_TIMER, 3).extend({ cv.GenerateID(): cv.declare_id(PulseWidthSensor), - cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pullup_pin_schema, + cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema, pins.validate_has_interrupt), }).extend(cv.polling_component_schema('60s')) diff --git a/esphome/components/status/status_binary_sensor.cpp b/esphome/components/status/status_binary_sensor.cpp index d004c70b95..7fbeb8c171 100644 --- a/esphome/components/status/status_binary_sensor.cpp +++ b/esphome/components/status/status_binary_sensor.cpp @@ -1,6 +1,7 @@ #include "status_binary_sensor.h" #include "esphome/core/log.h" #include "esphome/core/util.h" +#include "esphome/core/defines.h" #ifdef USE_MQTT #include "esphome/components/mqtt/mqtt_client.h" diff --git a/esphome/const.py b/esphome/const.py index d58ed406c7..d3da18448e 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -19,6 +19,7 @@ ARDUINO_VERSION_ESP8266_DEV = 'https://github.com/platformio/platform-espressif8 '/stage' ARDUINO_VERSION_ESP8266_2_5_0 = 'espressif8266@2.0.0' ARDUINO_VERSION_ESP8266_2_5_1 = 'espressif8266@2.1.0' +ARDUINO_VERSION_ESP8266_2_5_2 = 'espressif8266@2.2.0' ARDUINO_VERSION_ESP8266_2_3_0 = 'espressif8266@1.5.0' SOURCE_FILE_EXTENSIONS = {'.cpp', '.hpp', '.h', '.c', '.tcc', '.ino'} HEADER_FILE_EXTENSIONS = {'.h', '.hpp', '.tcc'} diff --git a/esphome/core_config.py b/esphome/core_config.py index a2663b8db8..d02b7c98de 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -11,7 +11,7 @@ from esphome.const import ARDUINO_VERSION_ESP32_DEV, ARDUINO_VERSION_ESP8266_DEV CONF_NAME, CONF_ON_BOOT, CONF_ON_LOOP, CONF_ON_SHUTDOWN, CONF_PLATFORM, \ CONF_PLATFORMIO_OPTIONS, CONF_PRIORITY, CONF_TRIGGER_ID, \ CONF_ESP8266_RESTORE_FROM_FLASH, __version__, ARDUINO_VERSION_ESP8266_2_3_0, \ - ARDUINO_VERSION_ESP8266_2_5_0, ARDUINO_VERSION_ESP8266_2_5_1 + ARDUINO_VERSION_ESP8266_2_5_0, ARDUINO_VERSION_ESP8266_2_5_1, ARDUINO_VERSION_ESP8266_2_5_2 from esphome.core import CORE, coroutine_with_priority from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS @@ -44,6 +44,7 @@ def validate_board(value): validate_platform = cv.one_of('ESP32', 'ESP8266', upper=True) PLATFORMIO_ESP8266_LUT = { + '2.5.2': 'espressif8266@2.2.0', '2.5.1': 'espressif8266@2.1.0', '2.5.0': 'espressif8266@2.0.1', '2.4.2': 'espressif8266@1.8.0', @@ -193,7 +194,7 @@ def to_code(config): 'espressif8266@1.6.0'): ld_script = ld_scripts[0] elif CORE.arduino_version in (ARDUINO_VERSION_ESP8266_DEV, ARDUINO_VERSION_ESP8266_2_5_0, - ARDUINO_VERSION_ESP8266_2_5_1): + ARDUINO_VERSION_ESP8266_2_5_1, ARDUINO_VERSION_ESP8266_2_5_2): ld_script = ld_scripts[1] if ld_script is not None: From dac624231f4cc20315ce10ff00316ecdc6bc4fa4 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 09:58:55 +0200 Subject: [PATCH 14/22] Fix custom output, add test Fixes https://github.com/esphome/issues/issues/346 --- esphome/config_validation.py | 1 + tests/custom.h | 10 ++++++++++ tests/test3.yaml | 16 ++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/esphome/config_validation.py b/esphome/config_validation.py index ad0d90fcd3..466c540c81 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -1092,6 +1092,7 @@ def typed_schema(schemas, **kwargs): key_v = key_validator(value.pop(key)) value = schemas[key_v](value) value[key] = key_v + return value return validator diff --git a/tests/custom.h b/tests/custom.h index 7a5edbbf61..b392819efb 100644 --- a/tests/custom.h +++ b/tests/custom.h @@ -26,3 +26,13 @@ class CustomComponent : public PollingComponent { void setup() override { ESP_LOGD("custom_component", "Setup"); } void update() override { ESP_LOGD("custom_component", "Update"); } }; + +class CustomBinaryOutput : public BinaryOutput, public Component { + protected: + void write_state(bool state) override { ESP_LOGD("custom_output", "Setting %s", ONOFF(state)); } +}; + +class CustomFloatOutput : public FloatOutput, public Component { + protected: + void write_state(float state) override { ESP_LOGD("custom_output", "Setting %f", state); } +}; diff --git a/tests/test3.yaml b/tests/test3.yaml index 7fcc7e1e60..9b41efd7fb 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -377,6 +377,22 @@ output: id: out pin: D3 frequency: 50Hz + - platform: custom + type: binary + lambda: |- + auto s = new CustomBinaryOutput(); + App.register_component(s); + return {s}; + outputs: + - id: custom_binary + - platform: custom + type: float + lambda: |- + auto s = new CustomFloatOutput(); + App.register_component(s); + return {s}; + outputs: + - id: custom_float mcp23017: id: mcp From 72a80f559a0c1ef905fd4db9e164ebedbd3b1a2d Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 13:06:45 +0200 Subject: [PATCH 15/22] Use relative include, add check --- esphome/components/adc/adc_sensor.cpp | 2 +- esphome/components/ads1115/sensor.py | 3 +- .../binary_sensor/binary_sensor.cpp | 2 +- esphome/components/binary_sensor/filter.cpp | 4 +- esphome/components/dht/dht.cpp | 2 +- .../components/esp32_touch/binary_sensor.py | 3 +- .../gpio/binary_sensor/gpio_binary_sensor.cpp | 2 +- .../components/gpio/switch/gpio_switch.cpp | 2 +- esphome/components/logger/logger.cpp | 2 +- esphome/components/my9231/output.py | 2 +- esphome/components/ota/ota_component.cpp | 2 +- esphome/components/sensor/filter.cpp | 4 +- esphome/components/sensor/sensor.cpp | 2 +- esphome/components/switch/switch.cpp | 2 +- esphome/components/uptime/uptime_sensor.cpp | 2 +- esphome/components/wifi/wifi_component.cpp | 2 +- .../components/wifi/wifi_component_esp32.cpp | 2 +- .../wifi/wifi_component_esp8266.cpp | 2 +- script/ci-custom.py | 55 +++++++++++++++---- 19 files changed, 65 insertions(+), 32 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index d0cea53d49..2c448d0392 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/adc/adc_sensor.h" +#include "adc_sensor.h" #include "esphome/core/log.h" #ifdef USE_ADC_SENSOR_VCC diff --git a/esphome/components/ads1115/sensor.py b/esphome/components/ads1115/sensor.py index 2fe9b6fa86..204ccb99d7 100644 --- a/esphome/components/ads1115/sensor.py +++ b/esphome/components/ads1115/sensor.py @@ -1,10 +1,9 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor, voltage_sampler -from esphome.components.ads1115 import ADS1115Component from esphome.const import CONF_GAIN, CONF_MULTIPLEXER, ICON_FLASH, UNIT_VOLT, CONF_ID from esphome.py_compat import string_types -from . import ads1115_ns +from . import ads1115_ns, ADS1115Component DEPENDENCIES = ['ads1115'] diff --git a/esphome/components/binary_sensor/binary_sensor.cpp b/esphome/components/binary_sensor/binary_sensor.cpp index 8c982f938d..27c835d38c 100644 --- a/esphome/components/binary_sensor/binary_sensor.cpp +++ b/esphome/components/binary_sensor/binary_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/binary_sensor/binary_sensor.h" +#include "binary_sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/binary_sensor/filter.cpp b/esphome/components/binary_sensor/filter.cpp index 39b88f867c..b7ac2c4a79 100644 --- a/esphome/components/binary_sensor/filter.cpp +++ b/esphome/components/binary_sensor/filter.cpp @@ -1,5 +1,5 @@ -#include "esphome/components/binary_sensor/filter.h" -#include "esphome/components/binary_sensor/binary_sensor.h" +#include "filter.h" +#include "binary_sensor.h" namespace esphome { diff --git a/esphome/components/dht/dht.cpp b/esphome/components/dht/dht.cpp index 79732bb269..24a017e478 100644 --- a/esphome/components/dht/dht.cpp +++ b/esphome/components/dht/dht.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/dht/dht.h" +#include "dht.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/esp32_touch/binary_sensor.py b/esphome/components/esp32_touch/binary_sensor.py index 94748e53e8..a72ca5796f 100644 --- a/esphome/components/esp32_touch/binary_sensor.py +++ b/esphome/components/esp32_touch/binary_sensor.py @@ -1,10 +1,9 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import binary_sensor -from esphome.components.esp32_touch import ESP32TouchComponent from esphome.const import CONF_NAME, CONF_PIN, CONF_THRESHOLD, ESP_PLATFORM_ESP32, CONF_ID from esphome.pins import validate_gpio_pin -from . import esp32_touch_ns +from . import esp32_touch_ns, ESP32TouchComponent ESP_PLATFORMS = [ESP_PLATFORM_ESP32] DEPENDENCIES = ['esp32_touch'] diff --git a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp index dff3609ce2..f95778af4c 100644 --- a/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp +++ b/esphome/components/gpio/binary_sensor/gpio_binary_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" +#include "gpio_binary_sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/gpio/switch/gpio_switch.cpp b/esphome/components/gpio/switch/gpio_switch.cpp index 22139d6b9c..d22a74847e 100644 --- a/esphome/components/gpio/switch/gpio_switch.cpp +++ b/esphome/components/gpio/switch/gpio_switch.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/gpio/switch/gpio_switch.h" +#include "gpio_switch.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/logger/logger.cpp b/esphome/components/logger/logger.cpp index cd71049f15..78f09989e4 100644 --- a/esphome/components/logger/logger.cpp +++ b/esphome/components/logger/logger.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/logger/logger.h" +#include "logger.h" #ifdef ARDUINO_ARCH_ESP32 #include diff --git a/esphome/components/my9231/output.py b/esphome/components/my9231/output.py index 9acc8bdcd6..c69649fd5e 100644 --- a/esphome/components/my9231/output.py +++ b/esphome/components/my9231/output.py @@ -1,8 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import output -from esphome.components.my9231 import MY9231OutputComponent from esphome.const import CONF_CHANNEL, CONF_ID +from . import MY9231OutputComponent DEPENDENCIES = ['my9231'] diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index ee2d67c85e..d37a7a0c6a 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/ota/ota_component.h" +#include "ota_component.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 4923a1c09b..306607dfda 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -1,5 +1,5 @@ -#include "esphome/components/sensor/filter.h" -#include "esphome/components/sensor/sensor.h" +#include "filter.h" +#include "sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index ca6f4c23bb..e12e55e320 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/sensor/sensor.h" +#include "sensor.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/switch/switch.cpp b/esphome/components/switch/switch.cpp index d2279e6ad6..a6a25dd9dc 100644 --- a/esphome/components/switch/switch.cpp +++ b/esphome/components/switch/switch.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/switch/switch.h" +#include "switch.h" #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/uptime/uptime_sensor.cpp b/esphome/components/uptime/uptime_sensor.cpp index a66ca0636f..f047724768 100644 --- a/esphome/components/uptime/uptime_sensor.cpp +++ b/esphome/components/uptime/uptime_sensor.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/uptime/uptime_sensor.h" +#include "uptime_sensor.h" #include "esphome/core/log.h" #include "esphome/core/helpers.h" diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 35735e5f08..882d45f793 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP32 #include diff --git a/esphome/components/wifi/wifi_component_esp32.cpp b/esphome/components/wifi/wifi_component_esp32.cpp index f50acee614..6b118cf0ae 100644 --- a/esphome/components/wifi/wifi_component_esp32.cpp +++ b/esphome/components/wifi/wifi_component_esp32.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP32 diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index a2c9ac2551..11981b801b 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -1,4 +1,4 @@ -#include "esphome/components/wifi/wifi_component.h" +#include "wifi_component.h" #ifdef ARDUINO_ARCH_ESP8266 diff --git a/script/ci-custom.py b/script/ci-custom.py index 6aff5717cf..27b45dbe27 100755 --- a/script/ci-custom.py +++ b/script/ci-custom.py @@ -86,9 +86,12 @@ def lint_content_find_check(find, **kwargs): decor = lint_content_check(**kwargs) def decorator(func): - def new_func(content): - for line, col in find_all(content, find): - err = func() + def new_func(fname, content): + find_ = find + if callable(find): + find_ = find(fname, content) + for line, col in find_all(content, find_): + err = func(fname) return "{err} See line {line}:{col}.".format(err=err, line=line+1, col=col+1) return decor(new_func) return decorator @@ -123,31 +126,63 @@ def lint_executable_bit(fname): 'esphome/dashboard/static/ace.js', 'esphome/dashboard/static/ext-searchbox.js', 'script/.neopixelbus.patch', ]) -def lint_tabs(): +def lint_tabs(fname): return "File contains tab character. Please convert tabs to spaces." @lint_content_find_check('\r') -def lint_newline(): +def lint_newline(fname): return "File contains windows newline. Please set your editor to unix newline mode." @lint_content_check() -def lint_end_newline(content): +def lint_end_newline(fname, content): if content and not content.endswith('\n'): return "File does not end with a newline, please add an empty line at the end of the file." return None +def relative_cpp_search_text(fname, content): + parts = fname.split('/') + integration = parts[2] + return '#include "esphome/components/{}'.format(integration) + + +@lint_content_find_check(relative_cpp_search_text, include=['esphome/components/*.cpp']) +def lint_relative_cpp_import(fname): + return ("Component contains absolute import - Components must always use " + "relative imports.\n" + "Change:\n" + ' #include "esphome/components/abc/abc.h"\n' + 'to:\n' + ' #include "abc.h"\n\n') + + +def relative_py_search_text(fname, content): + parts = fname.split('/') + integration = parts[2] + return 'esphome.components.{}'.format(integration) + + +@lint_content_find_check(relative_py_search_text, include=['esphome/components/*.py']) +def lint_relative_py_import(fname): + return ("Component contains absolute import - Components must always use " + "relative imports within the integration.\n" + "Change:\n" + ' from esphome.components.abc import abc_ns"\n' + 'to:\n' + ' from . import abc_ns\n\n') + + @lint_content_find_check('"esphome.h"', include=cpp_include, exclude=['tests/custom.h']) -def lint_esphome_h(): +def lint_esphome_h(fname): return ("File contains reference to 'esphome.h' - This file is " "auto-generated and should only be used for *custom* " "components. Please replace with references to the direct files.") @lint_content_check(include=['*.h']) -def lint_pragma_once(content): +def lint_pragma_once(fname, content): if '#pragma once' not in content: return ("Header file contains no 'pragma once' header guard. Please add a " "'#pragma once' line at the top of the file.") @@ -171,7 +206,7 @@ def lint_pragma_once(content): 'esphome/core/log.h', 'tests/custom.h', ]) -def lint_log_in_header(): +def lint_log_in_header(fname): return ('Found reference to ESP_LOG in header file. Using ESP_LOG* in header files ' 'is currently not possible - please move the definition to a source file (.cpp)') @@ -202,7 +237,7 @@ for fname in files: except UnicodeDecodeError: add_errors(fname, "File is not readable as UTF-8. Please set your editor to UTF-8 mode.") continue - run_checks(LINT_CONTENT_CHECKS, fname, content) + run_checks(LINT_CONTENT_CHECKS, fname, fname, content) for f, errs in sorted(errors.items()): print("\033[0;32m************* File \033[1;32m{}\033[0m".format(f)) From 7b5c4359c67c10e851bd3cf57e73b16a27a423b9 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 15:39:01 +0200 Subject: [PATCH 16/22] Update nextion.h --- esphome/components/nextion/nextion.h | 171 ++++++++++++++++++++++----- 1 file changed, 139 insertions(+), 32 deletions(-) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index b5bc86ec62..2286e4a7d2 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -32,8 +32,14 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param component The component name. * @param format The printf-style format string. * @param ... The arguments to the format. - * Example: `it.set_component_text_printf("textview", "The uptime is: %.1f", id(uptime_sensor).state);` - * Component named `textview` has been changed to `The uptime is:` Then the value of `uptime_sensor`. + * + * Example: + * ```cpp + * it.set_component_text_printf("textview", "The uptime is: %.0f", id(uptime_sensor).state); + * ``` + * + * This will change the text on the component named `textview` to `The uptime is:` Then the value of `uptime_sensor`. + * with zero decimals of accuracy (whole number). * For example when `uptime_sensor` = 506, then, `The uptime is: 506` will be displayed. */ void set_component_text_printf(const char *component, const char *format, ...) __attribute__((format(printf, 3, 4))); @@ -41,16 +47,26 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the integer value of a component * @param component The component name. * @param value The value to set. - * Example: `it.set_component_value("gauge", 50);` - * Component named `gauge` has changed the `val`to 50. + * + * Example: + * ```cpp + * it.set_component_value("gauge", 50); + * ``` + * + * This will change the property `value` of the component `gauge` to 50. */ void set_component_value(const char *component, int value); /** * Set the picture of an image component. * @param component The component name. * @param value The picture name. - * Example: `it.set_component_picture("pic", "4");` - * The picture component named `pic`, has changed the image which has the ID `4`, Which was set in the Nextion editor. + * + * Example: + * ```cpp + * it.set_component_picture("pic", "4"); + * ``` + * + * This will change the image of the component `pic` to the image with ID `4`. */ void set_component_picture(const char *component, const char *picture) { this->send_command_printf("%s.val=%s", component, picture); @@ -59,8 +75,13 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the background color of a component. * @param component The component name. * @param color The color (as a string). - * Example: `it.set_component_background_color("button", "17013"); - * The background color of component named `button`, has been changed to blue. + * + * Example: + * ```cpp + * it.set_component_background_color("button", "17013"); + * ``` + * + * This will change the background color of the component `button` to blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void set_component_background_color(const char *component, const char *color); @@ -68,27 +89,43 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the pressed background color of a component. * @param component The component name. * @param color The color (as a string). - * Example: `it.set_component_pressed_background_color("button", "17013"); - * The pressed background color of the component named `button` has been changed to blue. So when the button is pressed, the background color will change to blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors + * + * Example: + * ```cpp + * it.set_component_pressed_background_color("button", "17013"); + * ``` + * + * This will change the pressed background color of the component `button` to blue. This is the background color that is shown when the + * component is pressed. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void set_component_pressed_background_color(const char *component, const char *color); /** * Set the font color of a component. * @param component The component name. * @param color The color (as a string). - * Example: `it.set_component_font_color("textview", "17013); - * The font color of component named `textview` has been changed to blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors + * + * Example: + * ```cpp + * it.set_component_font_color("textview", "17013"); + * ``` + * + * This will change the font color of the component `textview` to a blue color. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void set_component_font_color(const char *component, const char *color); /** * Set the pressed font color of a component. * @param component The component name. * @param color The color (as a string). - * Example: `it.set_component_pressed_font_color("button", "17013");` - * The pressed font color for component named button has been changed to blue. So when `button` is pressed, the font color will change to blue - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors + * + * Example: + * ```cpp + * it.set_component_pressed_font_color("button", "17013"); + * ``` + * + * This will change the pressed font color of the component `button` to a blue color. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ void set_component_pressed_font_color(const char *component, const char *color); /** @@ -96,15 +133,25 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param component The component name. * @param x The x coordinate. * @param y The y coordinate. - * Example: `it.set_component_coordinates("pic", 55, 100);` - * The component named `pic` has moved to the x coordinate `55` and y coordinate `100`. + * + * Example: + * ```cpp + * it.set_component_coordinates("pic", 55, 100); + * ``` + * + * This will move the position of the component `pic` to the x coordinate `55` and y coordinate `100`. */ void set_component_coordinates(const char *component, int x, int y); /** * Set the font id for a component. * @param component The component name. * @param font_id The ID of the font (number). - * Example: `it.set_component_font("textview", "3");` + * + * Example: + * ```cpp + * it.set_component_font("textview", "3"); + * ``` + * * Changes the font of the component named `textveiw`. Font IDs are set in the Nextion Editor. */ void set_component_font(const char *component, uint8_t font_id); @@ -119,35 +166,60 @@ class Nextion : public PollingComponent, public uart::UARTDevice { /** * Show the page with a given name. * @param page The name of the page. - * Example: `it.goto_page("main");` + * + * Example: + * ```cpp + * it.goto_page("main"); + * ``` + * * Switches to the page named `main`. Pages are named in the Nextion Editor. */ void goto_page(const char *page); /** * Hide a component. * @param component The component name. - * Example: `hide_component("button");` + * + * Example: + * ```cpp + * hide_component("button"); + * ``` + * * Hides the component named `button`. */ void hide_component(const char *component); /** * Show a component. * @param component The component name. - * Example: `show_component("button");` + * + * Example: + * ```cpp + * show_component("button"); + * ``` + * * Shows the component named `button`. */ void show_component(const char *component); /** * Enable touch for a component. * @param component The component name. - * Example: `enable_component_touch("button");` + * + * Example: + * ```cpp + * enable_component_touch("button"); + * ``` + * * Enables touch for component named `button`. */ void enable_component_touch(const char *component); /** * Disable touch for a component. * @param component The component name. - * Examlple: `disable_component_touch("button");` + * + * Example: + * ```cpp + * disable_component_touch("button"); + * ``` + * * Disables touch for component named `button`. */ void disable_component_touch(const char *component); @@ -163,7 +235,12 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param picture_id The picture id. * @param x1 The x coordinate. * @param y1 The y coordniate. - * Example: `display_picture(2, 15, 25);` + * + * Example: + * ```cpp + * display_picture(2, 15, 25); + * ``` + * * Displays the picture who has the id `2` at the x coordinates `15` and y coordinates `25`. */ void display_picture(int picture_id, int x_start, int y_start); @@ -174,7 +251,12 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param width The width to draw. * @param height The height to draw. * @param color The color to draw with (as a string). - * Example: `fill_area(50, 50, 100, 100, "17013");` + * + * Example: + * ```cpp + * fill_area(50, 50, 100, 100, "17013"); + * ``` + * * Fills an area that starts at x coordiante `50` and y coordinate `50` with a height of `100` and width of `100` with the color of blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors */ @@ -186,7 +268,12 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param x2 The ending x coordinate. * @param y2 The ending y coordinate. * @param color The color to draw with (as a string). - * Example: `it.line(50, 50, 75, 75, "17013");` + * + * Example: + * ```cpp + * it.line(50, 50, 75, 75, "17013"); + * ``` + * * Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate `75` with the color of blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ @@ -198,7 +285,12 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param width The width of the rectangle. * @param height The height of the rectangle. * @param color The color to draw with (as a string). - * Example: `it.rectangle(25, 35, 40, 50, "17013");` + * + * Example: + * ```cpp + * it.rectangle(25, 35, 40, 50, "17013"); + * ``` + * * Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a length of `50` with color of blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ @@ -217,7 +309,12 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param center_y The center y coordinate. * @param radius The circle radius. * @param color The color to draw with (as a string). - * Example: `it.filled_cricle(25, 25, 10, "17013");` + * + * Example: + * ```cpp + * it.filled_cricle(25, 25, 10, "17013"); + * ``` + * * Makes a filled circle at the x cordinates `25` and y coordinate `25` with a radius of `10` with a color of blue. * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. */ @@ -226,14 +323,24 @@ class Nextion : public PollingComponent, public uart::UARTDevice { /** Set the brightness of the backlight. * * @param brightness The brightness, from 0 to 100. - * Example: `it.set_backlight_brightness(30);` + * + * Example: + * ```cpp + * it.set_backlight_brightness(30); + * ``` + * * Changes the brightness of the display to 30%. */ void set_backlight_brightness(uint8_t brightness); /** * Set the touch sleep timeout of the display. * @param timeout Timeout in seconds. - * Example: `it.set_touch_sleep_timeout(30);` + * + * Example: + * ```cpp + * it.set_touch_sleep_timeout(30); + * ``` + * * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up `thup`. */ void set_touch_sleep_timeout(uint16_t timeout); From 0d9f5ef363f8c78b98a3d4f204e37149a85a7df5 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 16:51:01 +0200 Subject: [PATCH 17/22] Update nextion.h --- esphome/components/nextion/nextion.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 2286e4a7d2..68d412cb47 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -23,8 +23,13 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Set the text of a component to a static string. * @param component The component name. * @param text The static text to set. - * Example: `it.set_component_text("textview", "Hello World!");` - * Component named `textview` `txt` value has been changed to `Hello World`. + * + * Example: + * ```cpp + * it.set_component_text("textview", "Hello World!"); + * ``` + * + * This will set the `txt` property of the component `textview` to `Hello World`. */ void set_component_text(const char *component, const char *text); /** From 72218171b3e7d572491bb587d33aba12eee4761c Mon Sep 17 00:00:00 2001 From: Brandon Davidson Date: Mon, 27 May 2019 08:21:01 -0700 Subject: [PATCH 18/22] Don't fall back to the global availability topic if it is empty (#553) Fixes esphome/issues#320 --- esphome/components/mqtt/mqtt_component.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index 13b48630fa..042e362f75 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -72,11 +72,13 @@ bool MQTTComponent::send_discovery_() { root["command_topic"] = this->get_command_topic_(); if (this->availability_ == nullptr) { - root["availability_topic"] = global_mqtt_client->get_availability().topic; - if (global_mqtt_client->get_availability().payload_available != "online") - root["payload_available"] = global_mqtt_client->get_availability().payload_available; - if (global_mqtt_client->get_availability().payload_not_available != "offline") - root["payload_not_available"] = global_mqtt_client->get_availability().payload_not_available; + if (!global_mqtt_client->get_availability().topic.empty()) { + root["availability_topic"] = global_mqtt_client->get_availability().topic; + if (global_mqtt_client->get_availability().payload_available != "online") + root["payload_available"] = global_mqtt_client->get_availability().payload_available; + if (global_mqtt_client->get_availability().payload_not_available != "offline") + root["payload_not_available"] = global_mqtt_client->get_availability().payload_not_available; + } } else if (!this->availability_->topic.empty()) { root["availability_topic"] = this->availability_->topic; if (this->availability_->payload_available != "online") From 28f2a7f99cb6551f3bb1273c95f4b5517b657af7 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 19:12:43 +0200 Subject: [PATCH 19/22] Update nextion.h --- esphome/components/nextion/nextion.h | 45 +++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index 68d412cb47..92b41a88af 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -57,7 +57,7 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * ```cpp * it.set_component_value("gauge", 50); * ``` - * + * * This will change the property `value` of the component `gauge` to 50. */ void set_component_value(const char *component, int value); @@ -87,7 +87,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * ``` * * This will change the background color of the component `button` to blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to + * Nextion HMI colors. */ void set_component_background_color(const char *component, const char *color); /** @@ -100,9 +101,10 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * it.set_component_pressed_background_color("button", "17013"); * ``` * - * This will change the pressed background color of the component `button` to blue. This is the background color that is shown when the - * component is pressed. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * This will change the pressed background color of the component `button` to blue. This is the background color that + * is shown when the component is pressed. Use this [color + * picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI + * colors. */ void set_component_pressed_background_color(const char *component, const char *color); /** @@ -116,7 +118,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * ``` * * This will change the font color of the component `textview` to a blue color. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to + * Nextion HMI colors. */ void set_component_font_color(const char *component, const char *color); /** @@ -124,13 +127,14 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * @param component The component name. * @param color The color (as a string). * - * Example: + * Example: * ```cpp * it.set_component_pressed_font_color("button", "17013"); * ``` * * This will change the pressed font color of the component `button` to a blue color. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to + * Nextion HMI colors. */ void set_component_pressed_font_color(const char *component, const char *color); /** @@ -184,7 +188,7 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * Hide a component. * @param component The component name. * - * Example: + * Example: * ```cpp * hide_component("button"); * ``` @@ -262,8 +266,9 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * fill_area(50, 50, 100, 100, "17013"); * ``` * - * Fills an area that starts at x coordiante `50` and y coordinate `50` with a height of `100` and width of `100` with the color of blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors + * Fills an area that starts at x coordiante `50` and y coordinate `50` with a height of `100` and width of `100` with + * the color of blue. Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to + * convert color codes to Nextion HMI colors */ void fill_area(int x1, int y1, int width, int height, const char *color); /** @@ -279,8 +284,10 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * it.line(50, 50, 75, 75, "17013"); * ``` * - * Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate `75` with the color of blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate + * `75` with the color of blue. Use this [color + * picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI + * colors. */ void line(int x1, int y1, int x2, int y2, const char *color); /** @@ -296,8 +303,10 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * it.rectangle(25, 35, 40, 50, "17013"); * ``` * - * Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a length of `50` with color of blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a + * length of `50` with color of blue. Use this [color + * picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI + * colors. */ void rectangle(int x1, int y1, int width, int height, const char *color); /** @@ -321,7 +330,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * ``` * * Makes a filled circle at the x cordinates `25` and y coordinate `25` with a radius of `10` with a color of blue. - * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI colors. + * Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to + * Nextion HMI colors. */ void filled_circle(int center_x, int center_y, int radius, const char *color); @@ -346,7 +356,8 @@ class Nextion : public PollingComponent, public uart::UARTDevice { * it.set_touch_sleep_timeout(30); * ``` * - * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up `thup`. + * After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up + * `thup`. */ void set_touch_sleep_timeout(uint16_t timeout); From 4bc306772590b6b6df5bf5e28ea356d17e8a272f Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 19:22:16 +0200 Subject: [PATCH 20/22] Add light restore mode I don't want users to rely on setup priority. Ref https://github.com/esphome/esphome-docs/pull/248 --- esphome/components/light/__init__.py | 14 +++++++++++++- esphome/components/light/light_state.cpp | 23 ++++++++++++++++++----- esphome/components/light/light_state.h | 10 ++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/esphome/components/light/__init__.py b/esphome/components/light/__init__.py index e0ea8f246d..63aac69462 100644 --- a/esphome/components/light/__init__.py +++ b/esphome/components/light/__init__.py @@ -3,7 +3,7 @@ import esphome.config_validation as cv from esphome.components import mqtt, power_supply from esphome.const import CONF_COLOR_CORRECT, \ CONF_DEFAULT_TRANSITION_LENGTH, CONF_EFFECTS, CONF_GAMMA_CORRECT, CONF_ID, \ - CONF_INTERNAL, CONF_NAME, CONF_MQTT_ID, CONF_POWER_SUPPLY + CONF_INTERNAL, CONF_NAME, CONF_MQTT_ID, CONF_POWER_SUPPLY, CONF_RESTORE_MODE from esphome.core import coroutine, coroutine_with_priority from .automation import light_control_to_code # noqa from .effects import validate_effects, BINARY_EFFECTS, \ @@ -12,9 +12,20 @@ from .types import ( # noqa LightState, AddressableLightState, light_ns, LightOutput, AddressableLight) IS_PLATFORM_COMPONENT = True + +LightRestoreMode = light_ns.enum('LightRestoreMode') +RESTORE_MODES = { + 'RESTORE_DEFAULT_OFF': LightRestoreMode.LIGHT_RESTORE_DEFAULT_OFF, + 'RESTORE_DEFAULT_ON': LightRestoreMode.LIGHT_RESTORE_DEFAULT_ON, + 'ALWAYS_OFF': LightRestoreMode.LIGHT_ALWAYS_OFF, + 'ALWAYS_ON': LightRestoreMode.LIGHT_ALWAYS_ON, +} + LIGHT_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(LightState), cv.OnlyWith(CONF_MQTT_ID, 'mqtt'): cv.declare_id(mqtt.MQTTJSONLightComponent), + cv.Optional(CONF_RESTORE_MODE, default='restore_default_off'): + cv.enum(RESTORE_MODES, upper=True, space='_'), }) BINARY_LIGHT_SCHEMA = LIGHT_SCHEMA.extend({ @@ -41,6 +52,7 @@ ADDRESSABLE_LIGHT_SCHEMA = RGB_LIGHT_SCHEMA.extend({ @coroutine def setup_light_core_(light_var, output_var, config): + cg.add(light_var.set_restore_mode(config[CONF_RESTORE_MODE])) if CONF_INTERNAL in config: cg.add(light_var.set_internal(config[CONF_INTERNAL])) if CONF_DEFAULT_TRANSITION_LENGTH in config: diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index 7e61ea245a..cafced27fc 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -98,12 +98,25 @@ void LightState::setup() { effect->init_internal(this); } - this->rtc_ = global_preferences.make_preference(this->get_object_id_hash()); - LightStateRTCState recovered{}; - // Attempt to load from preferences, else fall back to default values from struct - this->rtc_.load(&recovered); - auto call = this->make_call(); + LightStateRTCState recovered{}; + switch (this->restore_mode_) { + case LIGHT_RESTORE_DEFAULT_OFF: + case LIGHT_RESTORE_DEFAULT_ON: + this->rtc_ = global_preferences.make_preference(this->get_object_id_hash()); + // Attempt to load from preferences, else fall back to default values from struct + if (!this->rtc_.load(&recovered)) { + recovered.state = this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON; + } + break; + case LIGHT_ALWAYS_OFF: + recovered.state = false; + break; + case LIGHT_ALWAYS_ON: + recovered.state = true; + break; + } + call.set_state(recovered.state); call.set_brightness_if_supported(recovered.brightness); call.set_red_if_supported(recovered.red); diff --git a/esphome/components/light/light_state.h b/esphome/components/light/light_state.h index 24e659ec8a..d67aa2c53d 100644 --- a/esphome/components/light/light_state.h +++ b/esphome/components/light/light_state.h @@ -160,6 +160,13 @@ class LightCall { bool save_{true}; }; +enum LightRestoreMode { + LIGHT_RESTORE_DEFAULT_OFF, + LIGHT_RESTORE_DEFAULT_ON, + LIGHT_ALWAYS_OFF, + LIGHT_ALWAYS_ON, +}; + /** This class represents the communication layer between the front-end MQTT layer and the * hardware output layer. */ @@ -249,6 +256,7 @@ class LightState : public Nameable, public Component { /// Set the gamma correction factor void set_gamma_correct(float gamma_correct); float get_gamma_correct() const { return this->gamma_correct_; } + void set_restore_mode(LightRestoreMode restore_mode) { restore_mode_ = restore_mode; } const std::vector &get_effects() const; @@ -292,6 +300,8 @@ class LightState : public Nameable, public Component { /// Object used to store the persisted values of the light. ESPPreferenceObject rtc_; + /// Restore mode of the light. + LightRestoreMode restore_mode_; /// Default transition length for all transitions in ms. uint32_t default_transition_length_{}; /// Value for storing the index of the currently active effect. 0 if no effect is active From 4323ca88c37d6bf786072f633806fd31e5d9f41b Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 19:35:36 +0200 Subject: [PATCH 21/22] Add test for automation light effect --- esphome/components/light/effects.py | 6 +++--- esphome/const.py | 1 + tests/test1.yaml | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/esphome/components/light/effects.py b/esphome/components/light/effects.py index 93dea2628d..72a2155a1b 100644 --- a/esphome/components/light/effects.py +++ b/esphome/components/light/effects.py @@ -4,7 +4,7 @@ from esphome import automation from esphome.const import CONF_NAME, CONF_LAMBDA, CONF_UPDATE_INTERVAL, CONF_TRANSITION_LENGTH, \ CONF_COLORS, CONF_STATE, CONF_DURATION, CONF_BRIGHTNESS, CONF_RED, CONF_GREEN, CONF_BLUE, \ CONF_WHITE, CONF_ALPHA, CONF_INTENSITY, CONF_SPEED, CONF_WIDTH, CONF_NUM_LEDS, CONF_RANDOM, \ - CONF_THEN + CONF_THEN, CONF_SEQUENCE from esphome.util import Registry from .types import LambdaLightEffect, RandomLightEffect, StrobeLightEffect, \ StrobeLightEffectColor, LightColorValues, AddressableLightRef, AddressableLambdaLightEffect, \ @@ -63,11 +63,11 @@ def lambda_effect_to_code(config, effect_id): @register_effect('automation', AutomationLightEffect, "Automation", { - cv.Required(CONF_THEN): automation.validate_automation(single=True), + cv.Required(CONF_SEQUENCE): automation.validate_automation(single=True), }) def automation_effect_to_code(config, effect_id): var = yield cg.new_Pvariable(effect_id, config[CONF_NAME]) - yield automation.build_automation(var.get_trig(), [], config[CONF_THEN]) + yield automation.build_automation(var.get_trig(), [], config[CONF_SEQUENCE]) yield var diff --git a/esphome/const.py b/esphome/const.py index d3da18448e..d285105df4 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -366,6 +366,7 @@ CONF_SERVICE = 'service' CONF_SERVICES = 'services' CONF_SETUP_MODE = 'setup_mode' CONF_SETUP_PRIORITY = 'setup_priority' +CONF_SEQUENCE = 'sequence' CONF_SHUNT_RESISTANCE = 'shunt_resistance' CONF_SHUNT_VOLTAGE = 'shunt_voltage' CONF_SHUTDOWN_MESSAGE = 'shutdown_message' diff --git a/tests/test1.yaml b/tests/test1.yaml index b9177802fa..bb692d431e 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -17,7 +17,7 @@ esphome: ESP_LOGV("main", "ON LOOP!"); - light.addressable_set: id: addr1 - range_from: 1 + range_from: 0 range_to: 100 red: 100% green: !lambda 'return 255;' @@ -839,6 +839,20 @@ light: name: Flicker Effect With Custom Values update_interval: 16ms intensity: 5% + - automation: + name: Custom Effect + sequence: + - light.addressable_set: + id: addr1 + red: 100% + green: 100% + blue: 0% + - delay: 100ms + - light.addressable_set: + id: addr1 + red: 0% + green: 100% + blue: 0% - platform: fastled_spi id: addr2 chipset: WS2801 From b7daee533a3bec6c72c63da31bb4c3df9040f75f Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 27 May 2019 20:00:00 +0200 Subject: [PATCH 22/22] Lint --- esphome/components/light/effects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/light/effects.py b/esphome/components/light/effects.py index 72a2155a1b..a78165fb8a 100644 --- a/esphome/components/light/effects.py +++ b/esphome/components/light/effects.py @@ -4,7 +4,7 @@ from esphome import automation from esphome.const import CONF_NAME, CONF_LAMBDA, CONF_UPDATE_INTERVAL, CONF_TRANSITION_LENGTH, \ CONF_COLORS, CONF_STATE, CONF_DURATION, CONF_BRIGHTNESS, CONF_RED, CONF_GREEN, CONF_BLUE, \ CONF_WHITE, CONF_ALPHA, CONF_INTENSITY, CONF_SPEED, CONF_WIDTH, CONF_NUM_LEDS, CONF_RANDOM, \ - CONF_THEN, CONF_SEQUENCE + CONF_SEQUENCE from esphome.util import Registry from .types import LambdaLightEffect, RandomLightEffect, StrobeLightEffect, \ StrobeLightEffectColor, LightColorValues, AddressableLightRef, AddressableLambdaLightEffect, \