From d50e292285d4149f896113f0e8046c788e32d93d Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 10 Nov 2018 14:05:29 +0100 Subject: [PATCH] Make Switch not inherit from Component --- src/esphomelib/application.cpp | 6 +++--- src/esphomelib/switch_/gpio_switch.cpp | 2 +- src/esphomelib/switch_/gpio_switch.h | 2 +- src/esphomelib/switch_/output_switch.cpp | 3 +++ src/esphomelib/switch_/output_switch.h | 3 ++- src/esphomelib/switch_/restart_switch.cpp | 2 +- src/esphomelib/switch_/switch.cpp | 3 --- src/esphomelib/switch_/switch.h | 4 +--- src/esphomelib/switch_/template_switch.cpp | 2 +- src/esphomelib/switch_/template_switch.h | 2 +- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/esphomelib/application.cpp b/src/esphomelib/application.cpp index 4dff2c01..8c701f48 100644 --- a/src/esphomelib/application.cpp +++ b/src/esphomelib/application.cpp @@ -518,7 +518,7 @@ Application::MakeStatusBinarySensor Application::make_status_binary_sensor(const #ifdef USE_RESTART_SWITCH Application::MakeRestartSwitch Application::make_restart_switch(const std::string &friendly_name) { - auto *switch_ = this->register_component(new RestartSwitch(friendly_name)); // not a component + auto *switch_ = new RestartSwitch(friendly_name); // not a component return MakeRestartSwitch{ .restart = switch_, .mqtt = this->register_switch(switch_), @@ -528,7 +528,7 @@ Application::MakeRestartSwitch Application::make_restart_switch(const std::strin #ifdef USE_SHUTDOWN_SWITCH Application::MakeShutdownSwitch Application::make_shutdown_switch(const std::string &friendly_name) { - auto *switch_ = this->register_component(new ShutdownSwitch(friendly_name)); + auto *switch_ = new ShutdownSwitch(friendly_name); return MakeShutdownSwitch{ .shutdown = switch_, .mqtt = this->register_switch(switch_), @@ -954,7 +954,7 @@ PN532Component *Application::make_pn532_component(SPIComponent *parent, Application::MakeUARTSwitch Application::make_uart_switch(UARTComponent *parent, const std::string &name, const std::vector &data) { - auto *uart = this->register_component(new UARTSwitch(parent, name, data)); + auto *uart = new UARTSwitch(parent, name, data); return MakeUARTSwitch{ .uart = uart, diff --git a/src/esphomelib/switch_/gpio_switch.cpp b/src/esphomelib/switch_/gpio_switch.cpp index 64bda08c..cdb13646 100644 --- a/src/esphomelib/switch_/gpio_switch.cpp +++ b/src/esphomelib/switch_/gpio_switch.cpp @@ -12,7 +12,7 @@ namespace switch_ { static const char *TAG = "switch.gpio"; GPIOSwitch::GPIOSwitch(const std::string &name, GPIOPin *pin) - : Switch(name), pin_(pin) { + : Switch(name), Component(), pin_(pin) { } diff --git a/src/esphomelib/switch_/gpio_switch.h b/src/esphomelib/switch_/gpio_switch.h index 7150aeae..3e256a5d 100644 --- a/src/esphomelib/switch_/gpio_switch.h +++ b/src/esphomelib/switch_/gpio_switch.h @@ -13,7 +13,7 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { -class GPIOSwitch : public Switch { +class GPIOSwitch : public Switch, public Component { public: GPIOSwitch(const std::string &name, GPIOPin *pin); diff --git a/src/esphomelib/switch_/output_switch.cpp b/src/esphomelib/switch_/output_switch.cpp index ab41afc0..7b4ad560 100644 --- a/src/esphomelib/switch_/output_switch.cpp +++ b/src/esphomelib/switch_/output_switch.cpp @@ -33,6 +33,9 @@ void OutputSwitch::setup() { this->turn_off(); } } +float OutputSwitch::get_setup_priority() const { + return setup_priority::HARDWARE; +} } // namespace switch_ diff --git a/src/esphomelib/switch_/output_switch.h b/src/esphomelib/switch_/output_switch.h index 769ae574..ae8e5cfd 100644 --- a/src/esphomelib/switch_/output_switch.h +++ b/src/esphomelib/switch_/output_switch.h @@ -13,7 +13,7 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { /// A simple switch that exposes a binary output as a switch. -class OutputSwitch : public Switch { +class OutputSwitch : public Switch, public Component { public: /// Construct this SimpleSwitch with the provided BinaryOutput. explicit OutputSwitch(const std::string &name, output::BinaryOutput *output); @@ -22,6 +22,7 @@ class OutputSwitch : public Switch { // (In most use cases you won't need these) void setup() override; + float get_setup_priority() const override; protected: void write_state(bool state) override; diff --git a/src/esphomelib/switch_/restart_switch.cpp b/src/esphomelib/switch_/restart_switch.cpp index 874dfa6f..37af0c28 100644 --- a/src/esphomelib/switch_/restart_switch.cpp +++ b/src/esphomelib/switch_/restart_switch.cpp @@ -16,7 +16,7 @@ std::string RestartSwitch::icon() { return "mdi:restart"; } RestartSwitch::RestartSwitch(const std::string &name) -: Switch(name) { + : Switch(name) { } void RestartSwitch::write_state(bool state) { diff --git a/src/esphomelib/switch_/switch.cpp b/src/esphomelib/switch_/switch.cpp index 0e43d825..72359c7f 100644 --- a/src/esphomelib/switch_/switch.cpp +++ b/src/esphomelib/switch_/switch.cpp @@ -41,9 +41,6 @@ void Switch::toggle() { ESP_LOGD(TAG, "'%s' Toggling %s.", this->get_name().c_str(), this->state ? "OFF" : "ON"); this->write_state(this->inverted_ == this->state); } -float Switch::get_setup_priority() const { - return setup_priority::HARDWARE - 1.0f; -} optional Switch::get_initial_state() { this->rtc_ = global_preferences.make_preference(4, 2704004739UL); if (!this->rtc_.load()) diff --git a/src/esphomelib/switch_/switch.h b/src/esphomelib/switch_/switch.h index 2b5e5b73..9e5b9d2e 100644 --- a/src/esphomelib/switch_/switch.h +++ b/src/esphomelib/switch_/switch.h @@ -26,7 +26,7 @@ class TurnOnAction; * A switch is basically just a combination of a binary sensor (for reporting switch values) * and a write_state method that writes a state to the hardware. */ -class Switch : public Component, public Nameable { +class Switch : public Nameable { public: explicit Switch(const std::string &name); @@ -91,8 +91,6 @@ class Switch : public Component, public Nameable { */ void add_on_state_callback(std::function &&callback); - float get_setup_priority() const override; - optional get_initial_state(); /** Return whether this switch is optimistic - i.e. if both the ON/OFF actions should be displayed in Home Assistant diff --git a/src/esphomelib/switch_/template_switch.cpp b/src/esphomelib/switch_/template_switch.cpp index 8b3177ef..722d10c9 100644 --- a/src/esphomelib/switch_/template_switch.cpp +++ b/src/esphomelib/switch_/template_switch.cpp @@ -9,7 +9,7 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { TemplateSwitch::TemplateSwitch(const std::string &name) - : Switch(name), turn_on_trigger_(new Trigger()), turn_off_trigger_(new Trigger()) { + : Switch(name), Component(), turn_on_trigger_(new Trigger()), turn_off_trigger_(new Trigger()) { } void TemplateSwitch::loop() { diff --git a/src/esphomelib/switch_/template_switch.h b/src/esphomelib/switch_/template_switch.h index 1ae1e35e..2d1fc12f 100644 --- a/src/esphomelib/switch_/template_switch.h +++ b/src/esphomelib/switch_/template_switch.h @@ -12,7 +12,7 @@ ESPHOMELIB_NAMESPACE_BEGIN namespace switch_ { -class TemplateSwitch : public Switch { +class TemplateSwitch : public Switch, public Component { public: explicit TemplateSwitch(const std::string &name);