33from __future__ import annotations
44
55from asyncio import Future , gather , get_event_loop , wait_for
6- from collections .abc import Awaitable , Callable
6+ from collections .abc import Awaitable , Callable , Coroutine
77import logging
88from typing import Any
99
@@ -32,18 +32,26 @@ def __init__(self) -> None:
3232 self ._connected : bool = False
3333 self ._stick_event_subscribers : dict [
3434 Callable [[], None ],
35- tuple [Callable [[StickEvent ], Awaitable [None ]], StickEvent | None ]
35+ tuple [Callable [[StickEvent ], Awaitable [None ]], tuple [ StickEvent , ...]],
3636 ] = {}
3737 self ._unsubscribe_stick_events : Callable [[], None ] | None = None
3838
3939 @property
4040 def reduce_receive_logging (self ) -> bool :
4141 """Return if logging must reduced."""
42+ if self ._receiver is None :
43+ raise StickError (
44+ "Unable to return log settings when connection is not active."
45+ )
4246 return self ._receiver .reduce_logging
4347
4448 @reduce_receive_logging .setter
4549 def reduce_receive_logging (self , state : bool ) -> None :
4650 """Reduce logging of unhandled received messages."""
51+ if self ._receiver is None :
52+ raise StickError (
53+ "Unable to set log settings when connection is not active."
54+ )
4755 self ._receiver .reduce_logging = state
4856
4957 @property
@@ -62,14 +70,12 @@ def is_connected(self) -> bool:
6270
6371 def _subscribe_to_stick_events (self ) -> None :
6472 """Subscribe to handle stick events by manager."""
65- if not self .is_connected :
73+ if not self .is_connected or self . _receiver is None :
6674 raise StickError ("Unable to subscribe to events" )
6775 if self ._unsubscribe_stick_events is None :
68- self ._unsubscribe_stick_events = (
69- self ._receiver .subscribe_to_stick_events (
70- self ._handle_stick_event ,
71- (StickEvent .CONNECTED , StickEvent .DISCONNECTED )
72- )
76+ self ._unsubscribe_stick_events = self ._receiver .subscribe_to_stick_events (
77+ self ._handle_stick_event ,
78+ (StickEvent .CONNECTED , StickEvent .DISCONNECTED ),
7379 )
7480
7581 async def _handle_stick_event (
@@ -79,47 +85,47 @@ async def _handle_stick_event(
7985 """Call callback for stick event subscribers."""
8086 if len (self ._stick_event_subscribers ) == 0 :
8187 return
82- callback_list : list [Callable ] = []
83- for callback , filtered_events in list (
84- self ._stick_event_subscribers .values ()
85- ):
86- if event in filtered_events :
88+ callback_list : list [Awaitable [None ]] = []
89+ for callback , stick_events in self ._stick_event_subscribers .values ():
90+ if event in stick_events :
8791 callback_list .append (callback (event ))
8892 if len (callback_list ) > 0 :
8993 await gather (* callback_list )
9094
9195 def subscribe_to_stick_events (
9296 self ,
9397 stick_event_callback : Callable [[StickEvent ], Awaitable [None ]],
94- events : tuple [StickEvent ],
98+ events : tuple [StickEvent , ... ],
9599 ) -> Callable [[], None ]:
96100 """Subscribe callback when specified StickEvent occurs.
97101
98102 Returns the function to be called to unsubscribe later.
99103 """
104+
100105 def remove_subscription () -> None :
101106 """Remove stick event subscription."""
102107 self ._stick_event_subscribers .pop (remove_subscription )
103- self ._stick_event_subscribers [remove_subscription ] = (stick_event_callback , events )
108+
109+ self ._stick_event_subscribers [remove_subscription ] = (
110+ stick_event_callback ,
111+ events ,
112+ )
104113 return remove_subscription
105114
106115 def subscribe_to_stick_replies (
107116 self ,
108- callback : Callable [
109- [StickResponse ], Awaitable [None ]
110- ],
117+ callback : Callable [[StickResponse ], Coroutine [Any , Any , None ]],
111118 ) -> Callable [[], None ]:
112119 """Subscribe to response messages from stick."""
113120 if self ._receiver is None or not self ._receiver .is_connected :
114121 raise StickError (
115- "Unable to subscribe to stick response when receiver " +
116- "is not loaded"
122+ "Unable to subscribe to stick response when receiver " + "is not loaded"
117123 )
118124 return self ._receiver .subscribe_to_stick_responses (callback )
119125
120126 def subscribe_to_node_responses (
121127 self ,
122- node_response_callback : Callable [[PlugwiseResponse ], Awaitable [ None ]],
128+ node_response_callback : Callable [[PlugwiseResponse ], Coroutine [ Any , Any , bool ]],
123129 mac : bytes | None = None ,
124130 message_ids : tuple [bytes ] | None = None ,
125131 ) -> Callable [[], None ]:
@@ -129,21 +135,18 @@ def subscribe_to_node_responses(
129135 """
130136 if self ._receiver is None or not self ._receiver .is_connected :
131137 raise StickError (
132- "Unable to subscribe to node response when receiver " +
133- "is not loaded"
138+ "Unable to subscribe to node response when receiver " + "is not loaded"
134139 )
135140 return self ._receiver .subscribe_to_node_responses (
136141 node_response_callback , mac , message_ids
137142 )
138143
139- async def setup_connection_to_stick (
140- self , serial_path : str
141- ) -> None :
144+ async def setup_connection_to_stick (self , serial_path : str ) -> None :
142145 """Create serial connection to USB-stick."""
143146 if self ._connected :
144147 raise StickError ("Cannot setup connection, already connected" )
145148 loop = get_event_loop ()
146- connected_future : Future [Any ] = Future ()
149+ connected_future : Future [bool ] = Future ()
147150 self ._receiver = StickReceiver (connected_future )
148151 self ._port = serial_path
149152
@@ -187,14 +190,14 @@ async def write_to_stick(self, request: PlugwiseRequest) -> None:
187190 _LOGGER .debug ("Write to USB-stick: %s" , request )
188191 if not request .resend :
189192 raise StickError (
190- f"Failed to send { request .__class__ .__name__ } " +
191- f"to node { request .mac_decoded } , maximum number " +
192- f"of retries ({ request .max_retries } ) has been reached"
193+ f"Failed to send { request .__class__ .__name__ } "
194+ + f"to node { request .mac_decoded } , maximum number "
195+ + f"of retries ({ request .max_retries } ) has been reached"
193196 )
194197 if self ._sender is None :
195198 raise StickError (
196- f"Failed to send { request .__class__ .__name__ } " +
197- "because USB-Stick connection is not setup"
199+ f"Failed to send { request .__class__ .__name__ } "
200+ + "because USB-Stick connection is not setup"
198201 )
199202 await self ._sender .write_request_to_port (request )
200203
0 commit comments