14141. when accept, return sequence id for response message of node
1515
1616"""
17+
1718from __future__ import annotations
1819
1920from asyncio import Future , Lock , Transport , get_running_loop , timeout
21+ from collections .abc import Callable
2022import logging
2123
2224from ..constants import STICK_TIME_OUT
3133class StickSender :
3234 """Send request messages though USB Stick transport connection."""
3335
34- def __init__ (
35- self , stick_receiver : StickReceiver , transport : Transport
36- ) -> None :
36+ def __init__ (self , stick_receiver : StickReceiver , transport : Transport ) -> None :
3737 """Initialize the Stick Sender class."""
3838 self ._loop = get_running_loop ()
3939 self ._receiver = stick_receiver
4040 self ._transport = transport
4141 self ._stick_response : Future [StickResponse ] | None = None
4242 self ._stick_lock = Lock ()
4343 self ._current_request : None | PlugwiseRequest = None
44+ self ._unsubscribe_stick_response : Callable [[], None ] | None = None
4445
46+ async def start (self ) -> None :
47+ """Start the sender."""
4548 # Subscribe to ACCEPT stick responses, which contain the seq_id we need.
4649 # Other stick responses are not related to this request.
4750 self ._unsubscribe_stick_response = (
48- self ._receiver .subscribe_to_stick_responses (
49- self ._process_stick_response , None , StickResponseType .ACCEPT
51+ await self ._receiver .subscribe_to_stick_responses (
52+ self ._process_stick_response , None , (StickResponseType .ACCEPT ,)
53+ # self._process_stick_response, None, (StickResponseType.ACCEPT, StickResponseType.FAILED)
5054 )
5155 )
5256
@@ -61,10 +65,6 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
6165
6266 request .add_send_attempt ()
6367 _LOGGER .info ("Send %s" , request )
64- request .subscribe_to_responses (
65- self ._receiver .subscribe_to_stick_responses ,
66- self ._receiver .subscribe_to_node_responses ,
67- )
6868
6969 # Write message to serial port buffer
7070 serialized_data = request .serialize ()
@@ -77,7 +77,11 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
7777 async with timeout (STICK_TIME_OUT ):
7878 response : StickResponse = await self ._stick_response
7979 except TimeoutError :
80- _LOGGER .warning ("USB-Stick did not respond within %s seconds after writing %s" , STICK_TIME_OUT , request )
80+ _LOGGER .warning (
81+ "USB-Stick did not respond within %s seconds after writing %s" ,
82+ STICK_TIME_OUT ,
83+ request ,
84+ )
8185 request .assign_error (
8286 BaseException (
8387 StickError (
@@ -89,25 +93,30 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
8993 _LOGGER .warning ("Exception for %s: %s" , request , exc )
9094 request .assign_error (exc )
9195 else :
96+ _LOGGER .debug (
97+ "USB-Stick replied with %s to request %s" , response , request
98+ )
9299 if response .response_type == StickResponseType .ACCEPT :
93100 request .seq_id = response .seq_id
94- _LOGGER .debug ("USB-Stick accepted %s with seq_id=%s" , request , response .seq_id )
101+ await request .subscribe_to_response (
102+ self ._receiver .subscribe_to_stick_responses ,
103+ self ._receiver .subscribe_to_node_responses ,
104+ )
95105 elif response .response_type == StickResponseType .TIMEOUT :
96- _LOGGER .warning ("USB-Stick responded with communication timeout for %s" , request )
106+ _LOGGER .warning (
107+ "USB-Stick directly responded with communication timeout for %s" ,
108+ request ,
109+ )
97110 request .assign_error (
98111 BaseException (
99- StickError (
100- f"USB-Stick responded with timeout for { request } "
101- )
112+ StickError (f"USB-Stick responded with timeout for { request } " )
102113 )
103114 )
104115 elif response .response_type == StickResponseType .FAILED :
105116 _LOGGER .warning ("USB-Stick failed communication for %s" , request )
106117 request .assign_error (
107118 BaseException (
108- StickError (
109- f"USB-Stick failed communication for { request } "
110- )
119+ StickError (f"USB-Stick failed communication for { request } " )
111120 )
112121 )
113122 finally :
@@ -124,4 +133,5 @@ async def _process_stick_response(self, response: StickResponse) -> None:
124133
125134 def stop (self ) -> None :
126135 """Stop sender."""
127- self ._unsubscribe_stick_response ()
136+ if self ._unsubscribe_stick_response is not None :
137+ self ._unsubscribe_stick_response ()
0 commit comments