@@ -75,46 +75,76 @@ async def _set_value(self, device_id, uid, value):
7575
7676 async def _send_command (self , command : str ):
7777 try :
78- _LOGGER .debug ("Sending command %s" , command )
78+ _LOGGER .debug ("Preparing to send command: %s" , command )
7979 self ._received_response .clear ()
80- if self ._writer :
81- self ._writer .write (command .encode ("ascii" ))
82- await self ._writer .drain ()
83- timeout = 5.0
84- start_time = asyncio .get_event_loop ().time ()
85- while not self ._received_response .is_set ():
86- if asyncio .get_event_loop ().time () - start_time > timeout :
87- _LOGGER .error ("Timeout waiting for response" )
88- await self .stop ()
89- break
90- await asyncio .sleep (0.1 )
80+ if not self ._writer :
81+ _LOGGER .error ("No writer available. Cannot send command." )
82+ return
83+ _LOGGER .debug ("Writer state: %r" , self ._writer )
84+ encoded_command = command .encode ("ascii" )
85+ _LOGGER .debug ("Encoded command: %r (length: %d)" , encoded_command , len (encoded_command ))
86+ self ._writer .write (encoded_command )
87+ await self ._writer .drain ()
88+ _LOGGER .debug ("Command sent and drained. Waiting for response event." )
89+ timeout = 15.0
90+ start_time = asyncio .get_event_loop ().time ()
91+ while not self ._received_response .is_set ():
92+ elapsed = asyncio .get_event_loop ().time () - start_time
93+ if elapsed > timeout :
94+ _LOGGER .error ("Timeout waiting for response after %.2f seconds" , elapsed )
95+ await self .stop ()
96+ break
97+ _LOGGER .debug ("Still waiting for response event. Elapsed: %.2f seconds" , elapsed )
98+ await asyncio .sleep (0.1 )
99+ if self ._received_response .is_set ():
100+ _LOGGER .debug ("Response event set! Command succeeded." )
101+ else :
102+ _LOGGER .error ("Response event was never set. Command failed." )
91103 except OSError as exc :
92104 _LOGGER .error ("%s Exception. %s / %s" , type (exc ), exc .args , exc )
93105 except Exception as exc :
94- _LOGGER .error ("Unexpected error: %s" , exc )
106+ _LOGGER .error ("Unexpected error in _send_command : %s" , exc )
95107 await self .stop ()
96108
97109 async def _data_received (self ):
98110 try :
99111 while self ._reader :
100- raw_data = await self ._reader .readuntil (self ._data_delimiter )
112+ _LOGGER .debug ("Waiting to read data with delimiter %r" , self ._data_delimiter )
113+ try :
114+ raw_data = await self ._reader .readuntil (self ._data_delimiter )
115+ _LOGGER .debug ("Raw data received: %r (length: %d)" , raw_data , len (raw_data ) if raw_data else 0 )
116+ except IncompleteReadError as ire :
117+ _LOGGER .error (
118+ "IncompleteReadError: expected delimiter %r, got partial data: %r (length: %d)" ,
119+ self ._data_delimiter ,
120+ ire .partial ,
121+ len (ire .partial ) if ire .partial else 0
122+ )
123+ _LOGGER .error ("Reader state: %r" , self ._reader )
124+ _LOGGER .error ("Connection status: connected=%s, connecting=%s" , self ._connected , self ._connecting )
125+ raise
101126 if not raw_data :
127+ _LOGGER .debug ("No data received, breaking loop." )
102128 break
103- data = raw_data .decode ("ascii" )
129+ try :
130+ data = raw_data .decode ("ascii" )
131+ except Exception as decode_exc :
132+ _LOGGER .error ("Failed to decode raw_data: %r, error: %s" , raw_data , decode_exc )
133+ continue
104134 _LOGGER .debug ("Received: %s" , data )
105135
106136 await self ._parse_response (data )
107-
137+
108138 if not self ._received_response .is_set ():
109139 _LOGGER .debug ("Resolving set_value's await" )
110140 self ._received_response .set ()
111-
112141
113142 except IncompleteReadError :
114- _LOGGER .debug (
115- "pyIntesisHome lost connection to the %s server" , self ._device_type
143+ _LOGGER .error (
144+ "pyIntesisHome lost connection to the %s server due to IncompleteReadError. " , self ._device_type
116145 )
117146 except asyncio .CancelledError :
147+ _LOGGER .debug ("_data_received task was cancelled." )
118148 pass
119149 except (
120150 TimeoutError ,
@@ -126,6 +156,8 @@ async def _data_received(self):
126156 self ._device_type ,
127157 exc ,
128158 )
159+ except Exception as exc :
160+ _LOGGER .error ("Unexpected error in _data_received: %s" , exc )
129161 finally :
130162 self ._connected = False
131163 self ._connecting = False
0 commit comments