mirror of
https://github.com/esphome/esphome.git
synced 2026-05-31 17:06:40 +08:00
[core] Make register_component protected, remove runtime checks (#14371)
This commit is contained in:
@@ -79,24 +79,7 @@ static void insertion_sort_by_priority(Iterator first, Iterator last) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::register_component_(Component *comp) {
|
void Application::register_component_(Component *comp) { this->components_.push_back(comp); }
|
||||||
if (comp == nullptr) {
|
|
||||||
ESP_LOGW(TAG, "Tried to register null component!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto *c : this->components_) {
|
|
||||||
if (comp == c) {
|
|
||||||
ESP_LOGW(TAG, "Component %s already registered! (%p)", LOG_STR_ARG(c->get_component_log_str()), c);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this->components_.size() >= ESPHOME_COMPONENT_COUNT) {
|
|
||||||
ESP_LOGE(TAG, "Cannot register component %s - at capacity!", LOG_STR_ARG(comp->get_component_log_str()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this->components_.push_back(comp);
|
|
||||||
}
|
|
||||||
void Application::setup() {
|
void Application::setup() {
|
||||||
ESP_LOGI(TAG, "Running through setup()");
|
ESP_LOGI(TAG, "Running through setup()");
|
||||||
ESP_LOGV(TAG, "Sorting components by setup priority");
|
ESP_LOGV(TAG, "Sorting components by setup priority");
|
||||||
|
|||||||
@@ -108,6 +108,10 @@ namespace esphome::socket {
|
|||||||
class Socket;
|
class Socket;
|
||||||
} // namespace esphome::socket
|
} // namespace esphome::socket
|
||||||
|
|
||||||
|
// Forward declarations for friend access from codegen-generated setup()
|
||||||
|
void setup(); // NOLINT(readability-redundant-declaration) - may be declared in Arduino.h
|
||||||
|
void original_setup(); // NOLINT(readability-redundant-declaration) - used by cpp unit tests
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
// Teardown timeout constant (in milliseconds)
|
// Teardown timeout constant (in milliseconds)
|
||||||
@@ -247,13 +251,6 @@ class Application {
|
|||||||
|
|
||||||
/// Reserve space for components to avoid memory fragmentation
|
/// Reserve space for components to avoid memory fragmentation
|
||||||
|
|
||||||
/// Register the component in this Application instance.
|
|
||||||
template<class C> C *register_component(C *c) {
|
|
||||||
static_assert(std::is_base_of<Component, C>::value, "Only Component subclasses can be registered");
|
|
||||||
this->register_component_((Component *) c);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set up all the registered components. Call this at the end of your setup() function.
|
/// Set up all the registered components. Call this at the end of your setup() function.
|
||||||
void setup();
|
void setup();
|
||||||
|
|
||||||
@@ -508,6 +505,8 @@ class Application {
|
|||||||
protected:
|
protected:
|
||||||
friend Component;
|
friend Component;
|
||||||
friend class socket::Socket;
|
friend class socket::Socket;
|
||||||
|
friend void ::setup();
|
||||||
|
friend void ::original_setup();
|
||||||
|
|
||||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||||
/// Fast path for Socket::ready() via friendship - skips negative fd check.
|
/// Fast path for Socket::ready() via friendship - skips negative fd check.
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ async def register_component(var, config):
|
|||||||
if name is not None:
|
if name is not None:
|
||||||
add(var.set_component_source(LogStringLiteral(name)))
|
add(var.set_component_source(LogStringLiteral(name)))
|
||||||
|
|
||||||
add(App.register_component(var))
|
add(App.register_component_(var))
|
||||||
return var
|
return var
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ def test_deep_sleep_setup(generate_main):
|
|||||||
main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep1.yaml")
|
main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep1.yaml")
|
||||||
|
|
||||||
assert "deepsleep = new deep_sleep::DeepSleepComponent();" in main_cpp
|
assert "deepsleep = new deep_sleep::DeepSleepComponent();" in main_cpp
|
||||||
assert "App.register_component(deepsleep);" in main_cpp
|
assert "App.register_component_(deepsleep);" in main_cpp
|
||||||
|
|
||||||
|
|
||||||
def test_deep_sleep_sleep_duration(generate_main):
|
def test_deep_sleep_sleep_duration(generate_main):
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ def test_web_server_ota_generated(generate_main: Callable[[str], str]) -> None:
|
|||||||
assert "global_web_server_base" in main_cpp
|
assert "global_web_server_base" in main_cpp
|
||||||
|
|
||||||
# Check component is registered
|
# Check component is registered
|
||||||
assert "App.register_component(web_server_webserverotacomponent_id)" in main_cpp
|
assert "App.register_component_(web_server_webserverotacomponent_id)" in main_cpp
|
||||||
|
|
||||||
|
|
||||||
def test_web_server_ota_with_callbacks(generate_main: Callable[[str], str]) -> None:
|
def test_web_server_ota_with_callbacks(generate_main: Callable[[str], str]) -> None:
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ void setup() {
|
|||||||
auto *log = new logger::Logger(115200); // NOLINT
|
auto *log = new logger::Logger(115200); // NOLINT
|
||||||
log->pre_setup();
|
log->pre_setup();
|
||||||
log->set_uart_selection(logger::UART_SELECTION_UART0);
|
log->set_uart_selection(logger::UART_SELECTION_UART0);
|
||||||
App.register_component(log);
|
App.register_component_(log);
|
||||||
|
|
||||||
auto *wifi = new wifi::WiFiComponent(); // NOLINT
|
auto *wifi = new wifi::WiFiComponent(); // NOLINT
|
||||||
App.register_component(wifi);
|
App.register_component_(wifi);
|
||||||
wifi::WiFiAP ap;
|
wifi::WiFiAP ap;
|
||||||
ap.set_ssid("Test SSID");
|
ap.set_ssid("Test SSID");
|
||||||
ap.set_password("password1");
|
ap.set_password("password1");
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ async def test_gpio_pin_expression__conf_is_none(monkeypatch):
|
|||||||
async def test_register_component(monkeypatch):
|
async def test_register_component(monkeypatch):
|
||||||
var = Mock(base="foo.bar")
|
var = Mock(base="foo.bar")
|
||||||
|
|
||||||
app_mock = Mock(register_component=Mock(return_value=var))
|
app_mock = Mock(register_component_=Mock(return_value=var))
|
||||||
monkeypatch.setattr(ch, "App", app_mock)
|
monkeypatch.setattr(ch, "App", app_mock)
|
||||||
|
|
||||||
core_mock = Mock(component_ids=["foo.bar"])
|
core_mock = Mock(component_ids=["foo.bar"])
|
||||||
@@ -29,7 +29,7 @@ async def test_register_component(monkeypatch):
|
|||||||
|
|
||||||
assert actual is var
|
assert actual is var
|
||||||
assert add_mock.call_count == 2
|
assert add_mock.call_count == 2
|
||||||
app_mock.register_component.assert_called_with(var)
|
app_mock.register_component_.assert_called_with(var)
|
||||||
assert core_mock.component_ids == []
|
assert core_mock.component_ids == []
|
||||||
|
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ async def test_register_component__no_component_id(monkeypatch):
|
|||||||
async def test_register_component__with_setup_priority(monkeypatch):
|
async def test_register_component__with_setup_priority(monkeypatch):
|
||||||
var = Mock(base="foo.bar")
|
var = Mock(base="foo.bar")
|
||||||
|
|
||||||
app_mock = Mock(register_component=Mock(return_value=var))
|
app_mock = Mock(register_component_=Mock(return_value=var))
|
||||||
monkeypatch.setattr(ch, "App", app_mock)
|
monkeypatch.setattr(ch, "App", app_mock)
|
||||||
|
|
||||||
core_mock = Mock(component_ids=["foo.bar"])
|
core_mock = Mock(component_ids=["foo.bar"])
|
||||||
@@ -68,5 +68,5 @@ async def test_register_component__with_setup_priority(monkeypatch):
|
|||||||
assert actual is var
|
assert actual is var
|
||||||
add_mock.assert_called()
|
add_mock.assert_called()
|
||||||
assert add_mock.call_count == 4
|
assert add_mock.call_count == 4
|
||||||
app_mock.register_component.assert_called_with(var)
|
app_mock.register_component_.assert_called_with(var)
|
||||||
assert core_mock.component_ids == []
|
assert core_mock.component_ids == []
|
||||||
|
|||||||
Reference in New Issue
Block a user