Converting ESP Home Custom Component to External Component

Hi,
After recent updates to esphome in home assistant my YAML file no longer works because they have deprecated custom components under this ESPHome 2025.2.0 - 19th February 2025 — ESPHome
To be replaced with External Components under this
External Components — ESPHome
I currently have a yaml file which references another file that reads the output of xantrex solar inverter, however no longer works due to the deprecation.

I have no idea on how to convert to an external component and have tried ChatGPT with no luck. Any help would be greatly appreciated

The code for my files are as follows, which was working prior to the update

esphome-web-fc1ed5.yaml

esphome:
  name: xantrex
  includes:
    - xantrex.h

esp8266:
  board: esp01_1m

# Enable logging
logger:

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

ota:
  platform: esphome


wifi:
  networks:
    - ssid: ssid1
      password: password
      priority: 1
    - ssid: ssid2
      password: password
      priority: 2

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

captive_portal:


uart:
  id: uart_bus
  baud_rate: 9600
  tx_pin: GPIO2
  rx_pin: GPIO0



sensor:
  - platform: custom 
    lambda: |-
      auto my_xantrex = new Xantrex(id(uart_bus));
      App.register_component(my_xantrex);
      return {my_xantrex->kwhlife_sensor, my_xantrex->kwhtoday_sensor,
        my_xantrex->pin_sensor, my_xantrex->pout_sensor,
        my_xantrex->vin_sensor, my_xantrex->vout_sensor,
        my_xantrex->iin_sensor, my_xantrex->iout_sensor,
        my_xantrex->freq_sensor, my_xantrex->time_sensor, my_xantrex->temp_sensor         };
      
    sensors:
    - name: "PV kwh life"
      unit_of_measurement: "kWh"
      device_class: "energy"
      state_class: "total_increasing"
      accuracy_decimals: 0
    - name: "PV kwh today"
      unit_of_measurement: "kWh"
      device_class: "energy"
      state_class: "total_increasing"
      accuracy_decimals: 3
    - name: "PV power in"
      unit_of_measurement: "W"
      device_class: "power"
      state_class: "measurement"
      accuracy_decimals: 0
    - name: "PV power out"
      unit_of_measurement: "W"
      device_class: "power"
      state_class: "measurement"
      accuracy_decimals: 0
    - name: "PV volts in"
      unit_of_measurement: "V"
      device_class: "voltage"
      state_class: "measurement"
      accuracy_decimals: 1
    - name: "PV volts out"
      unit_of_measurement: "V"
      device_class: "voltage"
      state_class: "measurement"
      accuracy_decimals: 1
    - name: "PV current in"
      unit_of_measurement: "A"
      device_class: "current"
      state_class: "measurement"
      accuracy_decimals: 2
    - name: "PV current out"
      unit_of_measurement: "A"
      device_class: "current"
      state_class: "measurement"
      accuracy_decimals: 2
    - name: "PV frequency"
      unit_of_measurement: "Hz"
      state_class: "measurement"
      accuracy_decimals: 2
    - name: "PV time"
      unit_of_measurement: "seconds"
      state_class: "measurement"
      accuracy_decimals: 0
    - name: "PV temp"
      unit_of_measurement: "°C"
      device_class: "temperature"
      state_class: "measurement"
      accuracy_decimals: 1

xantrex.h

class Xantrex : public Component, public UARTDevice {

public:
  Sensor *kwhlife_sensor = new Sensor();
  Sensor *kwhtoday_sensor = new Sensor();
  Sensor *pin_sensor = new Sensor();
  Sensor *pout_sensor = new Sensor();
  Sensor *vin_sensor = new Sensor();
  Sensor *vout_sensor = new Sensor();
  Sensor *iin_sensor = new Sensor();
  Sensor *iout_sensor = new Sensor();
  Sensor *freq_sensor = new Sensor();
  Sensor *time_sensor = new Sensor();
  Sensor *temp_sensor = new Sensor();

  Xantrex(UARTComponent *parent) : Component(), UARTDevice(parent) {}

