External Component for Text Output

Hey,

I am trying to implement an external component for text output and I don’t seem to get the crucial step for getting an entity.

Background: I have 20 characters of “splitflap” display that I can control by esp32 and I want to send text there. Displaying the text involves driving some Shift Registers via SPI and so on … so this has to be custom code.

What I’ve done so far is easily summed up:

test.yaml:

esphome:
  name: abc

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "**"

ota:
  - platform: esphome
    password: "**"

wifi:
  ssid: "MySSID"
  password: "**"

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

captive_portal:

external_components:    
  - source:
      type: local
      path: components
    components: ["fallblatt_abc"]
  
text:
  - platform: fallblatt_abc
    mode: "text"
    name: "Fallblatt ABC"

In the directory components/fallblatt_abc next to test.yaml you find:

text.py:

import esphome.config_validation as cv
from esphome.components import text

CONFIG_SCHEMA = text.TEXT_SCHEMA

__init__.py: empty

fallblatt_abc.h:

#include "esphome.h"
#include "esphome/core/component.h"
#include "esphome/components/text/text.h"

using namespace esphome;
using namespace text;

class FallblattABC : public Component, public Text {
public:
    void setup() {
        
    }

    void loop() {

    }
  
    void control(const std::string &value) {
        ESP_LOGI("ABC", "Text has been set: %s", value.c_str());
    }
};

I would expect, that there pops up some kind of entity I can send text to (via the text.set Action) and that the log line appears if I do so.

However, this is not the case. If I define for instance a GPIO sensor in test.yaml, it pops up. So what am I missing?

Thanks a lot!

For future reference. Solution: Do not forget to include the C++ code generation (def to_code) in your python definition.

Inclusion of the component in node yaml:

external_components:    
  - source:
      type: local
      path: components
    components: ["fallblatt_abc"]
  
text:
  - platform: fallblatt_abc
    id: "fallblatt_abc_1"
    name: "Fallblatt ABC"
    mode: "text"

text.py with code generation:

import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
from esphome.components import text

fallblatt_ns = cg.esphome_ns.namespace("fallblatt")

FallblattABC = fallblatt_ns.class_(
    "FallblattABC", cg.Component, text.Text
)

CONFIG_SCHEMA = text.TEXT_SCHEMA.extend({
    cv.Required(CONF_ID): cv.declare_id(FallblattABC),
}).extend(cv.COMPONENT_SCHEMA)

async def to_code(config):
    var = cg.new_Pvariable(config[CONF_ID])
    await cg.register_component(var, config)
    await text.register_text(var, config)

Now with the C++ class also being wrapped in namespace esphome { namespace fallblatt { ... }}.