Yeelight lamp and RF switch

Hello friends! There is a task to connect the Yeelight lamp an RF switch. Now I use ESPHome firmware on the relay with RF-Resiver and the relay turns the lamp ON and OFF as usual, even without the HA server. It needs to work like this: if the relay is online, then when the RF switch is pressed, the lamp turns on and off with a command from HA, else it turns off through the relay. Please help me implement this on ESPHome and in HA. Thanks!