Can you make a TCP client connection with ESPHome?

I’m looking to possible port an IR receiver running on a raspberry PI over to ESPhome. The IR receiver accepts the remote signals and then via a tcp connections send commands to mythtv frontend via a TCP network connection. I looked around and saw a UDP component, but I didn’t see a TCP component. Any change someone is aware of a way to establish a TCP socket connection and send data over the socket on ESPHome.

While mythtv doesn’t accept UDP, I could probably run a UDP to TCP program on the mythtv linux box. So as a fallback I could probably make UDP work. I saw examples showed setting up a ESPhome UDP server for receive and transmission. Is it possible to just send UDP packets out to a destination IP without setting up a receiving server?

Have you looked at the Telnet platform? It’s limited only to a couple of commands, but works with any open port.

I have not as I’m trying to take the action directly from the ESPHome device vs on the Home Assistant controller. Thanks for pointing that out.

Oops must have been half asleep still…

I utilized Claude, I worked through writing a custom component to provide a persistent TCP connection that auto reconnects and can be utilized via lambda to send out data. Now I just have to work out the remote control signal processing, which should be easier as there already a component for that. I’ll either put the custom component on github or post it here once I get everything going.

A GitHub repo would probably be the better pick. Your component can then be pulled in directly as an ESPHome external component, making it easy for others to use, it’s easier to maintain, and others can contribute or open issues.

You can have Claude generate a repo that you upload manually, or if you are using VS Code, Cursor, or similar, those editors can create a repo for you directly using an add-on/plugin (GitHub MCP integration).

Once published on GitHub, it can be referenced like this in your ESPHome config:

external_components:
  source: github://bkprath/esphome-tcp-connection
  components: [tcp_connection]
1 Like

Alright it’s done. The custom component creates a persistent TCP connection with the specified server. While I only cared about forwarding IR commands over a TCP socket the developed component is general and provides a way to process responses at the ESPHome yaml configuration level.

external_components:
  source: github://0n3man/esphome_persistent_tcp
  components: [esphome_persistent_tcp]
1 Like