Skip to content

Commit 7682419

Browse files
committed
Properly set up ruff as the formatter
1 parent 7a88ed4 commit 7682419

13 files changed

+46
-32
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ repos:
1616
rev: v0.9.4
1717
hooks:
1818
- id: ruff
19+
- id: ruff-format
20+
args: [--check]
1921
# Temporarily disabled due to crash, probably caused by us having to use HA stubs which are out of date?
2022
# See https://github.com/nathanmarlor/foxess_modbus/actions/runs/12610946302/job/35146070831?pr=720
2123
# - repo: https://github.com/pre-commit/mirrors-mypy

.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"[python]": {
2626
"editor.codeActionsOnSave": {
2727
"source.fixAll": "explicit"
28-
},
29-
"editor.defaultFormatter": "charliermarsh.ruff"
28+
}
3029
},
3130
"python.analysis.extraPaths": [
3231
"./custom_components/foxess_modbus/vendor/pymodbus/pymodbus-3.6.9"

custom_components/foxess_modbus/client/modbus_client.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def __init__(self, hass: HomeAssistant, protocol: str, adapter: InverterAdapter,
8888

8989
# Some serial devices need a short delay after polling. Also do this for the inverter, just
9090
# in case it helps.
91-
self._poll_delay = 30 / 1000 if protocol == SERIAL or adapter.connection_type == ConnectionType.LAN else 0
91+
self._poll_delay = 30 / \
92+
1000 if protocol == SERIAL or adapter.connection_type == ConnectionType.LAN else 0
9293

9394
self._client = client["client"](**config)
9495

@@ -127,19 +128,23 @@ async def read_registers(
127128

128129
if response.isError():
129130
message = (
130-
f"Error reading registers. Type: {register_type}; start: {start_address}; count: {num_registers}; "
131+
f"Error reading registers. Type: {register_type}; start: {
132+
start_address}; count: {num_registers}; "
131133
f"slave: {slave}"
132134
)
133135
if isinstance(response, Exception):
134-
raise ModbusClientFailedError(message, self, response) from response
136+
raise ModbusClientFailedError(
137+
message, self, response) from response
135138
raise ModbusClientFailedError(message, self, response)
136139

137140
# We've seen cases where the remote device gets two requests at the same time and sends the wrong response to
138141
# the wrong thing. pymodbus doesn't check whether the response type matches the request type
139142
if not isinstance(response, expected_response_type):
140143
message = (
141-
f"Error reading registers. Type: {register_type}; start: {start_address}; count: {num_registers}; "
142-
f"slave: {slave}. Received incorrect response type {response}. Please ensure that your adapter is "
144+
f"Error reading registers. Type: {register_type}; start: {
145+
start_address}; count: {num_registers}; "
146+
f"slave: {slave}. Received incorrect response type {
147+
response}. Please ensure that your adapter is "
143148
"correctly configured to allow multiple connections, see the instructions at "
144149
"https://github.com/nathanmarlor/foxess_modbus/wiki"
145150
)
@@ -175,17 +180,21 @@ async def write_registers(self, register_address: int, register_values: list[int
175180
expected_response_type = WriteSingleRegisterResponse
176181

177182
if response.isError():
178-
message = f"Error writing registers. Start: {register_address}; values: {register_values}; slave: {slave}"
183+
message = f"Error writing registers. Start: {
184+
register_address}; values: {register_values}; slave: {slave}"
179185
if isinstance(response, Exception):
180-
raise ModbusClientFailedError(message, self, response) from response
186+
raise ModbusClientFailedError(
187+
message, self, response) from response
181188
raise ModbusClientFailedError(message, self, response)
182189

183190
# We've seen cases where the remote device gets two requests at the same time and sends the wrong response to
184191
# the wrong thing. pymodbus doesn't check whether the response type matches the request type
185192
if not isinstance(response, expected_response_type):
186193
message = (
187-
f"Error writing registers. Start: {register_address}; values: {register_values}; slave: {slave}. "
188-
f"Received incorrect response type {response}. Please ensure that your adapter is correctly "
194+
f"Error writing registers. Start: {register_address}; values: {
195+
register_values}; slave: {slave}. "
196+
f"Received incorrect response type {
197+
response}. Please ensure that your adapter is correctly "
189198
"configured to allow multiple connections, see the instructions at "
190199
"https://github.com/nathanmarlor/foxess_modbus/wiki"
191200
)

custom_components/foxess_modbus/client/protocol_pollserial.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def read(self, size: int = 1) -> bytes:
6868
result == _PollResult.TIMEOUT
6969
or result == _PollResult.ABORT
7070
or timeout.expired()
71-
or ((self._inter_byte_timeout is not None and self._inter_byte_timeout > 0)
72-
and not buf)
71+
or ((self._inter_byte_timeout is not None and self._inter_byte_timeout > 0) and not buf)
7372
):
7473
break # early abort on timeout
7574
return bytes(read)

custom_components/foxess_modbus/common/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
""""Unsupported inverter exception"""
1+
""" "Unsupported inverter exception"""
2+
23
import logging
34

45

custom_components/foxess_modbus/common/unload_controller.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Unload controller"""
2+
23
import logging
34
from typing import Callable
45

custom_components/foxess_modbus/config_flow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Adds config flow for foxess_modbus."""
2+
23
import logging
34

45
from .const import DOMAIN

custom_components/foxess_modbus/entities/base_validator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Validation"""
2+
23
from abc import ABC
34
from abc import abstractmethod
45

custom_components/foxess_modbus/entities/entity_factory.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def _address_for_inverter_model(
8787
if addresses is not None:
8888
assert len(addresses) == 1, f"{self}: != 1 addresses defined for ({inverter_model}, {register_type})"
8989
# We shouldn't get more than one spec which matches
90-
assert (
91-
result is None
92-
), f"{self}: more than one address spec defined for ({inverter_model}, {register_type})"
90+
assert result is None, (
91+
f"{self}: more than one address spec defined for ({inverter_model}, {register_type})"
92+
)
9393
result = addresses[0]
9494
return result
9595

@@ -111,9 +111,9 @@ def _addresses_for_inverter_model(
111111
addresses = spec.addresses_for_inverter_model(register_type=register_type, models=inverter_model)
112112
if addresses is not None:
113113
# We shouldn't get more than one spec which matches
114-
assert (
115-
result is None
116-
), f"{self}: more than one address spec defined for ({inverter_model}, {register_type})"
114+
assert result is None, (
115+
f"{self}: more than one address spec defined for ({inverter_model}, {register_type})"
116+
)
117117
result = addresses
118118
return result
119119

custom_components/foxess_modbus/entities/modbus_charge_period_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ def create_charge_period_config_if_supported(
178178
if inverter_model in address_spec.models:
179179
address_config = address_spec.register_types.get(register_type)
180180
if address_config is not None:
181-
assert (
182-
result is None
183-
), f"{self}: multiple charge periods defined for ({inverter_model}, {register_type})"
181+
assert result is None, (
182+
f"{self}: multiple charge periods defined for ({inverter_model}, {register_type})"
183+
)
184184

185185
start_id = get_entity_id(controller, Platform.SENSOR, self._period_start_key)
186186
end_id = get_entity_id(controller, Platform.SENSOR, self._period_end_key)

0 commit comments

Comments
 (0)