c++arduinoesphome

EspHome custom component


i have a probleme with custom code in esphome.. There is the error :

src/screen.h:23:59: error: cannot convert 'MyCustomComponent::MyCustomComponent(esphome::template_::TemplateNumber*&, esphome::template_::TemplateNumber*&, esphome::template_::TemplateNumber*&, esphome::homeassistant::HomeassistantTextSensor*&)::<lambda(String)>' to 'std::function<void(std::__cxx11::basic_string<char>)>'
   23 |                                           { str = str_n; });
      |                                                           ^

and the code :

  MyCustomComponent(esphome::template_::TemplateNumber *&_led_r,esphome::template_::TemplateNumber *&_led_g,esphome::template_::TemplateNumber *&_led_b,esphome::homeassistant::HomeassistantTextSensor *&_str)
  {
    _led_r->add_on_state_callback([this](float led_r_n)
                                      { led_r = led_r_n; });
    _led_g->add_on_state_callback([this](float led_g_n)
                                      { led_g = led_g_n; });                                      
    _led_b->add_on_state_callback([this](float led_b_n)
                                      { led_b = led_b_n; });
    _str->add_on_state_callback([this](String str_n)
                                          { str = str_n; });                                      
  }

I'm really lost..

thank you in advance

EDIT : here is my code : here is my code, in case I specify that I run this on a HomeAssistant instance with EspHome in version 2022.8.3

#include "esphome.h"
#include <Wire.h>
#include "rgb_lcd.h"
 
class MyCustomComponent : public Component, public CustomAPIDevice {
 
 public:
  rgb_lcd lcd;
  bool enable = false;
  int led_r = 0;
  int led_g = 0;
  int led_b = 0;
  String str = "ff";
  MyCustomComponent(esphome::template_::TemplateNumber *&_led_r,esphome::template_::TemplateNumber *&_led_g,esphome::template_::TemplateNumber *&_led_b,esphome::homeassistant::HomeassistantTextSensor *&_str)
  {
    _led_r->add_on_state_callback([this](float led_r_n)
                                      { led_r = led_r_n; });
    _led_g->add_on_state_callback([this](float led_g_n)
                                      { led_g = led_g_n; });                                      
    _led_b->add_on_state_callback([this](float led_b_n)
                                      { led_b = led_b_n; });
    _str->add_on_state_callback([this](String str_n)
                                          { str = str_n; });                                      


  }
  void setup() override {
    lcd.begin(16, 2);
  }
  
    void loop() override
  {
       lcd.setRGB(led_r,led_g,led_b);
}

Solution

  • As shown here

    void esphome::text_sensor::TextSensor::add_on_state_callback(std::function< void(std::string)>callback)
    

    You should use std::string instead of String (Arduino string), those are incompatible. I'm not sure what you're going to do with String str = "ff"; but In case you still want to use Arduino String, you should be able to use like

    _str->add_on_state_callback([this](std::string str_n)
                                          { str = String(str_n); });