37
37
SuccessResultMessage ,
38
38
)
39
39
from .connection import MatterClientConnection
40
- from .exceptions import ConnectionClosed , InvalidServerVersion , InvalidState
40
+ from .exceptions import ConnectionClosed , InvalidState , ServerVersionTooOld
41
41
from .models .node import (
42
42
MatterFabricData ,
43
43
MatterNode ,
@@ -509,31 +509,42 @@ async def interview_node(self, node_id: int) -> None:
509
509
"""Interview a node."""
510
510
await self .send_command (APICommand .INTERVIEW_NODE , node_id = node_id )
511
511
512
- async def send_command (
512
+ def _prepare_message (
513
513
self ,
514
514
command : str ,
515
515
require_schema : int | None = None ,
516
516
** kwargs : Any ,
517
- ) -> Any :
518
- """Send a command and get a response."""
519
- if not self .connection .connected or not self ._loop :
517
+ ) -> CommandMessage :
518
+ if not self .connection .connected :
520
519
raise InvalidState ("Not connected" )
521
520
522
521
if (
523
522
require_schema is not None
524
523
and self .server_info is not None
525
524
and require_schema > self .server_info .schema_version
526
525
):
527
- raise InvalidServerVersion (
526
+ raise ServerVersionTooOld (
528
527
"Command not available due to incompatible server version. Update the Matter "
529
- f"Server to a version that supports at least api schema { require_schema } ."
528
+ f"Server to a version that supports at least api schema { require_schema } ." ,
530
529
)
531
530
532
- message = CommandMessage (
531
+ return CommandMessage (
533
532
message_id = uuid .uuid4 ().hex ,
534
533
command = command ,
535
534
args = kwargs ,
536
535
)
536
+
537
+ async def send_command (
538
+ self ,
539
+ command : str ,
540
+ require_schema : int | None = None ,
541
+ ** kwargs : Any ,
542
+ ) -> Any :
543
+ """Send a command and get a response."""
544
+ if not self ._loop :
545
+ raise InvalidState ("Not connected" )
546
+
547
+ message = self ._prepare_message (command , require_schema , ** kwargs )
537
548
future : asyncio .Future [Any ] = self ._loop .create_future ()
538
549
self ._result_futures [message .message_id ] = future
539
550
await self .connection .send_message (message )
@@ -549,22 +560,8 @@ async def send_command_no_wait(
549
560
** kwargs : Any ,
550
561
) -> None :
551
562
"""Send a command without waiting for the response."""
552
- if not self .server_info :
553
- raise InvalidState ("Not connected" )
554
563
555
- if (
556
- require_schema is not None
557
- and require_schema > self .server_info .schema_version
558
- ):
559
- raise InvalidServerVersion (
560
- "Command not available due to incompatible server version. Update the Matter "
561
- f"Server to a version that supports at least api schema { require_schema } ."
562
- )
563
- message = CommandMessage (
564
- message_id = uuid .uuid4 ().hex ,
565
- command = command ,
566
- args = kwargs ,
567
- )
564
+ message = self ._prepare_message (command , require_schema , ** kwargs )
568
565
await self .connection .send_message (message )
569
566
570
567
async def get_diagnostics (self ) -> ServerDiagnostics :
0 commit comments