I am trying to create my own external component and so I’m 'started small" (or so I thought!). I have cloned the standard ‘GPIO switch’ code (making suitable changes with the namespace and class name etc.).
I the YAML file I can create a yaml file:
switch:
- platform: gpio
pin:
number: GPIO21
inverted: true
id: test_led
name: "Test LED"
and all works well.
I have edit the yaml file to be:
external_components:
- source:
type: local
path: /config/My_Components
components: [Lucci_Fan]
Lucci_Fan:
id: test_led2
name: "Test LED2"
pin:
number: GPIO21
inverted: true
and it tries to compile everything until it comes to the error:
src/main.cpp: In function 'void setup()':
src/main.cpp:181:7: error: 'class esphome::Application' has no member named 'register_switch'; did you mean 'register_component'?
App.register_switch(test_led2);
^~~~~~~~~~~~~~~
register_component
*** [.pioenvs/tester/src/main.cpp.o] Error 1
I understand that this is a file that is auto generated from the following files:
“__init__.py”
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import switch
from esphome.const import (
CONF_ID,
CONF_PIN,
)
lucci_fan_ns = cg.esphome_ns.namespace("lucci_fan_")
Lucci_Fan = lucci_fan_ns.class_("Lucci_Fan", switch.Switch, cg.Component)
CONFIG_SCHEMA = (
switch.switch_schema(Lucci_Fan)
.extend(
{
cv.Required(CONF_PIN) : pins.gpio_output_pin_schema,
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await switch.new_switch(config)
await cg.register_component(var, config)
pin = await cg.gpio_pin_expression(config[CONF_PIN])
cg.add(var.set_pin(pin))
“lucci_fan.h”:
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "/esphome/esphome/components/switch/switch.h"
namespace esphome {
namespace lucci_fan_ {
class Lucci_Fan : public switch_::Switch, public Component {
public:
void setup() override;
void dump_config() override;
void set_pin( GPIOPin *pin_number);
protected:
virtual void write_state(bool state);
GPIOPin *led_pin_;
};
} // namespace lucci_fan_
} // namespace esphome
“lucci_fan.cpp”:
#include "lucci_fan.h"
#include "esphome/core/log.h"
namespace esphome {
namespace lucci_fan_ {
static const char *const TAG = "lucci_fan";
void Lucci_Fan::setup()
{
ESP_LOGCONFIG(TAG, "Lucci Fan Setup : '%s'", this->name_.c_str());
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
if( initial_state)
{ this->turn_on(); }
else
{ this->turn_off(); }
this->led_pin_->setup();
// Do this again for other IOs (???)
if( initial_state)
{ this->turn_on(); }
else
{ this->turn_off(); }
}
void Lucci_Fan::dump_config()
{
LOG_SWITCH(TAG, "Lucci Fan Output:", this);
LOG_PIN(" Pin: ", this->led_pin_);
}
void Lucci_Fan::write_state( bool state)
{
ESP_LOGI(TAG, "'write state %s", ONOFF(state));
this->led_pin_->digital_write( state);
this->publish_state(state);
}
void Lucci_Fan::set_pin( GPIOPin *pin_number)
{
this->led_pin_ = pin_number;
ESP_LOGI(TAG, "GPIO pin is %d", this->led_pin_);
}
} // namespace lucci_fan_
} // namespace esphome
I cannot for the life of me see why that error is generated for my code but not for the GPIO Switch files.
Please let me know what I’m doing wrong?
Susan