From 808e3be324590425ad831c7d7e7c80d3680686bf Mon Sep 17 00:00:00 2001 From: Stanislav Meduna Date: Sat, 3 Apr 2021 04:00:41 +0200 Subject: [PATCH] Add the display.is_displaying_page condition (#1646) * display: add the display.is_displaying_page condition * use maybe_simple_value for page_id * add test --- esphome/components/display/__init__.py | 25 ++++++++++++++++++++- esphome/components/display/display_buffer.h | 14 ++++++++++++ tests/test1.yaml | 7 ++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/esphome/components/display/__init__.py b/esphome/components/display/__init__.py index f72ec88fae..fe8bc9bd7c 100644 --- a/esphome/components/display/__init__.py +++ b/esphome/components/display/__init__.py @@ -2,7 +2,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import core, automation from esphome.automation import maybe_simple_id -from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES, CONF_ROTATION +from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES, CONF_PAGE_ID, CONF_ROTATION from esphome.core import coroutine, coroutine_with_priority IS_PLATFORM_COMPONENT = True @@ -19,6 +19,9 @@ DisplayPageShowNextAction = display_ns.class_( DisplayPageShowPrevAction = display_ns.class_( "DisplayPageShowPrevAction", automation.Action ) +DisplayIsDisplayingPageCondition = display_ns.class_( + "DisplayIsDisplayingPageCondition", automation.Condition +) DISPLAY_ROTATIONS = { 0: display_ns.DISPLAY_ROTATION_0_DEGREES, @@ -125,6 +128,26 @@ def display_page_show_previous_to_code(config, action_id, template_arg, args): yield cg.new_Pvariable(action_id, template_arg, paren) +@automation.register_condition( + "display.is_displaying_page", + DisplayIsDisplayingPageCondition, + cv.maybe_simple_value( + { + cv.GenerateID(CONF_ID): cv.use_id(DisplayBuffer), + cv.Required(CONF_PAGE_ID): cv.use_id(DisplayPage), + }, + key=CONF_PAGE_ID, + ), +) +def display_is_displaying_page_to_code(config, condition_id, template_arg, args): + paren = yield cg.get_variable(config[CONF_ID]) + page = yield cg.get_variable(config[CONF_PAGE_ID]) + var = cg.new_Pvariable(condition_id, template_arg, paren) + cg.add(var.set_page(page)) + + yield var + + @coroutine_with_priority(100.0) def to_code(config): cg.add_global(display_ns.using) diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index 5a63441e2d..71a6189990 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -296,6 +296,8 @@ class DisplayBuffer { void set_pages(std::vector pages); + const DisplayPage *get_active_page() const { return this->page_; } + /// Internal method to set the display rotation with. void set_rotation(DisplayRotation rotation); @@ -448,5 +450,17 @@ template class DisplayPageShowPrevAction : public Action DisplayBuffer *buffer_; }; +template class DisplayIsDisplayingPageCondition : public Condition { + public: + DisplayIsDisplayingPageCondition(DisplayBuffer *parent) : parent_(parent) {} + + void set_page(DisplayPage *page) { this->page_ = page; } + bool check(Ts... x) override { return this->parent_->get_active_page() == this->page_; } + + protected: + DisplayBuffer *parent_; + DisplayPage *page_; +}; + } // namespace display } // namespace esphome diff --git a/tests/test1.yaml b/tests/test1.yaml index e9b4cdf67c..41f688e4e8 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1760,6 +1760,13 @@ interval: btn_left_state = ((uint32_t) id(btn_left)->get_value() + 63 * (uint32_t)btn_left_state) >> 6; id(btn_left)->set_threshold(btn_left_state * 0.9); + - if: + condition: + display.is_displaying_page: + id: display1 + page_id: page1 + then: + - logger.log: 'Seeing page 1' color: - id: kbx_red