Component to listen on a TCP socket for the Easer Android App

hi all, I’ve written a custom component to listen on a TCP socket. This code was tested with hass version 0.92.2 and written for the Android App Easer https://me.ryey.icu/Easer/en/ (available in https://f-droid.org/) and its TCP Trip Event.

It works except when I restart Hass in the web UI. I think that the problem is that none of the on_remove methods are called when I restart Hass and so the subproccess started by the component are not killed. They must have the UI socket open. Do you have an example of an async custom component that uses the on_remove methods?

The full error message that I see when I restart hass with an enabled tcp_listener component is:
ERROR (MainThread) [homeassistant.components.http] Failed to create HTTP server at port 8123: [Errno 98] error while attempting to bind on address ('0.0.0.0', 8123): address already in use

Any comments on my code would also be most welcome:

https://github.com/stuart12/stuart-system/blob/master/home-automation/home-assistant/kooka/custom_components/tcp_listener/switch.py

I replaced fork & listen with asyncio-stream and now restart works correctly. I’ve never used async before so please let me know if there are any problems in the code!

@Stuart this is exactly what I was looking for. I do however encounter something odd …

Nov 15 22:22:52 hassbian hass[15442]: 2020-11-15 22:22:52 DEBUG (MainThread) [custom_components.tcp_listener.switch] starting with config OrderedDict([('incoming_call_doorstation', OrderedDict([('port', 20568), ('name', 'Doorstation incomming'), ('state', False)]))])
Nov 15 22:22:52 hassbian hass[15442]: 2020-11-15 22:22:52 DEBUG (MainThread) [custom_components.tcp_listener.switch] new TCPListener with config <Entity Doorstation incomming: off>

I’ve been poking around in the code to see why async_listen section is never fired but stranded … any idea why the socket never opens?