Custom ESPHome component doesn’t build CMake subdirectory (library not included in build)

Hi everyone,

I’m developing a custom ESPHome component that works correctly on its own, but now I need to include a C++ library inside the component.

My folder structure looks like this:

esphome/components/
└── my_component/
    ├── __init__.py
    ├── my_component.cpp
    ├── my_component.h
    └── library/
        ├── CMakeLists.txt
        ├── include/library/*.hpp
        └── src/*.cpp

The problem is that the library/ folder isn’t copied into ESPHome’s build directory, and its CMakeLists.txt doesn’t seem to be executed.
The component compiles fine if I put everything directly under my_component/, but I’d like to keep the library as a proper submodule with its own CMake structure.

So far I’ve tried:

  • Adding a CMakeLists.txt inside my_component/ with add_subdirectory(library)
  • Including target_include_directories() and target_sources() manually
  • Checking whether __init__.py needs to reference it somehow

…but it seems that ESPHome ignores custom CMakeLists.txt inside components.

Is there a supported way to make ESPHome compile sources from a nested library/ folder with its own CMake file, or do I have to flatten the library into the component’s root?

Any guidance or examples would be greatly appreciated.

Here is a working partly example
Hopefuly i have copied the releavant info for you

Directory structure
esphome/custom_components/ikea_ansluta

  • ikea_ansluta.h
  • ikea_ansluta.cpp
  • ect…

In the yaml

external_components:
  - source:
      type: local
      path: custom_components

in ikea_ansluta.h

#pragma once

#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/components/spi/spi.h"
#include "unordered_map"

namespace esphome {
namespace ikea_ansluta {
enum class Command : uint8_t {
  OFF = 0x01,
  ON_50 = 0x02,
  ON_100 = 0x03,
  PAIR = 0xFF,
};

class IkeaAnsluta : public Component,
                    public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
                        spi::DATA_RATE_4MHZ> {

AND further code.