  bool command_issued = false;
  unsigned long cycle_start_time, command_issued_time;
  const int RESPONSE_WAIT = 500; // Wait for reply to each rs232 command
  const int CYCLE_WAIT = 10*1000;   // Overall poll cycle for Xantrex sensor values
                                  // Should be at least as long as RESPONSE_WAIT * number of sensors
  const char *queries[11] = { 
    "KWHLIFE?\r", "KWHTODAY?\r", "PIN?\r", "POUT?\r",
    "VIN?\r", "VOUT?\r", "IIN?\r", "IOUT?\r",
    "FREQ?\r", "TIME?\r", "MEASTEMP?\r" };
  int queryNum = 0;

  void setup() override {
    // Start the timers
    cycle_start_time = millis()-CYCLE_WAIT;
    command_issued_time = cycle_start_time;
    command_issued = false;
    // Probably not necessary
    Serial.setTimeout(30);
  }

  int readline(char *buffer, int len) {
    static int pos = 0;
    int rpos;
    int readch;

    while (available() >0) {
      readch = read();
      switch (readch) {
        case '\n': // Ignore new-lines
          break;
        case '\r': // Return on CR
          rpos = pos;
          pos = 0;  // Reset position index ready for next time
          return rpos;
        default:
          if (pos < len-1) {
            buffer[pos++] = readch;
            buffer[pos] = 0;
          }
      }
    }
    // No end of line has been found, so return -1.
    return -1;
  }

  void stripfarenheit(char *buffer, int len) {
    static int pos = 0;

    // Temp response format is: C:0.0 F:32.0
    for (pos = 0; buffer[pos] != '\0' && pos < len-1; pos++) {
      switch (buffer[pos]) {
        case 'C': // Replace with space
        case ':': // Replace with space
          buffer[pos] = ' ';
          break;
        case ' ': // Terminate at space
          buffer[pos] = 0;
          pos = len-1;  // Force end of loop
          break;
        default:
          break;
      }
    }
  }


  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];

    if (command_issued) {
      // wait after command before checking for a response
      if (millis() > command_issued_time + RESPONSE_WAIT) {
        ESP_LOGD("Xantrex", "RESPONSE_WAIT elapsed, available (%d)", available());
        if (readline(buffer, max_line_length)) {
          switch(queryNum) {
            case 0:
              kwhlife_sensor->publish_state(atof(buffer));
              break;
            case 1:
              kwhtoday_sensor->publish_state(atof(buffer));
              break;
            case 2:
              pin_sensor->publish_state(atof(buffer));
              break;
            case 3:
              pout_sensor->publish_state(atof(buffer));
              break;
            case 4:
              vin_sensor->publish_state(atof(buffer));
              break;
            case 5:
              vout_sensor->publish_state(atof(buffer));
              break;
            case 6:
              iin_sensor->publish_state(atof(buffer));
              break;
            case 7:
              iout_sensor->publish_state(atof(buffer));
              break;
            case 8:
              freq_sensor->publish_state(atof(buffer));
              break;
            case 9:
              time_sensor->publish_state(atof(buffer));
              break;
            case 10:
              // Temp response format is: C:0.0 F:32.0
              // Extract the celsius part
              stripfarenheit(buffer, max_line_length);
              temp_sensor->publish_state(atof(buffer));
              break;
            default:
              break;
          }
        }
        queryNum++;
        if (queryNum > 10) {
          queryNum=0;
        }
        buffer[0] = 0; // Clear the buffer
        command_issued = false;
      }
    }

    // check if polling wait time has elapsed for 1st command or 
    // subsequent command is due to be issued
    if (millis() > cycle_start_time + CYCLE_WAIT || (queryNum > 0 && !command_issued)) {
      ESP_LOGD("Xantrex", "CYCLE_WAIT elapsed");
      // Issue next command
      write_str(queries[queryNum]);
      flush();

      command_issued = true;

      // Reset poll timer and start response timer
      command_issued_time = millis();

      if (queryNum == 0) {
        // Increment the cycle timer by CYCLE_WAIT
        cycle_start_time = cycle_start_time + CYCLE_WAIT;
      }
    }

  }
};

You can try this
GitHub - robertklep/esphome-custom-component: Brings back support for custom ESPHome components

2 Likes

Thank you very much. Worked a charm :grinning:

Thanks! Works for me as well :slight_smile: