Need some help getting ahead with a TCP proxy

I’m pretty novice at Python and integrations but managed to build a responsive TCP proxy on multiple ports using the example_sensor integration.

There is this device on my network that is sending data to a destignation webserver, I want to capture this data using DNS rewrite, interpret it, use the data in sensors and foreward it to the destignation webserver.

I need some help because of some threading issues it seems. Here is my log:

2023-02-17 23:59:35.496 WARNING (SyncWorker_3) [custom_components.example_sensor.sensor] Proxy Started...
2023-02-17 23:59:45.455 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform example_sensor is taking over 10 seconds.
2023-02-18 00:00:05.566 WARNING (SyncWorker_2) [custom_components.example_sensor.sensor] Sensor update...
2023-02-18 00:00:26.473 WARNING (MainThread) [homeassistant.bootstrap] Waiting on integrations to complete setup: sensor.example_sensor
2023-02-18 00:00:35.458 ERROR (MainThread) [homeassistant.components.sensor] Setup of platform example_sensor is taking longer than 60 seconds. Startup will proceed without waiting any longer.
2023-02-18 00:00:35.605 WARNING (SyncWorker_0) [custom_components.example_sensor.sensor] Sensor update...
2023-02-18 00:01:05.583 WARNING (SyncWorker_4) [custom_components.example_sensor.sensor] Sensor update...
2023-02-18 00:01:15.270 WARNING (Thread-2 (serve_forever)) [custom_components.example_sensor.sensor] Received: "Test on port 5000"
2023-02-18 00:01:15.280 WARNING (Thread-2 (serve_forever)) [custom_components.example_sensor.sensor] Sending: Thank you!
2023-02-18 00:01:28.373 WARNING (Thread-2 (serve_forever)) [custom_components.example_sensor.sensor] Received: "Test on port 5001"
2023-02-18 00:01:28.384 WARNING (Thread-2 (serve_forever)) [custom_components.example_sensor.sensor] Sending: Thank you!

Issue with the listener is that it’s getting in some sort of loop while starting. I’m used to events handling incoming data without blocking HA in this case. How is that done in HA and what am I doing wrong? Here is the sensor.py:

"""Platform for sensor integration."""
from __future__ import annotations

from homeassistant.components.sensor import (
    SensorDeviceClass,
    SensorEntity,
    SensorStateClass,
)
from homeassistant.const import TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

import logging
LOGGER = logging.getLogger(__name__)

host = "172.16.0.19"

import socketserver
from socketserver import BaseRequestHandler
from datetime import datetime, timedelta
import threading


host = '172.16.0.19' #Your host IP-address here

class HTTPSERVER(BaseRequestHandler):
    def handle(self):
        rec = self.request.recv(1024)
        if rec:
            LOGGER.warning(f"Received: {rec}")
            send_str = "Thank you!"
            LOGGER.warning(f"Sending: {send_str}")
            self.request.send(send_str.encode('utf-8'))
            
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None
) -> None:
    """Set up the sensor platform."""
    add_entities([ExampleSensor()])

    listener_1 = socketserver.TCPServer((host, 5000), HTTPSERVER)
    thread_1 = threading.Thread(target=listener_1.serve_forever)
    listener_2 = socketserver.TCPServer((host, 5001), HTTPSERVER)
    thread_2 = threading.Thread(target=listener_2.serve_forever)
    LOGGER.warning("Proxy Started...")
    for threads in thread_1, thread_2:
        threads.start()

    for threads in thread_1, thread_2:
        threads.join()

class ExampleSensor(SensorEntity):
    """Representation of a Sensor."""
    _attr_name = "Example Temperature"
    _attr_native_unit_of_measurement = TEMP_CELSIUS
    _attr_device_class = SensorDeviceClass.TEMPERATURE
    _attr_state_class = SensorStateClass.MEASUREMENT

    def update(self) -> None:
        # Fetch new state data for the sensor, this is the only method that should fetch new data for Home Assistant.
        LOGGER.warning("Sensor update...")
        self._attr_native_value = 23

Help is greatly appreciated, if this is resolved I can continue with it, I consider this the most important basis on which I can build further.

Ow, I mean to say that joining the threads was not a good idea because the one thread waits for the other to end and that is never. It works now…