1919
2020FuncT = TypeVar ("FuncT" , bound = Callable [..., Any ])
2121
22- NOT_INITIALIZED_STICK_ERROR : Final [StickError ] = StickError ("Cannot load nodes when network is not initialized" )
22+ NOT_INITIALIZED_STICK_ERROR : Final [StickError ] = StickError (
23+ "Cannot load nodes when network is not initialized"
24+ )
2325_LOGGER = logging .getLogger (__name__ )
2426
2527
2628def raise_not_connected (func : FuncT ) -> FuncT :
2729 """Validate existence of an active connection to Stick. Raise StickError when there is no active connection."""
30+
2831 @wraps (func )
2932 def decorated (* args : Any , ** kwargs : Any ) -> Any :
3033 if not args [0 ].is_connected :
31- raise StickError (
32- "Not connected to USB-Stick, connect to USB-stick first."
33- )
34+ raise StickError ("Not connected to USB-Stick, connect to USB-stick first." )
3435 return func (* args , ** kwargs )
36+
3537 return cast (FuncT , decorated )
3638
3739
3840def raise_not_initialized (func : FuncT ) -> FuncT :
3941 """Validate if active connection is initialized. Raise StickError when not initialized."""
42+
4043 @wraps (func )
4144 def decorated (* args : Any , ** kwargs : Any ) -> Any :
4245 if not args [0 ].is_initialized :
4346 raise StickError (
44- "Connection to USB-Stick is not initialized, " +
45- "initialize USB-stick first."
47+ "Connection to USB-Stick is not initialized, "
48+ + "initialize USB-stick first."
4649 )
4750 return func (* args , ** kwargs )
51+
4852 return cast (FuncT , decorated )
4953
5054
5155class Stick :
5256 """Plugwise connection stick."""
5357
54- def __init__ (
55- self , port : str | None = None , cache_enabled : bool = True
56- ) -> None :
58+ def __init__ (self , port : str | None = None , cache_enabled : bool = True ) -> None :
5759 """Initialize Stick."""
5860 self ._loop = get_running_loop ()
5961 self ._loop .set_debug (True )
@@ -169,13 +171,8 @@ def port(self) -> str | None:
169171 @port .setter
170172 def port (self , port : str ) -> None :
171173 """Path to serial port of USB-Stick."""
172- if (
173- self ._controller .is_connected
174- and port != self ._port
175- ):
176- raise StickError (
177- "Unable to change port while connected. Disconnect first"
178- )
174+ if self ._controller .is_connected and port != self ._port :
175+ raise StickError ("Unable to change port while connected. Disconnect first" )
179176
180177 self ._port = port
181178
@@ -250,7 +247,9 @@ def subscribe_to_node_events(
250247 Returns the function to be called to unsubscribe later.
251248 """
252249 if self ._network is None :
253- raise SubscriptionError ("Unable to subscribe to node events without network connection initialized" )
250+ raise SubscriptionError (
251+ "Unable to subscribe to node events without network connection initialized"
252+ )
254253 return self ._network .subscribe_to_node_events (
255254 node_event_callback ,
256255 events ,
@@ -264,9 +263,7 @@ def _validate_node_discovery(self) -> None:
264263 if self ._network is None or not self ._network .is_running :
265264 raise StickError ("Plugwise network node discovery is not active." )
266265
267- async def setup (
268- self , discover : bool = True , load : bool = True
269- ) -> None :
266+ async def setup (self , discover : bool = True , load : bool = True ) -> None :
270267 """Fully connect, initialize USB-Stick and discover all connected nodes."""
271268 if not self .is_connected :
272269 await self .connect ()
@@ -283,17 +280,17 @@ async def connect(self, port: str | None = None) -> None:
283280 """Connect to USB-Stick. Raises StickError if connection fails."""
284281 if self ._controller .is_connected :
285282 raise StickError (
286- f"Already connected to { self ._port } , " +
287- "Close existing connection before (re)connect."
283+ f"Already connected to { self ._port } , "
284+ + "Close existing connection before (re)connect."
288285 )
289286
290287 if port is not None :
291288 self ._port = port
292289
293290 if self ._port is None :
294291 raise StickError (
295- "Unable to connect. " +
296- "Path to USB-Stick is not defined, set port property first"
292+ "Unable to connect. "
293+ + "Path to USB-Stick is not defined, set port property first"
297294 )
298295
299296 await self ._controller .connect_to_stick (
@@ -331,9 +328,7 @@ async def load_nodes(self) -> bool:
331328 if self ._network is None :
332329 raise NOT_INITIALIZED_STICK_ERROR
333330 if not self ._network .is_running :
334- raise StickError (
335- "Cannot load nodes when network is not started"
336- )
331+ raise StickError ("Cannot load nodes when network is not started" )
337332 return await self ._network .discover_nodes (load = True )
338333
339334 @raise_not_connected
0 commit comments