Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Make Switch not inherit from Component
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter committed Nov 10, 2018
1 parent 8385328 commit d50e292
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/esphomelib/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_),
Expand All @@ -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_),
Expand Down Expand Up @@ -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<uint8_t> &data) {
auto *uart = this->register_component(new UARTSwitch(parent, name, data));
auto *uart = new UARTSwitch(parent, name, data);

return MakeUARTSwitch{
.uart = uart,
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/switch_/gpio_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}

Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/switch_/gpio_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/esphomelib/switch_/output_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void OutputSwitch::setup() {
this->turn_off();
}
}
float OutputSwitch::get_setup_priority() const {
return setup_priority::HARDWARE;
}

} // namespace switch_

Expand Down
3 changes: 2 additions & 1 deletion src/esphomelib/switch_/output_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/switch_/restart_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions src/esphomelib/switch_/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> Switch::get_initial_state() {
this->rtc_ = global_preferences.make_preference(4, 2704004739UL);
if (!this->rtc_.load())
Expand Down
4 changes: 1 addition & 3 deletions src/esphomelib/switch_/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -91,8 +91,6 @@ class Switch : public Component, public Nameable {
*/
void add_on_state_callback(std::function<void(bool)> &&callback);

float get_setup_priority() const override;

optional<bool> get_initial_state();

/** Return whether this switch is optimistic - i.e. if both the ON/OFF actions should be displayed in Home Assistant
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/switch_/template_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ESPHOMELIB_NAMESPACE_BEGIN
namespace switch_ {

TemplateSwitch::TemplateSwitch(const std::string &name)
: Switch(name), turn_on_trigger_(new Trigger<NoArg>()), turn_off_trigger_(new Trigger<NoArg>()) {
: Switch(name), Component(), turn_on_trigger_(new Trigger<NoArg>()), turn_off_trigger_(new Trigger<NoArg>()) {

}
void TemplateSwitch::loop() {
Expand Down
2 changes: 1 addition & 1 deletion src/esphomelib/switch_/template_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d50e292

Please sign in to comment.