I’m trying to make a custom component (my first one also) that will read a device state through UART and parse the data to create different binary_sensor
and sensor
to Home Assistant.
For the moment, I’ve been able to create the custom component, read from UART, parse it and create a binary sensor.
The problem is when I want to publish another sensor type. Logger shows the new value published but there’s nothing updated to Home Assistant.
Any idea to get this working ?
Here’s what I have in the ESPHome yaml file for this:
uart:
- id: uart_bus
tx_pin: TX
rx_pin: RX
baud_rate: 9600
binary_sensor:
- platform: custom
lambda: |-
auto binary_sensor_ac = new lecture_ac_uart(id(uart_bus));
App.register_component(binary_sensor_ac);
return {binary_sensor_ac->led1_sensor};
binary_sensors:
name: "Mode Auto"
sensor:
- platform: custom
lambda: |-
auto sensor_ac = new lecture_ac_uart(id(uart_bus));
App.register_component(sensor_ac);
return {sensor_ac->temperature_sensor};
sensors:
name: "Temperature"
And here’s the relevant part of lecture_ac_uart.h
class lecture_ac_uart : public PollingComponent, public UARTDevice {
public:
lecture_ac_uart(UARTComponent *parent) : PollingComponent(500), UARTDevice(parent) {}
Sensor *temperature_sensor = new Sensor();
BinarySensor *led1_sensor = new BinarySensor();
/*
Data processing part in loop()
*/
void update() override {
if(led1 != led1_last) {
led1_sensor->publish_state(led1);
led1_last = led1;
}
if(temperature != temperature_last) {
temperature_sensor->publish_state(temperature);
temperature_last = temperature;
}
}
};