14
14
from async_timeout import timeout
15
15
16
16
from zha .event import EventBase
17
- from zha .exceptions import ZHAException
17
+ from zha .websocket import ZHAWebSocketException
18
18
from zha .websocket .client .model .messages import Message
19
19
from zha .websocket .server .api .model import WebSocketCommand , WebSocketCommandResponse
20
20
@@ -28,8 +28,8 @@ class Client(EventBase):
28
28
def __init__ (
29
29
self ,
30
30
ws_server_url : str ,
31
- aiohttp_session : ClientSession | None = None ,
32
31
* args : Any ,
32
+ aiohttp_session : ClientSession | None = None ,
33
33
** kwargs : Any ,
34
34
) -> None :
35
35
"""Initialize the Client class."""
@@ -120,7 +120,7 @@ async def connect(self) -> None:
120
120
)
121
121
except client_exceptions .ClientError as err :
122
122
_LOGGER .exception ("Error connecting to server" , exc_info = err )
123
- raise err
123
+ raise ZHAWebSocketException from err
124
124
125
125
async def listen_loop (self ) -> None :
126
126
"""Listen to the websocket."""
@@ -132,7 +132,7 @@ async def listen_loop(self) -> None:
132
132
async def listen (self ) -> None :
133
133
"""Start listening to the websocket."""
134
134
if not self .connected :
135
- raise Exception ("Not connected when start listening" ) # noqa: TRY002
135
+ raise ZHAWebSocketException ("Not connected when start listening" )
136
136
137
137
assert self ._client
138
138
@@ -170,21 +170,21 @@ async def _receive_json_or_raise(self) -> dict:
170
170
msg = await self ._client .receive ()
171
171
172
172
if msg .type in (WSMsgType .CLOSE , WSMsgType .CLOSED , WSMsgType .CLOSING ):
173
- raise Exception ( "Connection was closed." ) # noqa: TRY002
173
+ raise ZHAWebSocketException ( f "Connection was closed: { msg } " )
174
174
175
175
if msg .type == WSMsgType .ERROR :
176
- raise Exception () # noqa: TRY002
176
+ raise ZHAWebSocketException ( f"WS message type was ERROR: { msg } " )
177
177
178
178
if msg .type != WSMsgType .TEXT :
179
- raise Exception (f"Received non-Text message: { msg . type } " ) # noqa: TRY002
179
+ raise ZHAWebSocketException (f"Received non-Text message: { msg } " )
180
180
181
181
try :
182
182
if len (msg .data ) > SIZE_PARSE_JSON_EXECUTOR :
183
183
data : dict = await self ._loop .run_in_executor (None , msg .json )
184
184
else :
185
185
data = msg .json ()
186
186
except ValueError as err :
187
- raise Exception ( "Received invalid JSON. " ) from err # noqa: TRY002
187
+ raise ZHAWebSocketException ( f "Received invalid JSON: { msg } " ) from err
188
188
189
189
if _LOGGER .isEnabledFor (logging .DEBUG ):
190
190
_LOGGER .debug ("Received message:\n %s\n " , pprint .pformat (msg ))
@@ -199,12 +199,12 @@ def _handle_incoming_message(self, msg: dict) -> None:
199
199
200
200
try :
201
201
message = Message .model_validate (msg ).root
202
- except Exception as err :
202
+ except Exception as err : # pylint: disable=broad-except
203
203
_LOGGER .exception ("Error parsing message: %s" , msg , exc_info = err )
204
204
if msg ["message_type" ] == "result" :
205
205
future = self ._result_futures .get (msg ["message_id" ])
206
206
if future is not None :
207
- future .set_exception (err )
207
+ future .set_exception (ZHAWebSocketException ( err ) )
208
208
return
209
209
return
210
210
@@ -220,9 +220,9 @@ def _handle_incoming_message(self, msg: dict) -> None:
220
220
return
221
221
222
222
if msg ["error_code" ] != "zigbee_error" :
223
- error = ZHAException (msg ["message_id" ], msg ["error_code" ])
223
+ error = ZHAWebSocketException (msg ["message_id" ], msg ["error_code" ])
224
224
else :
225
- error = ZHAException (
225
+ error = ZHAWebSocketException (
226
226
msg ["message_id" ],
227
227
msg ["zigbee_error_code" ],
228
228
msg ["zigbee_error_message" ],
@@ -242,16 +242,17 @@ def _handle_incoming_message(self, msg: dict) -> None:
242
242
243
243
try :
244
244
self .emit (message .event_type , message )
245
- except Exception as err :
245
+ except Exception as err : # pylint: disable=broad-except
246
246
_LOGGER .exception ("Error handling event" , exc_info = err )
247
+ raise ZHAWebSocketException from err
247
248
248
249
async def _send_json_message (self , message : str ) -> None :
249
250
"""Send a message.
250
251
251
252
Raises NotConnected if client not connected.
252
253
"""
253
254
if not self .connected :
254
- raise Exception () # noqa: TRY002
255
+ raise ZHAWebSocketException ( "Sending message failed: no active connection." )
255
256
256
257
_LOGGER .debug ("Publishing message:\n %s\n " , pprint .pformat (message ))
257
258
0 commit comments