mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +01:00
[online_image] add option to show placeholder while downloading (#7083)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
2a70ef05d1
commit
506e69addf
3 changed files with 25 additions and 0 deletions
|
@ -27,6 +27,7 @@ CODEOWNERS = ["@guillempages"]
|
||||||
MULTI_CONF = True
|
MULTI_CONF = True
|
||||||
|
|
||||||
CONF_ON_DOWNLOAD_FINISHED = "on_download_finished"
|
CONF_ON_DOWNLOAD_FINISHED = "on_download_finished"
|
||||||
|
CONF_PLACEHOLDER = "placeholder"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -73,6 +74,7 @@ ONLINE_IMAGE_SCHEMA = cv.Schema(
|
||||||
#
|
#
|
||||||
cv.Required(CONF_URL): cv.url,
|
cv.Required(CONF_URL): cv.url,
|
||||||
cv.Required(CONF_FORMAT): cv.enum(IMAGE_FORMAT, upper=True),
|
cv.Required(CONF_FORMAT): cv.enum(IMAGE_FORMAT, upper=True),
|
||||||
|
cv.Optional(CONF_PLACEHOLDER): cv.use_id(Image_),
|
||||||
cv.Optional(CONF_BUFFER_SIZE, default=2048): cv.int_range(256, 65536),
|
cv.Optional(CONF_BUFFER_SIZE, default=2048): cv.int_range(256, 65536),
|
||||||
cv.Optional(CONF_ON_DOWNLOAD_FINISHED): automation.validate_automation(
|
cv.Optional(CONF_ON_DOWNLOAD_FINISHED): automation.validate_automation(
|
||||||
{
|
{
|
||||||
|
@ -152,6 +154,10 @@ async def to_code(config):
|
||||||
|
|
||||||
cg.add(var.set_transparency(transparent))
|
cg.add(var.set_transparency(transparent))
|
||||||
|
|
||||||
|
if placeholder_id := config.get(CONF_PLACEHOLDER):
|
||||||
|
placeholder = await cg.get_variable(placeholder_id)
|
||||||
|
cg.add(var.set_placeholder(placeholder))
|
||||||
|
|
||||||
for conf in config.get(CONF_ON_DOWNLOAD_FINISHED, []):
|
for conf in config.get(CONF_ON_DOWNLOAD_FINISHED, []):
|
||||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||||
await automation.build_automation(trigger, [], conf)
|
await automation.build_automation(trigger, [], conf)
|
||||||
|
|
|
@ -35,6 +35,14 @@ OnlineImage::OnlineImage(const std::string &url, int width, int height, ImageFor
|
||||||
this->set_url(url);
|
this->set_url(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OnlineImage::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
|
||||||
|
if (this->data_start_) {
|
||||||
|
Image::draw(x, y, display, color_on, color_off);
|
||||||
|
} else if (this->placeholder_) {
|
||||||
|
this->placeholder_->draw(x, y, display, color_on, color_off);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void OnlineImage::release() {
|
void OnlineImage::release() {
|
||||||
if (this->buffer_) {
|
if (this->buffer_) {
|
||||||
ESP_LOGD(TAG, "Deallocating old buffer...");
|
ESP_LOGD(TAG, "Deallocating old buffer...");
|
||||||
|
|
|
@ -50,6 +50,8 @@ class OnlineImage : public PollingComponent,
|
||||||
OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type,
|
OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type,
|
||||||
uint32_t buffer_size);
|
uint32_t buffer_size);
|
||||||
|
|
||||||
|
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
|
||||||
|
|
||||||
void update() override;
|
void update() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
|
|
||||||
|
@ -60,6 +62,14 @@ class OnlineImage : public PollingComponent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the image that needs to be shown as long as the downloaded image
|
||||||
|
* is not available.
|
||||||
|
*
|
||||||
|
* @param placeholder Pointer to the (@link Image) to show as placeholder.
|
||||||
|
*/
|
||||||
|
void set_placeholder(image::Image *placeholder) { this->placeholder_ = placeholder; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Release the buffer storing the image. The image will need to be downloaded again
|
* Release the buffer storing the image. The image will need to be downloaded again
|
||||||
* to be able to be displayed.
|
* to be able to be displayed.
|
||||||
|
@ -113,6 +123,7 @@ class OnlineImage : public PollingComponent,
|
||||||
DownloadBuffer download_buffer_;
|
DownloadBuffer download_buffer_;
|
||||||
|
|
||||||
const ImageFormat format_;
|
const ImageFormat format_;
|
||||||
|
image::Image *placeholder_{nullptr};
|
||||||
|
|
||||||
std::string url_{""};
|
std::string url_{""};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue