There is a similar thread where I also linked to the code I’m using both in ESP home and Home Assistant.