How to create a custom component that allows for custom platforms?

I recently started with ESPHome and creating custom components. I managed to get some components working, but I would now like to order them in such a way that I have one custom component, and platforms connected to it. More specifically, I would like to create an airpurifier custom component, that for example defines a filtering strategy, and connected to it a fan platform, which implements the way of controlling the fan. Such that when other fans are connected in the future, this can easily be adapted by creating a new platform.

I am programming an ESP32 locally using the method described here:
Installing ESPHome Manually — ESPHome

The local folder structure is as follows:

├── src
│   ├── config.yml
│   ├── components
│   │   ├── airpurifier
│   │   │   ├── airpurifier.h
│   │   │   ├── airpurifier.cpp
│   │   │   ├── __init__.py
│   │   ├── myfan
│   │   │   ├── myfan.h
│   │   │   ├── myfan.cpp
│   │   │   ├── __init__.py

The init.py file of the airpurifier works on its own to implement an airpurifier component, so without a platform. The yaml file looks like:

airpurifier:
  sensor_iaq: iaq

However, when I add the following line to the init.py file:

IS_PLATFORM_COMPONENT = True

and change the yaml code to:

airpurifier:
  - platform: myfan
    sensor_iaq: iaq

I get the following error:

Platform not found: 'airpurifier.myfan'

Additionally, when I try to implement just the fan as a custom component, like:

myfan:
  sensor_iaq: iaq

I get the error:

esphome/components/airpurifier/airpurifier.h

I guess I have a problem with file structuring and linking, but I am clueless on how to solve this. Can somebody help me to resolve these issues?

Hi - welcome to the forums. It sounds like you know what you are doing (more than I do :slight_smile: ).

You may get a quicker answer to development question on the Discord server: