mirror of
https://github.com/esphome/esphome.git
synced 2026-11-15 17:45:57 +00:00
Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40a49ae72a | ||
|
|
14241cb5b9 | ||
|
|
95da6f4fc8 | ||
|
|
df789d2cb5 | ||
|
|
ddd0d14187 | ||
|
|
67b814d77a | ||
|
|
027c21ecc1 | ||
|
|
ff03570051 | ||
|
|
64a8e0e0b0 | ||
|
|
d665fd297d | ||
|
|
cbecf1c1ff | ||
|
|
ce243ce4dd |
No files matched your search
@@ -11,13 +11,12 @@ from esphome.const import (
|
||||
CONF_ON_ERROR,
|
||||
CONF_OTA,
|
||||
CONF_PLATFORM,
|
||||
CONF_TRIGGER_ID,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
from esphome.coroutine import CoroPriority
|
||||
from esphome.cpp_generator import MockObj
|
||||
from esphome.types import ConfigType
|
||||
from esphome.types import ConfigType, TemplateArgsType
|
||||
|
||||
OTA_STATE_LISTENER_KEY = "ota_state_listener"
|
||||
|
||||
@@ -45,14 +44,25 @@ CONF_ON_PROGRESS = "on_progress"
|
||||
ota_ns = cg.esphome_ns.namespace("ota")
|
||||
OTAComponent = ota_ns.class_("OTAComponent", cg.Component)
|
||||
OTAState = ota_ns.enum("OTAState")
|
||||
OTAAbortTrigger = ota_ns.class_("OTAAbortTrigger", automation.Trigger.template())
|
||||
OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template())
|
||||
OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template())
|
||||
OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.template())
|
||||
OTAStartTrigger = ota_ns.class_("OTAStartTrigger", automation.Trigger.template())
|
||||
OTAStateChangeTrigger = ota_ns.class_(
|
||||
"OTAStateChangeTrigger", automation.Trigger.template()
|
||||
)
|
||||
|
||||
_request_state_callback_slot = cg.slot_counter("OTA_STATE_CALLBACK_COUNT")
|
||||
|
||||
# Callback parameters, shared by every key and every OTA component
|
||||
_STATE_CALLBACK_PARAMS: TemplateArgsType = [
|
||||
(OTAState, "state"),
|
||||
(cg.float_, "progress"),
|
||||
(cg.uint8, "error"),
|
||||
(OTAComponent.operator("ptr"), "component"),
|
||||
]
|
||||
# conf key: (automation args, forwarded expressions, state filter)
|
||||
_STATE_AUTOMATIONS: dict[str, tuple[TemplateArgsType, list[str], MockObj | None]] = {
|
||||
CONF_ON_STATE_CHANGE: ([(OTAState, "state")], ["state"], None),
|
||||
CONF_ON_ABORT: ([], [], ota_ns.OTA_ABORT),
|
||||
CONF_ON_BEGIN: ([], [], ota_ns.OTA_STARTED),
|
||||
CONF_ON_PROGRESS: ([(cg.float_, "x")], ["progress"], ota_ns.OTA_IN_PROGRESS),
|
||||
CONF_ON_END: ([], [], ota_ns.OTA_COMPLETED),
|
||||
CONF_ON_ERROR: ([(cg.uint8, "x")], ["error"], ota_ns.OTA_ERROR),
|
||||
}
|
||||
|
||||
|
||||
def _ota_final_validate(config: ConfigType) -> None:
|
||||
@@ -65,38 +75,7 @@ def _ota_final_validate(config: ConfigType) -> None:
|
||||
FINAL_VALIDATE_SCHEMA = _ota_final_validate
|
||||
|
||||
BASE_OTA_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_ON_STATE_CHANGE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStateChangeTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_ABORT): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAAbortTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_BEGIN): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAStartTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_END): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAEndTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_ERROR): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAErrorTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PROGRESS): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OTAProgressTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
{cv.Optional(key): automation.validate_automation({}) for key in _STATE_AUTOMATIONS}
|
||||
)
|
||||
|
||||
|
||||
@@ -112,30 +91,18 @@ async def to_code(config: ConfigType) -> None:
|
||||
async def ota_to_code(var: MockObj, config: ConfigType) -> None:
|
||||
await cg.past_safe_mode()
|
||||
use_state_callback = False
|
||||
for conf in config.get(CONF_ON_STATE_CHANGE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(OTAState, "state")], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_ABORT, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_BEGIN, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_PROGRESS, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(float, "x")], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_END, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
use_state_callback = True
|
||||
for conf in config.get(CONF_ON_ERROR, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(cg.uint8, "x")], conf)
|
||||
use_state_callback = True
|
||||
for key, (args, forward, state) in _STATE_AUTOMATIONS.items():
|
||||
# Checked per callback, as each trigger did, so one that marks it failed stops the rest
|
||||
when = "component == {parent} && !component->is_failed()"
|
||||
if state is not None:
|
||||
when += f" && state == {state}"
|
||||
for conf in config.get(key, []):
|
||||
_request_state_callback_slot()
|
||||
callback = await automation.build_trigger_callback(
|
||||
args, conf, _STATE_CALLBACK_PARAMS, forward, when, var
|
||||
)
|
||||
cg.add(MockObj(f"{OTAComponent}::add_state_callback")(callback))
|
||||
use_state_callback = True
|
||||
if use_state_callback:
|
||||
request_ota_state_listeners()
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
#pragma once
|
||||
#ifdef USE_OTA_STATE_LISTENER
|
||||
#include "ota_backend.h"
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
|
||||
namespace esphome::ota {
|
||||
|
||||
class OTAStateChangeTrigger final : public Trigger<OTAState>, public OTAStateListener {
|
||||
public:
|
||||
explicit OTAStateChangeTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
|
||||
|
||||
void on_ota_state(OTAState state, float progress, uint8_t error) override {
|
||||
if (!this->parent_->is_failed()) {
|
||||
this->trigger(state);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
OTAComponent *parent_;
|
||||
};
|
||||
|
||||
template<OTAState State> class OTAStateTrigger final : public Trigger<>, public OTAStateListener {
|
||||
public:
|
||||
explicit OTAStateTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
|
||||
|
||||
void on_ota_state(OTAState state, float progress, uint8_t error) override {
|
||||
if (state == State && !this->parent_->is_failed()) {
|
||||
this->trigger();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
OTAComponent *parent_;
|
||||
};
|
||||
|
||||
using OTAStartTrigger = OTAStateTrigger<OTA_STARTED>;
|
||||
using OTAEndTrigger = OTAStateTrigger<OTA_COMPLETED>;
|
||||
using OTAAbortTrigger = OTAStateTrigger<OTA_ABORT>;
|
||||
|
||||
class OTAProgressTrigger final : public Trigger<float>, public OTAStateListener {
|
||||
public:
|
||||
explicit OTAProgressTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
|
||||
|
||||
void on_ota_state(OTAState state, float progress, uint8_t error) override {
|
||||
if (state == OTA_IN_PROGRESS && !this->parent_->is_failed()) {
|
||||
this->trigger(progress);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
OTAComponent *parent_;
|
||||
};
|
||||
|
||||
class OTAErrorTrigger final : public Trigger<uint8_t>, public OTAStateListener {
|
||||
public:
|
||||
explicit OTAErrorTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
|
||||
|
||||
void on_ota_state(OTAState state, float progress, uint8_t error) override {
|
||||
if (state == OTA_ERROR && !this->parent_->is_failed()) {
|
||||
this->trigger(error);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
OTAComponent *parent_;
|
||||
};
|
||||
|
||||
} // namespace esphome::ota
|
||||
#endif
|
||||
@@ -89,6 +89,12 @@ void UpdateLock::release() {
|
||||
void UpdateLock::force_release() { update_lock_clear(); }
|
||||
|
||||
#ifdef USE_OTA_STATE_LISTENER
|
||||
#ifdef OTA_STATE_CALLBACK_COUNT
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
StaticCallbackManager<OTA_STATE_CALLBACK_COUNT, void(OTAState, float, uint8_t, OTAComponent *)>
|
||||
OTAComponent::state_callbacks;
|
||||
#endif
|
||||
|
||||
OTAGlobalCallback *global_ota_callback{nullptr}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
OTAGlobalCallback *get_global_ota_callback() {
|
||||
@@ -112,6 +118,9 @@ void OTAComponent::notify_state_deferred_(OTAState state, float progress, uint8_
|
||||
}
|
||||
|
||||
void OTAComponent::notify_state_(OTAState state, float progress, uint8_t error) {
|
||||
#ifdef OTA_STATE_CALLBACK_COUNT
|
||||
state_callbacks.call(state, progress, error, this);
|
||||
#endif
|
||||
for (auto *listener : this->state_listeners_) {
|
||||
listener->on_ota_state(state, progress, error);
|
||||
}
|
||||
|
||||
@@ -160,6 +160,12 @@ class OTAComponent : public Component {
|
||||
#ifdef USE_OTA_STATE_LISTENER
|
||||
public:
|
||||
void add_state_listener(OTAStateListener *listener) { this->state_listeners_.push_back(listener); }
|
||||
#ifdef OTA_STATE_CALLBACK_COUNT
|
||||
/// For the generated on_* automations: codegen sizes the list, so other code should use add_state_listener().
|
||||
/// One list serves every OTA component and runs before the listeners; each callback checks component and
|
||||
/// is_failed() itself.
|
||||
template<typename F> static void add_state_callback(F &&callback) { state_callbacks.add(std::forward<F>(callback)); }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
void notify_state_(OTAState state, float progress, uint8_t error);
|
||||
@@ -172,6 +178,10 @@ class OTAComponent : public Component {
|
||||
void notify_state_deferred_(OTAState state, float progress, uint8_t error);
|
||||
|
||||
std::vector<OTAStateListener *> state_listeners_;
|
||||
#ifdef OTA_STATE_CALLBACK_COUNT
|
||||
static StaticCallbackManager<OTA_STATE_CALLBACK_COUNT, void(OTAState, float, uint8_t, OTAComponent *)>
|
||||
state_callbacks; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -321,6 +321,7 @@
|
||||
#define USE_NEXTION_WAVEFORM
|
||||
#define USE_NUMBER
|
||||
#define USE_OTA_STATE_LISTENER
|
||||
#define OTA_STATE_CALLBACK_COUNT 2
|
||||
#define USE_OUTPUT
|
||||
#define USE_OUTPUT_FLOAT_POWER_SCALING
|
||||
#define PACKET_INTERFACE_ADDRESSING_PORT_COUNT 2
|
||||
|
||||
@@ -159,5 +159,5 @@ def test_safe_mode_keeps_the_ble_trigger(
|
||||
"App.register_component_(adafruit_ble_otacomponent_id",
|
||||
):
|
||||
assert main_cpp.index(registration) < guard
|
||||
for automation in ("->add_passkey_callback(", "new(ota_otastarttrigger_id)"):
|
||||
for automation in ("->add_passkey_callback(", "OTAComponent::add_state_callback("):
|
||||
assert main_cpp.index(automation) > guard
|
||||
@@ -41,9 +41,26 @@ def test_ota_automations_generated(generate_main: Callable[[str], str]) -> None:
|
||||
assert "USE_OTA_STATE_LISTENER" in _defines()
|
||||
|
||||
|
||||
def test_ota_automations_are_callbacks(generate_main: Callable[[str], str]) -> None:
|
||||
"""Each automation is one callback on the list every OTA component shares,
|
||||
filtered on its component and state; the list is sized for all of them."""
|
||||
main_cpp = generate_main(AUTOMATIONS_YAML)
|
||||
assert main_cpp.count("ota::OTAComponent::add_state_callback(") == 10
|
||||
assert main_cpp.count("component == ::ota_native") == 7
|
||||
assert main_cpp.count("component == ::ota_web") == 2
|
||||
assert main_cpp.count("component == ::ota_http") == 1
|
||||
assert main_cpp.count("!component->is_failed()") == 10
|
||||
assert "Trigger" not in main_cpp
|
||||
assert "__fn(progress);" in main_cpp
|
||||
assert "__fn(error);" in main_cpp
|
||||
assert "__fn(state);" in main_cpp
|
||||
counts = {d.name: str(d.value) for d in CORE.defines}
|
||||
assert counts["OTA_STATE_CALLBACK_COUNT"] == "10"
|
||||
|
||||
|
||||
def test_ota_without_automations_skips_state_listener(
|
||||
generate_main: Callable[[str], str],
|
||||
) -> None:
|
||||
"""Without automations or listeners the state notifications compile out."""
|
||||
generate_main(PLAIN_YAML)
|
||||
assert "USE_OTA_STATE_LISTENER" not in _defines()
|
||||
assert not {"USE_OTA_STATE_LISTENER", "OTA_STATE_CALLBACK_COUNT"} & _defines()
|
||||
@@ -0,0 +1,12 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.types import ConfigType
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# The real to_code requests these from ota_to_code when on_* automations are configured
|
||||
async def to_code_testing(config: ConfigType) -> None:
|
||||
cg.add_define("USE_OTA_STATE_LISTENER")
|
||||
cg.add_define("OTA_STATE_CALLBACK_COUNT", 5)
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
@@ -0,0 +1,99 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/ota/ota_backend.h"
|
||||
|
||||
namespace esphome::ota::testing {
|
||||
|
||||
struct Event {
|
||||
char source;
|
||||
OTAState state;
|
||||
float progress;
|
||||
uint8_t error;
|
||||
};
|
||||
|
||||
static std::vector<Event> events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
class RecordingListener : public OTAStateListener {
|
||||
public:
|
||||
void on_ota_state(OTAState state, float progress, uint8_t error) override {
|
||||
events.push_back({'l', state, progress, error});
|
||||
}
|
||||
};
|
||||
|
||||
class TestOTAComponent : public OTAComponent {
|
||||
public:
|
||||
using OTAComponent::notify_state_;
|
||||
void fail() { this->mark_failed(); }
|
||||
};
|
||||
|
||||
// The callback list is shared by every OTA component and cannot be cleared, so the
|
||||
// callbacks are registered once, each filtering on its component like the generated ones.
|
||||
static TestOTAComponent first_ota; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static TestOTAComponent second_ota; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static TestOTAComponent failed_ota; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static RecordingListener listener; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
class OTAStateCallback : public ::testing::Test {
|
||||
protected:
|
||||
static void SetUpTestSuite() {
|
||||
OTAComponent::add_state_callback([](OTAState state, float progress, uint8_t error, OTAComponent *component) {
|
||||
if (component == &first_ota)
|
||||
events.push_back({'a', state, progress, error});
|
||||
});
|
||||
OTAComponent::add_state_callback([](OTAState state, float progress, uint8_t error, OTAComponent *component) {
|
||||
if (component == &first_ota)
|
||||
events.push_back({'b', state, progress, error});
|
||||
});
|
||||
OTAComponent::add_state_callback([](OTAState state, float progress, uint8_t error, OTAComponent *component) {
|
||||
if (component == &second_ota)
|
||||
events.push_back({'s', state, progress, error});
|
||||
});
|
||||
// Like the generated callbacks: the first marks the component failed, the second checks it first
|
||||
OTAComponent::add_state_callback([](OTAState state, float progress, uint8_t error, OTAComponent *component) {
|
||||
if (component == &failed_ota && !component->is_failed()) {
|
||||
events.push_back({'f', state, progress, error});
|
||||
failed_ota.fail();
|
||||
}
|
||||
});
|
||||
OTAComponent::add_state_callback([](OTAState state, float progress, uint8_t error, OTAComponent *component) {
|
||||
if (component == &failed_ota && !component->is_failed())
|
||||
events.push_back({'g', state, progress, error});
|
||||
});
|
||||
first_ota.add_state_listener(&listener);
|
||||
failed_ota.add_state_listener(&listener);
|
||||
}
|
||||
void SetUp() override { events.clear(); }
|
||||
};
|
||||
|
||||
TEST_F(OTAStateCallback, ForwardsStateProgressAndError) {
|
||||
second_ota.notify_state_(OTA_IN_PROGRESS, 42.5f, 0);
|
||||
second_ota.notify_state_(OTA_ERROR, 0.0f, OTA_RESPONSE_ERROR_UPDATE_END);
|
||||
ASSERT_EQ(events.size(), 2U);
|
||||
EXPECT_EQ(events[0].source, 's');
|
||||
EXPECT_EQ(events[0].state, OTA_IN_PROGRESS);
|
||||
EXPECT_FLOAT_EQ(events[0].progress, 42.5f);
|
||||
EXPECT_EQ(events[1].state, OTA_ERROR);
|
||||
EXPECT_EQ(events[1].error, OTA_RESPONSE_ERROR_UPDATE_END);
|
||||
}
|
||||
|
||||
TEST_F(OTAStateCallback, EveryCallbackRunsBeforeTheListeners) {
|
||||
first_ota.notify_state_(OTA_STARTED, 0.0f, 0);
|
||||
ASSERT_EQ(events.size(), 3U);
|
||||
EXPECT_EQ(events[0].source, 'a');
|
||||
EXPECT_EQ(events[1].source, 'b');
|
||||
EXPECT_EQ(events[2].source, 'l');
|
||||
}
|
||||
|
||||
TEST_F(OTAStateCallback, MarkingFailedStopsLaterCallbacksButListenersStillRun) {
|
||||
failed_ota.notify_state_(OTA_STARTED, 0.0f, 0);
|
||||
ASSERT_EQ(events.size(), 2U);
|
||||
EXPECT_EQ(events[0].source, 'f');
|
||||
EXPECT_EQ(events[1].source, 'l');
|
||||
failed_ota.notify_state_(OTA_STARTED, 0.0f, 0);
|
||||
ASSERT_EQ(events.size(), 3U);
|
||||
EXPECT_EQ(events[2].source, 'l');
|
||||
}
|
||||
|
||||
} // namespace esphome::ota::testing
|
||||
Reference in new issue
Block a user