TCP Packet Interface
The tcp Packet Interface platform sends and receives packets over a TCP connection
between two devices. One device is the client, which connects to the other; the other is the server, which
accepts one client at a time.
TCP delivers what is sent in order and sends again what is lost, and a connection joins exactly two devices. So a TCP interface does the job of the reliability, addressing and session layers itself, and those layers cannot be added to it. Components such as entity transport are told when the connection comes up or goes down, as they would be by a session layer. The encryption, fragmentation and compression layers can be added, but not the encryption layer’s rolling code, which needs the keep-alives of a session layer.
Each packet is sent with its length in front. When nothing else has been sent for keep_alive, an empty packet is
sent, and if nothing at all has been received for timeout, the connection is closed and made again.
This finds a device that has lost power or its network, which TCP alone can take many minutes to notice.
When the connection stalls, because the other device has stopped reading or the network has stopped moving data,
packets are held in a 1 KB send buffer, and once that is full they are refused, and the component sending each one is
told. With a fragmentation layer, a packet of several fragments can be refused part way through: the other device
then drops the fragments it has, and the sender is told the packet was not sent. entity_transport tries again a
second later.
Example Configuration
Section titled “Example Configuration”On the device that connects:
packet_interface: - platform: tcp id: tcp_link host: 192.168.1.50 port: 6070 encryption: !secret tcp_link_keyOn the device it connects to:
packet_interface: - platform: tcp id: tcp_link role: server port: 6070 encryption: !secret tcp_link_keyConfiguration Variables
Section titled “Configuration Variables”- role (Optional, string):
clientto connect to another device, orserverto accept a connection from one. Defaults toclient. - host (Required for a client, string): The IPv4 address or host name of the server.
- port (Required, int): The TCP port the server listens on.
- allowed_ips (Optional, list, server only): The networks, such as
192.168.1.0/24, from which a client may connect. By default any client may connect. - reconnect_interval (Optional, Time): How long to wait before connecting,
or listening, again after a failure. Defaults to
5s. - keep_alive (Optional, Time): How often to send an empty packet when
nothing else is sent. From
1sto1h. Defaults to10s. - timeout (Optional, Time): How long the link waits without receiving
anything before it closes the connection. It must be longer than this device’s
keep_alive, which is checked, and longer than the other device’skeep_alive, which sets how often packets arrive on an idle link; set it when the two devices have differentkeep_alivevalues. At most3h. Defaults to three timeskeep_alive. - connected (Optional): A binary sensor that is ON while the connection is up, and OFF from boot until it first comes up. All options from Binary Sensor.
- max_packet_size (Optional, int): The largest packet, in bytes, that can be sent or received. A larger packet
received closes the connection, so set the same value on both devices. Range is 16 to 1022. Defaults to
1022. Use a fragmentation layer for larger packets. - id (Optional, ID): Manually specify the ID used for code generation.
A server accepts only one client at a time. If the client restarts, the server closes the old connection when it finds the client gone, and then accepts the new one.