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
This commit is contained in:
Stanislav Meduna 2021-04-03 04:00:41 +02:00 committed by GitHub
parent 9e23987db8
commit 808e3be324
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View file

@ -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)

View file

@ -296,6 +296,8 @@ class DisplayBuffer {
void set_pages(std::vector<DisplayPage *> 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<typename... Ts> class DisplayPageShowPrevAction : public Action<Ts...>
DisplayBuffer *buffer_;
};
template<typename... Ts> class DisplayIsDisplayingPageCondition : public Condition<Ts...> {
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

View file

@ -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