@aequitas Any chance you could implement this? Sadly it’s beyond my skills. 
I have been using your code for years and have started to see the same issue with RFlink entities going unavailable every 5 minutes, using RFLink over TCP (My Home-Assistant-RFLink-Gateway-ESP8266 setup).
I was scratching my head until I saw this post from 2019 realised that since I took away my 433Mhz home energy meter I no longer have a “heartbeat” keeping the connection alive.
Update: @aequitas, I see you seem to have added this to your python library? GitHub - aequitas/python-rflink
I have coded a quick and dirty examples in HASS IO to perform this keep-alive action, both of these work for my install (Pi 4, Home Assistant OS).
# Example configuration.yaml entry
sensor:
- platform: command_line
name: RFLink Keep Alive Command
command: 'echo "10;PING;" | nc 192.168.2.22 23'
scan_interval: 60 #seconds
or a shell command which could be called from an automation:
# Example configuration.yaml entry
shell_command:
rflink_ping: 'echo "10;PING;" | nc 192.168.2.22 23'
Response back from the RFLink gateway, viewed from the tcp-to-serial bridge, showing the “pong” responses.
I did also try the tcp sensor, which should work for everyone regardless to their build (location of netcat) but for me at least this locks the RFLink integration out from Home Assistant 
# Example configuration.yaml entry
sensor:
- platform: tcp
name: rflink_ping_tcp_binary
host: 192.168.2.22
port: 23
payload: '10;PING'
I have always has the issue that if I power cycle the RFLink connected Wifi serial bridge, then the RFLink integration in Home Assistant stops working, until I then restart Home Assistant.
Also the tcp sensor seems to make no way to detecting if no response is resulted / timed-out.
Seems both the RFLink integration and my ‘hack’ sensor are fighting it out!
2020-10-04 22:29:50 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.2816748752]
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 137, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1315, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1350, in _execute_service
await handler.func(service_call)
File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
await self.hass.helpers.service.entity_service_call(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 459, in entity_service_call
future.result() # pop exception if have
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 655, in async_request_call
await coro
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 490, in _handle_entity_call
await result
File "/usr/src/homeassistant/homeassistant/components/light/__init__.py", line 249, in async_handle_light_on_service
await light.async_turn_on(**params)
File "/usr/src/homeassistant/homeassistant/components/rflink/__init__.py", line 561, in async_turn_on
await self._async_handle_command("turn_on")
File "/usr/src/homeassistant/homeassistant/components/rflink/__init__.py", line 498, in _async_handle_command
await self._async_send_command(cmd, self._signal_repetitions)
File "/usr/src/homeassistant/homeassistant/components/rflink/__init__.py", line 524, in _async_send_command
await self._protocol.send_command_ack(self._device_id, cmd)
File "/usr/local/lib/python3.8/site-packages/rflink/protocol.py", line 227, in send_command_ack
yield from asyncio.wait_for(
File "/usr/local/lib/python3.8/asyncio/tasks.py", line 490, in wait_for
raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError
vs
2020-10-04 22:30:22 ERROR (SyncWorker_63) [homeassistant.components.tcp.sensor] Unable to connect to 192.168.2.22 on port 23: timed out
Note, I use the “TCP mode” string, I have seen others use the “Port” string but rather than set it to USB they set it to a TCP port with extra parameters for reconnect.
socat pty,link=/config/serialport-rflink,echo=0 tcp:x.x.x.x:xxxx,forever,reuseaddr,keepalive,keepidle=10,keepintvl=10,keepcnt=2
Issue raised: RFLink TCP mode does not implement ping to keep connection alive - connection times out #41251