Skip to content

Commit 4138888

Browse files
committed
ruff format according to ruless
1 parent d983bf7 commit 4138888

28 files changed

+139
-95
lines changed

plugwise_usb/__init__.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,43 @@
1919

2020
FuncT = 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

2628
def 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

3840
def 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

5155
class 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

plugwise_usb/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,5 +676,4 @@ async def message_for_node(self, message: Any) -> None:
676676
677677
"""
678678

679-
680679
# endregion

plugwise_usb/connection/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,15 @@ async def get_node_details(
209209
ping_response: NodePingResponse | None = None
210210
if ping_first:
211211
# Define ping request with one retry
212-
ping_request = NodePingRequest(
213-
self.send, bytes(mac, UTF8), retries=1
214-
)
212+
ping_request = NodePingRequest(self.send, bytes(mac, UTF8), retries=1)
215213
try:
216214
ping_response = await ping_request.send()
217215
except StickError:
218216
return (None, None)
219217
if ping_response is None:
220218
return (None, None)
221219

222-
info_request = NodeInfoRequest(
223-
self.send, bytes(mac, UTF8), retries=1
224-
)
220+
info_request = NodeInfoRequest(self.send, bytes(mac, UTF8), retries=1)
225221
try:
226222
info_response = await info_request.send()
227223
except StickError:

plugwise_usb/connection/queue.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ async def submit(self, request: PlugwiseRequest) -> PlugwiseResponse | None:
102102
if isinstance(request, NodePingRequest):
103103
# For ping requests it is expected to receive timeouts, so lower log level
104104
_LOGGER.debug(
105-
"%s, cancel because timeout is expected for NodePingRequests", exc
105+
"%s, cancel because timeout is expected for NodePingRequests",
106+
exc,
106107
)
107108
elif request.resend:
108109
_LOGGER.debug("%s, retrying", exc)
@@ -146,7 +147,9 @@ async def _send_queue_worker(self) -> None:
146147
if self._stick.queue_depth > 3:
147148
await sleep(0.125)
148149
if self._stick.queue_depth > 3:
149-
_LOGGER.warning("Awaiting plugwise responses %d", self._stick.queue_depth)
150+
_LOGGER.warning(
151+
"Awaiting plugwise responses %d", self._stick.queue_depth
152+
)
150153

151154
await self._stick.write_to_stick(request)
152155
self._submit_queue.task_done()

plugwise_usb/connection/receiver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,5 @@ async def _notify_node_response_subscribers(
513513
name=f"Postpone subscription task for {node_response.seq_id!r} retry {node_response.retries}",
514514
)
515515

516+
516517
# endregion

plugwise_usb/connection/sender.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
7979

8080
# Write message to serial port buffer
8181
serialized_data = request.serialize()
82-
_LOGGER.debug("write_request_to_port | Write %s to port as %s", request, serialized_data)
82+
_LOGGER.debug(
83+
"write_request_to_port | Write %s to port as %s", request, serialized_data
84+
)
8385
self._transport.write(serialized_data)
8486
# Don't timeout when no response expected
8587
if not request.no_response:
@@ -106,7 +108,11 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
106108
_LOGGER.warning("Exception for %s: %s", request, exc)
107109
request.assign_error(exc)
108110
else:
109-
_LOGGER.debug("write_request_to_port | USB-Stick replied with %s to request %s", response, request)
111+
_LOGGER.debug(
112+
"write_request_to_port | USB-Stick replied with %s to request %s",
113+
response,
114+
request,
115+
)
110116
if response.response_type == StickResponseType.ACCEPT:
111117
if request.seq_id is not None:
112118
request.assign_error(
@@ -121,7 +127,9 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
121127
self._receiver.subscribe_to_stick_responses,
122128
self._receiver.subscribe_to_node_responses,
123129
)
124-
_LOGGER.debug("write_request_to_port | request has subscribed : %s", request)
130+
_LOGGER.debug(
131+
"write_request_to_port | request has subscribed : %s", request
132+
)
125133
elif response.response_type == StickResponseType.TIMEOUT:
126134
_LOGGER.warning(
127135
"USB-Stick directly responded with communication timeout for %s",
@@ -143,7 +151,6 @@ async def write_request_to_port(self, request: PlugwiseRequest) -> None:
143151
self._stick_response.cancel()
144152
self._stick_lock.release()
145153

146-
147154
async def _process_stick_response(self, response: StickResponse) -> None:
148155
"""Process stick response."""
149156
if self._stick_response is None or self._stick_response.done():

plugwise_usb/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Plugwise Stick constants."""
2+
23
from __future__ import annotations
34

