Defining a custom binary sensor which is passed an object pointer to an existing custom component

I want to pass in a previously defined custom component as an argument to a constructor for a custom binary sensor.

The concept is to pass in a pointer to the custom component so the custom binary sensor can access one of its methods.

When I attempt to do so, I get a compile error in the lambda for the custom binary sensor. Which seems to indicate that the incorrect object type is being passed in. How do I access the previously defined object pointer for the custom component at the time the binary sensor is defined?

These are the lambdas for the custom component and custom binary sensor:


# Clock display component
custom_component:
- lambda: |-
    XY_Clock *xy_clock_display = new XY_Clock(id(sntp_time), id(ds1307_time), id(bus_a));
    return {xy_clock_display};
  components:
  - id: xy_clock_display


binary_sensor:
  - platform: custom
    lambda: |-
      auto hwclock_comm_fail_binary_sensor = new HWClockCommFailBinarySensor(id(xy_clock_display));
      App.register_component(hwclock_comm_fail_binary_sensor);
      return {hwclock_comm_fail_binary_sensor};

    binary_sensors:
      name: "HW Clock Comm Fail"


This is the compile error I’m getting:

xy_clock.yaml:77:78: error: invalid conversion from 'esphome::Component*' to 'XY_Clock*' [-fpermissive]
In file included from src/main.cpp:34:
src/xy_clock.h:271:41: note:   initializing argument 1 of 'HWClockCommFailBinarySensor::HWClockCommFailBinarySensor(XY_Clock*)'
  271 |   HWClockCommFailBinarySensor(XY_Clock *xyc) : PollingComponent(15000) {
      |                               ~~~~~~~~~~^~~
*** [.pioenvs/xy-clock/src/main.cpp.o] Error 1

This is the code generated in main.cpp for the custom components:


  // custom_component:
  //   lambda: !lambda |-
  //     XY_Clock *xy_clock_display = new XY_Clock(id(sntp_time), id(ds1307_time), id(bus_a));
  //     return {xy_clock_display};
  //   components:
  //   - id: xy_clock_display
  //   id: custom_component_customcomponentconstructor
  custom_component::CustomComponentConstructor custom_component_customcomponentconstructor = custom_component::CustomComponentConstructor([=]() -> std::vector<Component *> {
      #line 68 "xy_clock.yaml"
      XY_Clock *xy_clock_display = new XY_Clock(sntp_time, ds1307_time, bus_a);
      return {xy_clock_display};
  });
  xy_clock_display = custom_component_customcomponentconstructor.get_component(0);
  xy_clock_display->set_component_source("custom_component");
  App.register_component(xy_clock_display);
  // binary_sensor.custom:
  //   platform: custom
  //   lambda: !lambda |-
  //     auto hwclock_comm_fail_binary_sensor = new HWClockCommFailBinarySensor(id(xy_clock_display));
  //     App.register_component(hwclock_comm_fail_binary_sensor);
  //     return {hwclock_comm_fail_binary_sensor};
  //   binary_sensors:
  //   - name: HW Clock Comm Fail
  //     disabled_by_default: false
  //     id: binary_sensor_binarysensor
  //   id: custom_custombinarysensorconstructor
  custom::CustomBinarySensorConstructor custom_custombinarysensorconstructor = custom::CustomBinarySensorConstructor([=]() -> std::vector<binary_sensor::BinarySensor *> {
      #line 77 "xy_clock.yaml"
      auto hwclock_comm_fail_binary_sensor = new HWClockCommFailBinarySensor(xy_clock_display);
      App.register_component(hwclock_comm_fail_binary_sensor);
      return {hwclock_comm_fail_binary_sensor};
  });
  binary_sensor_binarysensor = custom_custombinarysensorconstructor.get_binary_sensor(0);
  App.register_binary_sensor(binary_sensor_binarysensor);
  binary_sensor_binarysensor->set_name("HW Clock Comm Fail");
  binary_sensor_binarysensor->set_disabled_by_default(false);
  // network:
  //   {}
  // socket:
  //   implementation: lwip_tcp
  // =========== AUTO GENERATED CODE END ============
  App.setup();

Does casting the id() function to XY_Clock* get it to compile?

binary_sensor:
  - platform: custom
    lambda: |-
      auto hwclock_comm_fail_binary_sensor = new HWClockCommFailBinarySensor( (XY_Clock*) id(xy_clock_display));
      App.register_component(hwclock_comm_fail_binary_sensor);
      return {hwclock_comm_fail_binary_sensor};

Thanks! It compiles now. Here’s what I did:

auto hwclock_comm_fail_binary_sensor = new HWClockCommFailBinarySensor((XY_Clock *) id(xy_clock_display));

I will test and see if it works.

It tests good.

1 Like