Hi all
Trying to create some custom components.
Succeeded in creating a binary sensor doing nothing :).
But now tried to duplicate the GPIO digital sensor.
Just copied the files, renamed the init.py to binary_sensor.py and renamed namespace and class
Gives me error: binary_sensor.binary_sensor_schema(LdoBinarySensor01) AttributeError: module ‘esphome.components.binary_sensor’ has no attribute ‘binary_sensor_schema’
Can someone explain me what I am doing wrong.
Also I see in the python file: async def to_code(config):
In other examples that async is not there, but here it seems necessary?
directory structure:
project (dir)
– project.yaml
– custom_components (dir)
---- binary_sensor.py
---- ldo_binarysensor_01.h
---- ldo_binarysensor_01.cpp
project yaml:
esp32:
board: nodemcu-32s
framework:
type: arduino
esphome:
name: project01
build_path: "./build"
logger:
binary_sensor:
- platform: gpio
name: Std sensor
pin:
number: 27
mode:
input: true
pulldown: true
- platform: ldo_binary_sensor_01
name: Ldo sensor
pin:
number: 26
mode:
input: true
pulldown: true
binary_sensor.py
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import binary_sensor
from esphome.const import CONF_PIN
ldo_binary_sensor_01_ns = cg.esphome_ns.namespace('ldo_binary_sensor_01')
LdoBinarySensor01 = ldo_binary_sensor_01_ns.class_("LdoBinarySensor01", binary_sensor.BinarySensor, cg.Component)
CONFIG_SCHEMA = (
binary_sensor.binary_sensor_schema(LdoBinarySensor01)
.extend({cv.Required(CONF_PIN): pins.gpio_input_pin_schema,})
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config)
pin = await cg.gpio_pin_expression(config[CONF_PIN])
cg.add(var.set_pin(pin))
ldo_binarysensor_01.h
// ***** ldo_binarysensor_01.h
#pragma once
// INCLUDES
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
// NAMESPACES
namespace esphome {
namespace ldo_binary_sensor_01 {
// DEFINE CLASS
class LdoBinarySensor01 : public binary_sensor::BinarySensor, public Component {
public:
void setup() override;
void loop() override;
void set_pin(GPIOPin *pin) { pin_ = pin; }
void dump_config() override;
float get_setup_priority() const override;
protected:
GPIOPin *pin_;
ldo_binarysensor_01.cpp
// ***** ldo_binarysensor_01.cpp
// INCLUDES
#include "ldo_binary_sensor_01.h"
#include "esphome/core/log.h"
// NAMESPACES
namespace esphome {
namespace ldo_binary_sensor_01 {
// GLOBALS
static const char *const TAG = "LdoBinarySensor01";
// CLASS
void LdoBinarySensor01::setup() {
this->pin_->setup();
this->publish_initial_state(this->pin_->digital_read());
}
void LdoBinarySensor01::loop() {
this->publish_state(this->pin_->digital_read());
}
void LdoBinarySensor01::dump_config() {
LOG_BINARY_SENSOR("", "LdoSensor01", this);
LOG_PIN(" Pin: ", this->pin_);
}
float LdoBinarySensor01::get_setup_priority() const {
return setup_priority::HARDWARE;
}