45
import datetime as dt

plugwise_usb/helpers/cache.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ async def initialize_cache(self, create_root_folder: bool = False) -> None:
5353
"""Set (and create) the plugwise cache directory to store cache file."""
5454
if self._root_dir != "":
5555
if not create_root_folder and not await ospath.exists(self._root_dir):
56-
raise CacheError(f"Unable to initialize caching. Cache folder '{self._root_dir}' does not exists.")
56+
raise CacheError(
57+
f"Unable to initialize caching. Cache folder '{self._root_dir}' does not exists."
58+
)
5759
cache_dir = self._root_dir
5860
else:
5961
cache_dir = self._get_writable_os_dir()
@@ -72,13 +74,17 @@ def _get_writable_os_dir(self) -> str:
7274
if os_name == "nt":
7375
if (data_dir := os_getenv("APPDATA")) is not None:
7476
return os_path_join(data_dir, CACHE_DIR)
75-
raise CacheError("Unable to detect writable cache folder based on 'APPDATA' environment variable.")
77+
raise CacheError(
78+
"Unable to detect writable cache folder based on 'APPDATA' environment variable."
79+
)
7680
return os_path_join(os_path_expand_user("~"), CACHE_DIR)
7781

7882
async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None:
79-
""""Save information to cache file."""
83+
""" "Save information to cache file."""
8084
if not self._initialized:
81-
raise CacheError(f"Unable to save cache. Initialize cache file '{self._file_name}' first.")
85+
raise CacheError(
86+
f"Unable to save cache. Initialize cache file '{self._file_name}' first."
87+
)
8288

8389
current_data: dict[str, str] = {}
8490
if not rewrite:
@@ -111,19 +117,20 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None
111117
if not self._cache_file_exists:
112118
self._cache_file_exists = True
113119
_LOGGER.debug(
114-
"Saved %s lines to cache file %s",
115-
str(len(data)),
116-
self._cache_file
120+
"Saved %s lines to cache file %s", str(len(data)), self._cache_file
117121
)
118122

119123
async def read_cache(self) -> dict[str, str]:
120124
"""Return current data from cache file."""
121125
if not self._initialized:
122-
raise CacheError(f"Unable to save cache. Initialize cache file '{self._file_name}' first.")
126+
raise CacheError(
127+
f"Unable to save cache. Initialize cache file '{self._file_name}' first."
128+
)
123129
current_data: dict[str, str] = {}
124130
if not self._cache_file_exists:
125131
_LOGGER.debug(
126-
"Cache file '%s' does not exists, return empty cache data", self._cache_file
132+
"Cache file '%s' does not exists, return empty cache data",
133+
self._cache_file,
127134
)
128135
return current_data
129136
try:
@@ -146,10 +153,10 @@ async def read_cache(self) -> dict[str, str]:
146153
_LOGGER.warning(
147154
"Skip invalid line '%s' in cache file %s",
148155
data,
149-
str(self._cache_file)
156+
str(self._cache_file),
150157
)
151158
break
152-
current_data[data[:index_separator]] = data[index_separator + 1:]
159+
current_data[data[:index_separator]] = data[index_separator + 1 :]
153160
return current_data
154161

155162
async def delete_cache(self) -> None:

plugwise_usb/helpers/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Plugwise utility helpers."""
2+
23
from __future__ import annotations
34

45
import re
@@ -21,7 +22,7 @@ def validate_mac(mac: str) -> bool:
2122
return True
2223

2324

24-
def version_to_model(version: str | None) -> tuple[str|None, str]:
25+
def version_to_model(version: str | None) -> tuple[str | None, str]:
2526
"""Translate hardware_version to device type."""
2627
if version is None:
2728
return (None, "Unknown")

plugwise_usb/messages/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Priority(Enum):
1919
MEDIUM = 2
2020
LOW = 3
2121

22+
2223
class PlugwiseMessage:
2324
"""Plugwise message base class."""
2425

0 commit comments

Comments
 (0)