mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[online_image] Use register_apply_action for the set_url and release actions (#19547)
This commit is contained in:
@@ -14,8 +14,7 @@ from esphome.const import (
|
||||
CONF_TYPE,
|
||||
CONF_URL,
|
||||
)
|
||||
from esphome.core import ID, Lambda
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.core import Lambda
|
||||
from esphome.types import ConfigType
|
||||
|
||||
AUTO_LOAD = ["runtime_image"]
|
||||
@@ -31,14 +30,6 @@ OnlineImage = online_image_ns.class_(
|
||||
"OnlineImage", cg.PollingComponent, runtime_image.RuntimeImage
|
||||
)
|
||||
|
||||
# Actions
|
||||
SetUrlAction = online_image_ns.class_(
|
||||
"OnlineImageSetUrlAction", automation.Action, cg.Parented.template(OnlineImage)
|
||||
)
|
||||
ReleaseImageAction = online_image_ns.class_(
|
||||
"OnlineImageReleaseAction", automation.Action, cg.Parented.template(OnlineImage)
|
||||
)
|
||||
|
||||
ONLINE_IMAGE_SCHEMA = (
|
||||
runtime_image.runtime_image_schema(OnlineImage)
|
||||
.extend(
|
||||
@@ -90,31 +81,18 @@ RELEASE_IMAGE_SCHEMA = automation.maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"online_image.set_url", SetUrlAction, SET_URL_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"online_image.set_url",
|
||||
SET_URL_SCHEMA,
|
||||
automation.ApplyField(CONF_URL, "set_url", cg.std_string),
|
||||
automation.ApplyField(CONF_UPDATE, "update_if", cg.bool_),
|
||||
)
|
||||
@automation.register_action(
|
||||
"online_image.release",
|
||||
ReleaseImageAction,
|
||||
RELEASE_IMAGE_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
async def online_image_action_to_code(
|
||||
config: ConfigType,
|
||||
action_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
if CONF_URL in config:
|
||||
template_ = await cg.templatable(config[CONF_URL], args, cg.std_string)
|
||||
cg.add(var.set_url(template_))
|
||||
if CONF_UPDATE in config:
|
||||
template_ = await cg.templatable(config[CONF_UPDATE], args, cg.bool_)
|
||||
cg.add(var.set_update(template_))
|
||||
return var
|
||||
automation.register_apply_action(
|
||||
"online_image.release",
|
||||
RELEASE_IMAGE_SCHEMA,
|
||||
automation.ApplyCall("release()"),
|
||||
)
|
||||
|
||||
|
||||
_CALLBACK_AUTOMATIONS = (
|
||||
|
||||
@@ -43,6 +43,11 @@ class OnlineImage final : public PollingComponent,
|
||||
bool is_big_endian = false);
|
||||
|
||||
void update() override;
|
||||
/** Download now when `update` is true (the `update` flag of `online_image.set_url`). */
|
||||
void update_if(bool update) {
|
||||
if (update)
|
||||
this->update();
|
||||
}
|
||||
void loop() override;
|
||||
|
||||
/** Set the URL to download the image from. */
|
||||
@@ -104,29 +109,4 @@ class OnlineImage final : public PollingComponent,
|
||||
uint32_t start_time_{0};
|
||||
};
|
||||
|
||||
template<typename... Ts> class OnlineImageSetUrlAction final : public Action<Ts...> {
|
||||
public:
|
||||
OnlineImageSetUrlAction(OnlineImage *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(std::string, url)
|
||||
TEMPLATABLE_VALUE(bool, update)
|
||||
void play(const Ts &...x) override {
|
||||
this->parent_->set_url(this->url_.value(x...));
|
||||
if (this->update_.value(x...)) {
|
||||
this->parent_->update();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
OnlineImage *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class OnlineImageReleaseAction final : public Action<Ts...> {
|
||||
public:
|
||||
OnlineImageReleaseAction(OnlineImage *parent) : parent_(parent) {}
|
||||
void play(const Ts &...x) override { this->parent_->release(); }
|
||||
|
||||
protected:
|
||||
OnlineImage *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::online_image
|
||||
|
||||
@@ -84,3 +84,14 @@ esphome:
|
||||
id: online_rgba_image
|
||||
url: !lambda |-
|
||||
return str_sprintf("http://homeassistant.local:8123");
|
||||
- online_image.set_url:
|
||||
id: online_rgba_image
|
||||
url: http://www.example.org/example.png
|
||||
update: false
|
||||
- online_image.set_url:
|
||||
id: online_rgba_image
|
||||
url: http://www.example.org/example.png
|
||||
update: !lambda return id(online_rgba_image).get_width() > 0;
|
||||
- online_image.release: online_rgba_image
|
||||
- online_image.release:
|
||||
id: online_rgb24_image
|
||||
|
||||
Reference in New Issue
Block a user