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
@@ -189,10 +186,8 @@ async def energy_reset_request(self, mac: str) -> bool:
189186 raise NodeError (f"{ exc } " ) from exc
190187
191188 # Follow up by an energy-intervals (re)set
192- if (
193- result := await self .set_energy_intervals (
194- mac , DEFAULT_CONS_INTERVAL , NO_PRODUCTION_INTERVAL
195- )
189+ if result := await self .set_energy_intervals (
190+ mac , DEFAULT_CONS_INTERVAL , NO_PRODUCTION_INTERVAL
196191 ):
197192 return result
198193
@@ -238,7 +233,9 @@ def subscribe_to_node_events(
238233 Returns the function to be called to unsubscribe later.
239234 """
240235 if self ._network is None :
241- raise SubscriptionError ("Unable to subscribe to node events without network connection initialized" )
236+ raise SubscriptionError (
237+ "Unable to subscribe to node events without network connection initialized"
238+ )
242239 return self ._network .subscribe_to_node_events (
243240 node_event_callback ,
244241 events ,
@@ -252,9 +249,7 @@ def _validate_node_discovery(self) -> None:
252249 if self ._network is None or not self ._network .is_running :
253250 raise StickError ("Plugwise network node discovery is not active." )
254251
255- async def setup (
256- self , discover : bool = True , load : bool = True
257- ) -> None :
252+ async def setup (self , discover : bool = True , load : bool = True ) -> None :
258253 """Fully connect, initialize USB-Stick and discover all connected nodes."""
259254 if not self .is_connected :
260255 await self .connect ()
@@ -271,17 +266,17 @@ async def connect(self, port: str | None = None) -> None:
271266 """Connect to USB-Stick. Raises StickError if connection fails."""
272267 if self ._controller .is_connected :
273268 raise StickError (
274- f"Already connected to { self ._port } , " +
275- "Close existing connection before (re)connect."
269+ f"Already connected to { self ._port } , "
270+ + "Close existing connection before (re)connect."
276271 )
277272
278273 if port is not None :
279274 self ._port = port
280275
281276 if self ._port is None :
282277 raise StickError (
283- "Unable to connect. " +
284- "Path to USB-Stick is not defined, set port property first"
278+ "Unable to connect. "
279+ + "Path to USB-Stick is not defined, set port property first"
285280 )
286281
287282 await self ._controller .connect_to_stick (
@@ -319,9 +314,7 @@ async def load_nodes(self) -> bool:
319314 if self ._network is None :
320315 raise NOT_INITIALIZED_STICK_ERROR
321316 if not self ._network .is_running :
322- raise StickError (
323- "Cannot load nodes when network is not started"
324- )
317+ raise StickError ("Cannot load nodes when network is not started" )
325318 return await self ._network .discover_nodes (load = True )
326319
327320 @raise_not_connected
0 commit comments