2020
2121FuncT = TypeVar ("FuncT" , bound = Callable [..., Any ])
2222
23- NOT_INITIALIZED_STICK_ERROR : Final [StickError ] = StickError ("Cannot load nodes when network is not initialized" )
23+ NOT_INITIALIZED_STICK_ERROR : Final [StickError ] = StickError (
24+ "Cannot load nodes when network is not initialized"
25+ )
2426_LOGGER = logging .getLogger (__name__ )
2527
2628
2729def raise_not_connected (func : FuncT ) -> FuncT :
2830 """Validate existence of an active connection to Stick. Raise StickError when there is no active connection."""
31+
2932 @wraps (func )
3033 def decorated (* args : Any , ** kwargs : Any ) -> Any :
3134 if not args [0 ].is_connected :
32- raise StickError (
33- "Not connected to USB-Stick, connect to USB-stick first."
34- )
35+ raise StickError ("Not connected to USB-Stick, connect to USB-stick first." )
3536 return func (* args , ** kwargs )
37+
3638 return cast (FuncT , decorated )
3739
3840
3941def raise_not_initialized (func : FuncT ) -> FuncT :
4042 """Validate if active connection is initialized. Raise StickError when not initialized."""
43+
4144 @wraps (func )
4245 def decorated (* args : Any , ** kwargs : Any ) -> Any :
4346 if not args [0 ].is_initialized :
4447 raise StickError (
45- "Connection to USB-Stick is not initialized, " +
46- "initialize USB-stick first."
48+ "Connection to USB-Stick is not initialized, "
49+ + "initialize USB-stick first."
4750 )
4851 return func (* args , ** kwargs )
52+
4953 return cast (FuncT , decorated )
5054
5155
5256class Stick :
5357 """Plugwise connection stick."""
5458
55- def __init__ (
56- self , port : str | None = None , cache_enabled : bool = True
57- ) -> None :
59+ def __init__ (self , port : str | None = None , cache_enabled : bool = True ) -> None :
5860 """Initialize Stick."""
5961 self ._loop = get_running_loop ()
6062 self ._loop .set_debug (True )
@@ -170,13 +172,8 @@ def port(self) -> str | None:
170172 @port .setter
171173 def port (self , port : str ) -> None :
172174 """Path to serial port of USB-Stick."""
173- if (
174- self ._controller .is_connected
175- and port != self ._port
176- ):
177- raise StickError (
178- "Unable to change port while connected. Disconnect first"
179- )
175+ if self ._controller .is_connected and port != self ._port :
176+ raise StickError ("Unable to change port while connected. Disconnect first" )
180177
181178 self ._port = port
182179
@@ -269,7 +266,9 @@ def subscribe_to_node_events(
269266 Returns the function to be called to unsubscribe later.
270267 """
271268 if self ._network is None :
272- raise SubscriptionError ("Unable to subscribe to node events without network connection initialized" )
269+ raise SubscriptionError (
270+ "Unable to subscribe to node events without network connection initialized"
271+ )
273272 return self ._network .subscribe_to_node_events (
274273 node_event_callback ,
275274 events ,
@@ -283,9 +282,7 @@ def _validate_node_discovery(self) -> None:
283282 if self ._network is None or not self ._network .is_running :
284283 raise StickError ("Plugwise network node discovery is not active." )
285284
286- async def setup (
287- self , discover : bool = True , load : bool = True
288- ) -> None :
285+ async def setup (self , discover : bool = True , load : bool = True ) -> None :
289286 """Fully connect, initialize USB-Stick and discover all connected nodes."""
290287 if not self .is_connected :
291288 await self .connect ()
@@ -302,17 +299,17 @@ async def connect(self, port: str | None = None) -> None:
302299 """Connect to USB-Stick. Raises StickError if connection fails."""
303300 if self ._controller .is_connected :
304301 raise StickError (
305- f"Already connected to { self ._port } , " +
306- "Close existing connection before (re)connect."
302+ f"Already connected to { self ._port } , "
303+ + "Close existing connection before (re)connect."
307304 )
308305
309306 if port is not None :
310307 self ._port = port
311308
312309 if self ._port is None :
313310 raise StickError (
314- "Unable to connect. " +
315- "Path to USB-Stick is not defined, set port property first"
311+ "Unable to connect. "
312+ + "Path to USB-Stick is not defined, set port property first"
316313 )
317314
318315 await self ._controller .connect_to_stick (
@@ -350,9 +347,7 @@ async def load_nodes(self) -> bool:
350347 if self ._network is None :
351348 raise NOT_INITIALIZED_STICK_ERROR
352349 if not self ._network .is_running :
353- raise StickError (
354- "Cannot load nodes when network is not started"
355- )
350+ raise StickError ("Cannot load nodes when network is not started" )
356351 return await self ._network .discover_nodes (load = True )
357352
358353 @raise_not_connected
0 commit comments