mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
[touchscreen] Allow binary sensor to have multiple pages in config (#7112)
* [touchscreen] Allow binary sensor to have multiple pages in config * Sort imports
This commit is contained in:
parent
caa2ea64e3
commit
d7231fadb1
3 changed files with 31 additions and 14 deletions
|
@ -1,10 +1,9 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
|
||||||
|
|
||||||
from esphome.components import binary_sensor, display
|
from esphome.components import binary_sensor, display
|
||||||
from esphome.const import CONF_PAGE_ID
|
import esphome.config_validation as cv
|
||||||
|
from esphome.const import CONF_PAGE_ID, CONF_PAGES
|
||||||
|
|
||||||
from .. import touchscreen_ns, CONF_TOUCHSCREEN_ID, Touchscreen, TouchListener
|
from .. import CONF_TOUCHSCREEN_ID, TouchListener, Touchscreen, touchscreen_ns
|
||||||
|
|
||||||
DEPENDENCIES = ["touchscreen"]
|
DEPENDENCIES = ["touchscreen"]
|
||||||
|
|
||||||
|
@ -22,7 +21,7 @@ CONF_Y_MIN = "y_min"
|
||||||
CONF_Y_MAX = "y_max"
|
CONF_Y_MAX = "y_max"
|
||||||
|
|
||||||
|
|
||||||
def validate_coords(config):
|
def _validate_coords(config):
|
||||||
if (
|
if (
|
||||||
config[CONF_X_MAX] < config[CONF_X_MIN]
|
config[CONF_X_MAX] < config[CONF_X_MIN]
|
||||||
or config[CONF_Y_MAX] < config[CONF_Y_MIN]
|
or config[CONF_Y_MAX] < config[CONF_Y_MIN]
|
||||||
|
@ -33,6 +32,15 @@ def validate_coords(config):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def _set_pages(config: dict) -> dict:
|
||||||
|
if CONF_PAGES in config or CONF_PAGE_ID not in config:
|
||||||
|
return config
|
||||||
|
|
||||||
|
config = config.copy()
|
||||||
|
config[CONF_PAGES] = [config.pop(CONF_PAGE_ID)]
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
binary_sensor.binary_sensor_schema(TouchscreenBinarySensor)
|
binary_sensor.binary_sensor_schema(TouchscreenBinarySensor)
|
||||||
.extend(
|
.extend(
|
||||||
|
@ -42,11 +50,17 @@ CONFIG_SCHEMA = cv.All(
|
||||||
cv.Required(CONF_X_MAX): cv.int_range(min=0, max=2000),
|
cv.Required(CONF_X_MAX): cv.int_range(min=0, max=2000),
|
||||||
cv.Required(CONF_Y_MIN): cv.int_range(min=0, max=2000),
|
cv.Required(CONF_Y_MIN): cv.int_range(min=0, max=2000),
|
||||||
cv.Required(CONF_Y_MAX): cv.int_range(min=0, max=2000),
|
cv.Required(CONF_Y_MAX): cv.int_range(min=0, max=2000),
|
||||||
cv.Optional(CONF_PAGE_ID): cv.use_id(display.DisplayPage),
|
cv.Exclusive(CONF_PAGE_ID, group_of_exclusion=CONF_PAGES): cv.use_id(
|
||||||
|
display.DisplayPage
|
||||||
|
),
|
||||||
|
cv.Exclusive(CONF_PAGES, group_of_exclusion=CONF_PAGES): cv.ensure_list(
|
||||||
|
cv.use_id(display.DisplayPage)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA),
|
.extend(cv.COMPONENT_SCHEMA),
|
||||||
validate_coords,
|
_validate_coords,
|
||||||
|
_set_pages,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,6 +78,6 @@ async def to_code(config):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if CONF_PAGE_ID in config:
|
for page_id in config.get(CONF_PAGES, []):
|
||||||
page = await cg.get_variable(config[CONF_PAGE_ID])
|
page = await cg.get_variable(page_id)
|
||||||
cg.add(var.set_page(page))
|
cg.add(var.add_page(page))
|
||||||
|
|
|
@ -11,8 +11,9 @@ void TouchscreenBinarySensor::setup() {
|
||||||
void TouchscreenBinarySensor::touch(TouchPoint tp) {
|
void TouchscreenBinarySensor::touch(TouchPoint tp) {
|
||||||
bool touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_);
|
bool touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_);
|
||||||
|
|
||||||
if (this->page_ != nullptr) {
|
if (!this->pages_.empty()) {
|
||||||
touched &= this->page_ == this->parent_->get_display()->get_active_page();
|
auto *current_page = this->parent_->get_display()->get_active_page();
|
||||||
|
touched &= std::find(this->pages_.begin(), this->pages_.end(), current_page) != this->pages_.end();
|
||||||
}
|
}
|
||||||
if (touched) {
|
if (touched) {
|
||||||
this->publish_state(true);
|
this->publish_state(true);
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace touchscreen {
|
namespace touchscreen {
|
||||||
|
|
||||||
|
@ -30,14 +32,14 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor,
|
||||||
int16_t get_width() { return this->x_max_ - this->x_min_; }
|
int16_t get_width() { return this->x_max_ - this->x_min_; }
|
||||||
int16_t get_height() { return this->y_max_ - this->y_min_; }
|
int16_t get_height() { return this->y_max_ - this->y_min_; }
|
||||||
|
|
||||||
void set_page(display::DisplayPage *page) { this->page_ = page; }
|
void add_page(display::DisplayPage *page) { this->pages_.push_back(page); }
|
||||||
|
|
||||||
void touch(TouchPoint tp) override;
|
void touch(TouchPoint tp) override;
|
||||||
void release() override;
|
void release() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int16_t x_min_, x_max_, y_min_, y_max_;
|
int16_t x_min_, x_max_, y_min_, y_max_;
|
||||||
display::DisplayPage *page_{nullptr};
|
std::vector<display::DisplayPage *> pages_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace touchscreen
|
} // namespace touchscreen
|
||||||
|
|
Loading…
Reference in a new issue