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
@@ -238,7 +235,9 @@ def subscribe_to_node_events(
238235 Returns the function to be called to unsubscribe later.
239236 """
240237 if self ._network is None :
241- raise SubscriptionError ("Unable to subscribe to node events without network connection initialized" )
238+ raise SubscriptionError (
239+ "Unable to subscribe to node events without network connection initialized"
240+ )
242241 return self ._network .subscribe_to_node_events (
243242 node_event_callback ,
244243 events ,
@@ -252,9 +251,7 @@ def _validate_node_discovery(self) -> None:
252251 if self ._network is None or not self ._network .is_running :
253252 raise StickError ("Plugwise network node discovery is not active." )
254253
255- async def setup (
256- self , discover : bool = True , load : bool = True
257- ) -> None :
254+ async def setup (self , discover : bool = True , load : bool = True ) -> None :
258255 """Fully connect, initialize USB-Stick and discover all connected nodes."""
259256 if not self .is_connected :
260257 await self .connect ()
@@ -271,17 +268,17 @@ async def connect(self, port: str | None = None) -> None:
271268 """Connect to USB-Stick. Raises StickError if connection fails."""
272269 if self ._controller .is_connected :
273270 raise StickError (
274- f"Already connected to { self ._port } , " +
275- "Close existing connection before (re)connect."
271+ f"Already connected to { self ._port } , "
272+ + "Close existing connection before (re)connect."
276273 )
277274
278275 if port is not None :
279276 self ._port = port
280277
281278 if self ._port is None :
282279 raise StickError (
283- "Unable to connect. " +
284- "Path to USB-Stick is not defined, set port property first"
280+ "Unable to connect. "
281+ + "Path to USB-Stick is not defined, set port property first"
285282 )
286283
287284 await self ._controller .connect_to_stick (
@@ -319,9 +316,7 @@ async def load_nodes(self) -> bool:
319316 if self ._network is None :
320317 raise NOT_INITIALIZED_STICK_ERROR
321318 if not self ._network .is_running :
322- raise StickError (
323- "Cannot load nodes when network is not started"
324- )
319+ raise StickError ("Cannot load nodes when network is not started" )
325320 return await self ._network .discover_nodes (load = True )
326321
327322 @raise_not_connected
0 commit comments