Custom components

I am trying to install a DFRobot lux sensor, DFRobot_B_LUX_V30B as a custom component. The driver compiles and runs on a Arduino mini pro but when I compile under HA esphome it flags a error

In file included from src/main.cpp:30:
src/my_custom_sensor.h: In member function 'virtual void MyCustomSensor::setup()':
src/my_custom_sensor.h:11:5: error: invalid use of member function 'DFRobot_B_LUX_V30B MyCustomSensor::shedLux()' (did you forget the '()' ?)
   11 |     shedLux.begin();
      |     ^~~~~~~
      |            ()
src/my_custom_sensor.h: In member function 'virtual void MyCustomSensor::update()':
src/my_custom_sensor.h:15:23: error: invalid use of member function 'DFRobot_B_LUX_V30B MyCustomSensor::shedLux()' (did you forget the '()' ?)
   15 |     float outsideLux =shedLux.lightStrengthLux() ;
      |                       ^~~~~~~
      |                              ()
*** [.pioenvs/esphome-web-d02449/src/main.cpp.o] Error 1

The yaml configuration file is here

esphome:
  name: esphome-web-d02449
  friendly_name: Shed
  includes:
  - /config/esphome/lib/my_custom_sensor.h
   
esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: xxxxx
  
ota:

i2c:
  scl: GPIO14
  sda: GPIO12

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  # could not get this to work in the shed when I changed the password
  # got cold will try again 
  ap:
    ssid: "Esphome-Web-D02449"
    password: xxxxxx

captive_portal:
    
# Example configuration entry

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor};

  sensors:
    name: "Outside Lux"
    unit_of_measurement: Lux
    accuracy_decimals: 2

and the custom include file is

include "esphome.h"
#include "/config/esphome/lib/DFRobot_B_LUX_V30B.h"

class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  DFRobot_B_LUX_V30B    shedLux();  // GPIO16 is enable 14 scl, 12 sda

  MyCustomSensor() : PollingComponent(15000) { }

  void setup() override {
    shedLux.begin();
  }

  void update() override {
    float outsideLux =shedLux.lightStrengthLux() ;
    publish_state(outsideLux);
  }

Would appreciate some pointers of where I am going wrong. The target is an ESP8266

I guess you want to declare a member variable, here, so:

DFRobot_B_LUX_V30B    shedLux;  

I declare a specific instance of DFRobot_B_LUX_V30B

Blockquote

#include "esphome.h"
#include "/config/esphome/lib/DFRobot_B_LUX_V30B.h"

class MyCustomSensor : public PollingComponent, public Sensor {

  public:
  DFRobot_B_LUX_V30B  shedLux(16);  // GPIO16 is enable 14 scl, 12 DFRobot_B_LU>

  MyCustomSensor() : PollingComponent(15000) { }

Blockquote

Error :

src/my_custom_sensor0.h:7:31: error: expected identifier before numeric constant
    7 |   DFRobot_B_LUX_V30B  shedLux(16);  // GPIO16 is enable 14 scl, 12 DFRobot_B_LUX_V30B  shedLux(16);  // GPIO16 is enable 14 scl, 12
      |
                               ^~

Do you have a working solution in the meantime?

I have this yaml file:

esphome:
  name: dfrobottest
  friendly_name: dfrobottest
  includes:
  - /config/esphome/lib/DFRobot_B_LUX_V30B/my_custom_sensor.h

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  - platform: esphome
    password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dfrobottest Fallback Hotspot"
    password: "xxxxxxxxx"

captive_portal:

i2c:
  sda: D2
  scl: D1
  scan: True
  
sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor};

  sensors:
    name: "Outside Lux"
    unit_of_measurement: Lux
    accuracy_decimals: 2

This is my custom sensor:

#include "esphome.h"
#include "/config/esphome/lib/DFRobot_B_LUX_V30B/DFRobot_B_LUX_V30B.h"

class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  // constructor
  MyCustomSensor() : PollingComponent(5000) {} 
  
  DFRobot_B_LUX_V30B    shedLux{13};//The sensor chip is set to 13 pins, SCL and SDA adopt default configuration

  void setup() override {
    shedLux.begin();
  }

  void update() override {
    float outsideLux = shedLux.lightStrengthLux();
    publish_state(outsideLux);
  }
};

When I try to install it, I get this error:

INFO ESPHome 2024.8.3
INFO Reading configuration /config/esphome/dfrobottest.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing dfrobottest (board: d1_mini; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.2.2
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.6
|-- Wire @ 1.0
Linking .pioenvs/dfrobottest/firmware.elf
/data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pioenvs/dfrobottest/src/main.cpp.o:(.text._ZN14MyCustomSensor6updateEv[_ZN14MyCustomSensor6updateEv]+0x0): undefined reference to `_ZN18DFRobot_B_LUX_V30B16lightStrengthLuxEv'
/data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pioenvs/dfrobottest/src/main.cpp.o: in function `_ZN14MyCustomSensor6updateEv':
main.cpp:(.text._ZN14MyCustomSensor6updateEv[_ZN14MyCustomSensor6updateEv]+0x14): undefined reference to `_ZN18DFRobot_B_LUX_V30B16lightStrengthLuxEv'
/data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pioenvs/dfrobottest/src/main.cpp.o:(.text._ZN14MyCustomSensor5setupEv[_ZN14MyCustomSensor5setupEv]+0x0): undefined reference to `_ZN18DFRobot_B_LUX_V30B5beginEv'
/data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pioenvs/dfrobottest/src/main.cpp.o:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE_E9_M_invokeERKSt9_Any_data+0x8): undefined reference to `_ZN18DFRobot_B_LUX_V30BC1Ehhh'
/data/cache/platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld: .pioenvs/dfrobottest/src/main.cpp.o: in function `_ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE_E9_M_invokeERKSt9_Any_data':
main.cpp:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE_E9_M_invokeERKSt9_Any_data+0x45): undefined reference to `_ZN18DFRobot_B_LUX_V30BC1Ehhh'
collect2: error: ld returned 1 exit status
*** [.pioenvs/dfrobottest/firmware.elf] Error 1
========================= [FAILED] Took 13.39 seconds =========================

I have a working version:

esphome:
  name: dfrobottest
  friendly_name: dfrobottest
  includes:
    - /config/esphome/lib/DFRobot_B_LUX_V30B/my_custom_sensor.h
  libraries:
    - dfrobot/DFRobot_B_LUX_V30B@^1.0.1

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxx"

ota:
  - platform: esphome
    password: "xxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dfrobottest Fallback Hotspot"
    password: "xxxxxxxxxxx"

captive_portal:

i2c:
  sda: D2
  scl: D1
  scan: True
  
sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor};

  sensors:
    name: "Outside Lux"
    unit_of_measurement: Lux
    accuracy_decimals: 2

my_custom_sensor.h:

#include "esphome.h"
#include <DFRobot_B_LUX_V30B.h>

class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  // constructor
  MyCustomSensor() : PollingComponent(5000) {} 
  
  DFRobot_B_LUX_V30B    shedLux{13};//The sensor chip is set to 13 pins, SCL and SDA adopt default configuration

  void setup() override {
    shedLux.begin();
  }

  void update() override {
    float outsideLux = shedLux.lightStrengthLux();
    publish_state(outsideLux);
  }
};