I think trying to make it "reconnect faster" is a poor solution to your problem, the fact that you made this script is generally a bad idea. You should resolve the cause of the disconnects.
I think from the earlier posts you have heard stories of various issues that cause WiFi drop outs, and in fact if you check the Raspberry Pi forums you will find many more, but to recap:
- HDMI causes interference in the 2.4GHz band (known to affect WiFi channel 1, maybe BT?), firmware fix AFAIK. Never an issue for me, but I wasn't an early adopter.
- USB3 causes interference with on-board Wi-Fi, no idea if this is still a thing or not but the RPi4 has 3 firmwares to my knowledge (Bootloader, USB, and Wifi/BT).
- BT itself has interference issues with WiFi on the same chip, think there was firmware fixes & parameter changes to address it.
- People have reported USB hubs or other devices being too close as the cause interference with WiFi.
- Perhaps your tuner HAT is causing the issue, no idea, don't have one.
That tells me you want to get on the latest software available for the platform with all of the firmware patches possible. You could have kernel related issues, or SDIO bus saturation which may manifest as problems as well [hard to know unless you can quantify your wireless performance at the time of the drop outs -- i.e. how much data is being sent/received]. So I think either running LE 10 or testing out a recent update of Raspberry Pi OS may help. It might also be worthwhile to poke in on the Raspberry Pi Forums and see if the engineers respond with any insight on your issue, a few of the Raspberry Pi engineers occasionally check in on issues and will reply.
As I mentioned earlier, I am running a similar setup, RPi4, Tvheadend client, Kodi 19.1 (LE 10b5), and have zero issues. Even before I bought a $15 USB dongle, it managed fine with a larger cache. No wireless drop outs, ever.
As a side note, your incorrect about the buffermode. 4 is the default, which means remote only, you want 1:
#define CACHE_BUFFER_MODE_INTERNET 0
#define CACHE_BUFFER_MODE_ALL 1
#define CACHE_BUFFER_MODE_TRUE_INTERNET 2
#define CACHE_BUFFER_MODE_NONE 3
#define CACHE_BUFFER_MODE_REMOTE 4
I don't think remote includes "sources", so this is something you might want to check is working right. There is a visual cue on the playback progress bar of the caching.
Anyways, good luck.