From 34f0485d91d808c206a45dedab125f1985c89a85 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/protocol_pollserial.py | 3 +-- custom_components/foxess_modbus/common/exceptions.py | 3 ++- .../foxess_modbus/common/unload_controller.py | 1 + custom_components/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 + 12 files changed, 28 insertions(+), 23 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/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