Component Development: propagate the hardware state back to HA?

I’m developing a custom component for a gesture-based light switch / dimmer on VL6180X ToF range sensor.

The hard part is already done: interfacing with the hardware, state machine and the logic to recognize gestures, etc etc. As a result I can toggle a standard PWM output (ESP32 ledc platform) with a gesture, dim it, etc.

However, the issue is that these changes are not reflected in HA “Light” entity state: it’s not toggled, brightness is not changed.

What would be the proper way to propagate changes made to light::LightOutput class physical state back to the user? this->lightstate_->toggle(), this->lightstate_->publish_state(); appear to do nothing.

I’ve set up a small example of the code, taking empty_light example and having it to set the output to random values periodically here: vl6180x-gesture-switch/empty-light-example at main · k-korn/vl6180x-gesture-switch · GitHub

The actual code is here, on line 19. There should be something to propagate the changed state back to HA, but I have not found anything close in other Light/Dimmer components or in the docs.

You can probably use something like the shelly dimme as a template:

Thanks, I’ve actually already used it as an example of a device which is simultaneously a Light and a Sensor.

However, what I need is something Shelly dimmer lacks - hardware control of the output which is synced back to HA.

Shelly dimmer does not have any controls, so it is only processing commands from HA to change the state and sends these to the hardware. What I need is a way to tell HA “the brightness or output state has been changed in hardware, display that in the UI now”.

OK, I was able to find the answer in unexpected place - Lambda examples for light.turn_on() action:

Lambda code is:
id(light_1).turn_on().set_brightness(1.0).perform();

So instead of calling this->lightstate_->toggle() one should call this->lightstate_->toggle().perform();, or set other options from LightCall class and then perform().

2 Likes