From d3c2861e4acf12e38338bf2d3d52561f23ed2673 Mon Sep 17 00:00:00 2001
From: "J. Nick Koston" <nick@koston.org>
Date: Mon, 27 Nov 2023 09:25:49 -0600
Subject: [PATCH] Set TCP_NODELAY to avoid needless buffering (#554)

---
 kasa/protocol.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kasa/protocol.py b/kasa/protocol.py
index c49ab223..6413ba5d 100755
--- a/kasa/protocol.py
+++ b/kasa/protocol.py
@@ -13,6 +13,7 @@ import asyncio
 import contextlib
 import errno
 import logging
+import socket
 import struct
 from abc import ABC, abstractmethod
 from pprint import pformat as pf
@@ -107,6 +108,12 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
         task = asyncio.open_connection(self.host, self.port)
         async with asyncio_timeout(timeout):
             self.reader, self.writer = await task
+            sock: socket.socket = self.writer.get_extra_info("socket")
+            # Ensure our packets get sent without delay as we do all
+            # our writes in a single go and we do not want any buffering
+            # which would needlessly delay the request or risk overloading
+            # the buffer on the device
+            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
 
     async def _execute_query(self, request: str) -> Dict:
         """Execute a query on the device and wait for the response."""