From 7682419f0310b55e5b7eabe9a1568f12da8ac21a Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 1 Feb 2025 21:59:26 +0000 Subject: [PATCH] Properly set up ruff as the formatter --- .pre-commit-config.yaml | 2 ++ .vscode/settings.json | 3 +-- .../foxess_modbus/client/modbus_client.py | 27 ++++++++++++------- .../client/protocol_pollserial.py | 3 +-- .../foxess_modbus/common/exceptions.py | 3 ++- .../foxess_modbus/common/unload_controller.py | 1 + .../foxess_modbus/config_flow.py | 1 + .../foxess_modbus/entities/base_validator.py | 1 + .../foxess_modbus/entities/entity_factory.py | 12 ++++----- .../entities/modbus_charge_period_config.py | 6 ++--- .../entities/modbus_charge_period_sensors.py | 12 ++++----- .../entities/modbus_remote_control_config.py | 6 ++--- .../foxess_modbus/entities/validation.py | 1 + 13 files changed, 46 insertions(+), 32 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e31b88b9..02a48c2e 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,6 +16,8 @@ repos: rev: v0.9.4 hooks: - id: ruff + - id: ruff-format + args: [--check] # Temporarily disabled due to crash, probably caused by us having to use HA stubs which are out of date? # See https://github.com/nathanmarlor/foxess_modbus/actions/runs/12610946302/job/35146070831?pr=720 # - repo: https://github.com/pre-commit/mirrors-mypy diff --git a/.vscode/settings.json b/.vscode/settings.json index 654bd74a..59db7c07 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,8 +25,7 @@ "[python]": { "editor.codeActionsOnSave": { "source.fixAll": "explicit" - }, - "editor.defaultFormatter": "charliermarsh.ruff" + } }, "python.analysis.extraPaths": [ "./custom_components/foxess_modbus/vendor/pymodbus/pymodbus-3.6.9" diff --git a/custom_components/foxess_modbus/client/modbus_client.py b/custom_components/foxess_modbus/client/modbus_client.py index dc506e69..7176e5a1 100644 --- a/custom_components/foxess_modbus/client/modbus_client.py +++ b/custom_components/foxess_modbus/client/modbus_client.py @@ -88,7 +88,8 @@ def __init__(self, hass: HomeAssistant, protocol: str, adapter: InverterAdapter, # Some serial devices need a short delay after polling. Also do this for the inverter, just # in case it helps. - self._poll_delay = 30 / 1000 if protocol == SERIAL or adapter.connection_type == ConnectionType.LAN else 0 + self._poll_delay = 30 / \ + 1000 if protocol == SERIAL or adapter.connection_type == ConnectionType.LAN else 0 self._client = client["client"](**config) @@ -127,19 +128,23 @@ async def read_registers( if response.isError(): message = ( - f"Error reading registers. Type: {register_type}; start: {start_address}; count: {num_registers}; " + f"Error reading registers. Type: {register_type}; start: { + start_address}; count: {num_registers}; " f"slave: {slave}" ) if isinstance(response, Exception): - raise ModbusClientFailedError(message, self, response) from response + raise ModbusClientFailedError( + message, self, response) from response raise ModbusClientFailedError(message, self, response) # We've seen cases where the remote device gets two requests at the same time and sends the wrong response to # the wrong thing. pymodbus doesn't check whether the response type matches the request type if not isinstance(response, expected_response_type): message = ( - f"Error reading registers. Type: {register_type}; start: {start_address}; count: {num_registers}; " - f"slave: {slave}. Received incorrect response type {response}. Please ensure that your adapter is " + f"Error reading registers. Type: {register_type}; start: { + start_address}; count: {num_registers}; " + f"slave: {slave}. Received incorrect response type { + response}. Please ensure that your adapter is " "correctly configured to allow multiple connections, see the instructions at " "https://github.com/nathanmarlor/foxess_modbus/wiki" ) @@ -175,17 +180,21 @@ async def write_registers(self, register_address: int, register_values: list[int expected_response_type = WriteSingleRegisterResponse if response.isError(): - message = f"Error writing registers. Start: {register_address}; values: {register_values}; slave: {slave}" + message = f"Error writing registers. Start: { + register_address}; values: {register_values}; slave: {slave}" if isinstance(response, Exception): - raise ModbusClientFailedError(message, self, response) from response + raise ModbusClientFailedError( + message, self, response) from response raise ModbusClientFailedError(message, self, response) # We've seen cases where the remote device gets two requests at the same time and sends the wrong response to # the wrong thing. pymodbus doesn't check whether the response type matches the request type if not isinstance(response, expected_response_type): message = ( - f"Error writing registers. Start: {register_address}; values: {register_values}; slave: {slave}. " - f"Received incorrect response type {response}. Please ensure that your adapter is correctly " + f"Error writing registers. Start: {register_address}; values: { + register_values}; slave: {slave}. " + f"Received incorrect response type { + response}. Please ensure that your adapter is correctly " "configured to allow multiple connections, see the instructions at " "https://github.com/nathanmarlor/foxess_modbus/wiki" ) diff --git a/custom_components/foxess_modbus/client/protocol_pollserial.py b/custom_components/foxess_modbus/client/protocol_pollserial.py index 44addd8d..38c75b3f 100644 --- a/custom_components/foxess_modbus/client/protocol_pollserial.py +++ b/custom_components/foxess_modbus/client/protocol_pollserial.py @@ -68,8 +68,7 @@ def read(self, size: int = 1) -> bytes: result == _PollResult.TIMEOUT or result == _PollResult.ABORT or timeout.expired() - or ((self._inter_byte_timeout is not None and self._inter_byte_timeout > 0) - and not buf) + or ((self._inter_byte_timeout is not None and self._inter_byte_timeout > 0) and not buf) ): break # early abort on timeout return bytes(read) diff --git a/custom_components/foxess_modbus/common/exceptions.py b/custom_components/foxess_modbus/common/exceptions.py index 04bd39a3..c92e08bf 100644 --- a/custom_components/foxess_modbus/common/exceptions.py +++ b/custom_components/foxess_modbus/common/exceptions.py @@ -1,4 +1,5 @@ -""""Unsupported inverter exception""" +""" "Unsupported inverter exception""" + import logging diff --git a/custom_components/foxess_modbus/common/unload_controller.py b/custom_components/foxess_modbus/common/unload_controller.py index 4f781fdc..c1173412 100755 --- a/custom_components/foxess_modbus/common/unload_controller.py +++ b/custom_components/foxess_modbus/common/unload_controller.py @@ -1,4 +1,5 @@ """Unload controller""" + import logging from typing import Callable diff --git a/custom_components/foxess_modbus/config_flow.py b/custom_components/foxess_modbus/config_flow.py index ceb0be2c..60943326 100755 --- a/custom_components/foxess_modbus/config_flow.py +++ b/custom_components/foxess_modbus/config_flow.py @@ -1,4 +1,5 @@ """Adds config flow for foxess_modbus.""" + import logging from .const import DOMAIN diff --git a/custom_components/foxess_modbus/entities/base_validator.py b/custom_components/foxess_modbus/entities/base_validator.py index c992a2ba..97e85c63 100644 --- a/custom_components/foxess_modbus/entities/base_validator.py +++ b/custom_components/foxess_modbus/entities/base_validator.py @@ -1,4 +1,5 @@ """Validation""" + from abc import ABC from abc import abstractmethod diff --git a/custom_components/foxess_modbus/entities/entity_factory.py b/custom_components/foxess_modbus/entities/entity_factory.py index 3ebe3e46..54780642 100644 --- a/custom_components/foxess_modbus/entities/entity_factory.py +++ b/custom_components/foxess_modbus/entities/entity_factory.py @@ -87,9 +87,9 @@ def _address_for_inverter_model( if addresses is not None: assert len(addresses) == 1, f"{self}: != 1 addresses defined for ({inverter_model}, {register_type})" # We shouldn't get more than one spec which matches - assert ( - result is None - ), f"{self}: more than one address spec defined for ({inverter_model}, {register_type})" + assert result is None, ( + f"{self}: more than one address spec defined for ({inverter_model}, {register_type})" + ) result = addresses[0] return result @@ -111,9 +111,9 @@ def _addresses_for_inverter_model( addresses = spec.addresses_for_inverter_model(register_type=register_type, models=inverter_model) if addresses is not None: # We shouldn't get more than one spec which matches - assert ( - result is None - ), f"{self}: more than one address spec defined for ({inverter_model}, {register_type})" + assert result is None, ( + f"{self}: more than one address spec defined for ({inverter_model}, {register_type})" + ) result = addresses return result diff --git a/custom_components/foxess_modbus/entities/modbus_charge_period_config.py b/custom_components/foxess_modbus/entities/modbus_charge_period_config.py index 43fd0e86..109ae549 100644 --- a/custom_components/foxess_modbus/entities/modbus_charge_period_config.py +++ b/custom_components/foxess_modbus/entities/modbus_charge_period_config.py @@ -178,9 +178,9 @@ def create_charge_period_config_if_supported( if inverter_model in address_spec.models: address_config = address_spec.register_types.get(register_type) if address_config is not None: - assert ( - result is None - ), f"{self}: multiple charge periods defined for ({inverter_model}, {register_type})" + assert result is None, ( + f"{self}: multiple charge periods defined for ({inverter_model}, {register_type})" + ) start_id = get_entity_id(controller, Platform.SENSOR, self._period_start_key) end_id = get_entity_id(controller, Platform.SENSOR, self._period_end_key) diff --git a/custom_components/foxess_modbus/entities/modbus_charge_period_sensors.py b/custom_components/foxess_modbus/entities/modbus_charge_period_sensors.py index ca440cba..36d50da9 100644 --- a/custom_components/foxess_modbus/entities/modbus_charge_period_sensors.py +++ b/custom_components/foxess_modbus/entities/modbus_charge_period_sensors.py @@ -77,14 +77,14 @@ def create_entity_if_supported( other_address = self._address_for_inverter_model(self.other_address, inverter_model, register_type) if address is None: - assert ( - other_address is None - ), f"{self}: address is None but other_address is {other_address} for ({inverter_model}, {register_type})" + assert other_address is None, ( + f"{self}: address is None but other_address is {other_address} for ({inverter_model}, {register_type})" + ) return None - assert ( - other_address is not None - ), f"{self}: address is {address} but other_address is None for ({inverter_model}, {register_type})" + assert other_address is not None, ( + f"{self}: address is {address} but other_address is None for ({inverter_model}, {register_type})" + ) return ModbusChargePeriodStartEndSensor(controller, self, address, other_address) def serialize(self, inverter_model: Inv) -> dict[str, Any] | None: diff --git a/custom_components/foxess_modbus/entities/modbus_remote_control_config.py b/custom_components/foxess_modbus/entities/modbus_remote_control_config.py index bcb7039e..ed16aef9 100644 --- a/custom_components/foxess_modbus/entities/modbus_remote_control_config.py +++ b/custom_components/foxess_modbus/entities/modbus_remote_control_config.py @@ -202,9 +202,9 @@ def create_if_supported( if inverter_model in address_spec.models: address_config = address_spec.register_types.get(register_type) if address_config is not None: - assert ( - result is None - ), f"{self}: multiple remote control addresses defined for ({inverter_model}, {register_type})" + assert result is None, ( + f"{self}: multiple remote control addresses defined for ({inverter_model}, {register_type})" + ) result = address_config return result diff --git a/custom_components/foxess_modbus/entities/validation.py b/custom_components/foxess_modbus/entities/validation.py index 2fa522f6..59f583c9 100755 --- a/custom_components/foxess_modbus/entities/validation.py +++ b/custom_components/foxess_modbus/entities/validation.py @@ -1,4 +1,5 @@ """Validation""" + from .base_validator import BaseValidator from .modbus_charge_period_sensors import is_time_value_valid