diff --git a/esphome/components/nextion/display.py b/esphome/components/nextion/display.py index 30d7519380..394de69585 100644 --- a/esphome/components/nextion/display.py +++ b/esphome/components/nextion/display.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import display, uart -from esphome.const import CONF_ID, CONF_LAMBDA +from esphome.const import CONF_ID, CONF_LAMBDA, CONF_BRIGHTNESS from . import nextion_ns DEPENDENCIES = ['uart'] @@ -12,6 +12,7 @@ NextionRef = Nextion.operator('ref') CONFIG_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(Nextion), + cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage, }).extend(cv.polling_component_schema('5s')).extend(uart.UART_DEVICE_SCHEMA) @@ -20,6 +21,8 @@ def to_code(config): yield cg.register_component(var, config) yield uart.register_uart_device(var, config) + if CONF_BRIGHTNESS in config: + cg.add(var.set_brightness(config[CONF_BRIGHTNESS])) if CONF_LAMBDA in config: lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], [(NextionRef, 'it')], return_type=cg.void) diff --git a/esphome/components/nextion/nextion.cpp b/esphome/components/nextion/nextion.cpp index e594e147f4..d18d9f33d8 100644 --- a/esphome/components/nextion/nextion.cpp +++ b/esphome/components/nextion/nextion.cpp @@ -9,6 +9,7 @@ static const char *TAG = "nextion"; void Nextion::setup() { this->send_command_no_ack(""); this->send_command_printf("bkcmd=3"); + this->set_backlight_brightness(static_cast(brightness_ * 100)); this->goto_page("0"); } float Nextion::get_setup_priority() const { return setup_priority::PROCESSOR; } diff --git a/esphome/components/nextion/nextion.h b/esphome/components/nextion/nextion.h index bd37e241e9..a55ff747ee 100644 --- a/esphome/components/nextion/nextion.h +++ b/esphome/components/nextion/nextion.h @@ -365,6 +365,7 @@ class Nextion : public PollingComponent, public uart::UARTDevice { // (In most use cases you won't need these) void register_touch_component(NextionTouchComponent *obj) { this->touch_.push_back(obj); } void setup() override; + void set_brightness(float brightness) { this->brightness_ = brightness; } float get_setup_priority() const override; void update() override; void loop() override; @@ -392,6 +393,7 @@ class Nextion : public PollingComponent, public uart::UARTDevice { std::vector touch_; optional writer_; bool wait_for_ack_{true}; + float brightness_{1.0}; }; class NextionTouchComponent : public binary_sensor::BinarySensorInitiallyOff {