General method to target individual sleepy devices to inhibit sleep mode

Question for the wizards. I use sensors that I put into deep sleep most of the time. I’ve implemented a helper that sets a binary sensor to instruct the device to disable deep sleep so I can do ota updates. The problem is if I have 20 sensors Idon’t want to disable sleep for all of them when I set the disable sleep flag, and I’d like to avoid creating 20 individual helpers. Is there some way to create a generic method to target a device to inhibit sleep mode?

Maybe you could make a input select or input text in Home Assistant, and import that into your ESPHome devices as a Home Assistant Text Sensor, and check some conditions with that?

Maybe there’s something you can do with the device name?

I created a text helper in HA and each device has that as a text sensor and checks it for specific text, the ESPHome device’s friendly name, to see if it should disable sleep in a repeating polling script that launched on_boot . It works very well but the need to poll is “hacky”.

I’d rather have a binary sensor in each device that can be set from the HA device page to control sleep. I’m able to do that to enable sleep, but not disable sleep. There seems to be an issue with a sleepy device not being able to fetch that sensor value when it wakes up. I’ve tried all kinds of things in on_boot but ended up soft-bricking the device and having to perform a USB install with the device in boot
loader mode. Clearly, something is going on that I don’t understand.

Because deep sleep is so vital to using battery power devices efficiently, I think sleep control should be built into ESPHome and be part of each device HA device page

I was also unable to move the “Enable sleep” toggle switch from a helper entity to the device page.

I really thought it would (and should) work to add a “switch” in the YAML that shows up as a toggle on the HA web interface. I tried that switch with every combination of “RESTORE” options, but it never worked as intended. I also tried custom lambda code to save the setting to the device and restore on boot, but that didn’t work either.

I reverted to the helper entity (input_boolean) and that’s worked reliably.

I was also unable to get a switch on the device page to control the helper entity: HA wouldn’t let my device write to the entity state.

I wonder whether the new Home assistant switch can be better leveraged for this purpose.

As opposed to the typical helper solution.

That’s what I tried and failed i.e. “switch” with platform “homeassistant”