Skip to content

Commit

Permalink
Properly set up ruff as the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Feb 1, 2025
1 parent 7a88ed4 commit 7682419
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 18 additions & 9 deletions custom_components/foxess_modbus/client/modbus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/foxess_modbus/common/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""""Unsupported inverter exception"""
""" "Unsupported inverter exception"""

import logging


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Unload controller"""

import logging
from typing import Callable

Expand Down
1 change: 1 addition & 0 deletions custom_components/foxess_modbus/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adds config flow for foxess_modbus."""

import logging

from .const import DOMAIN
Expand Down
1 change: 1 addition & 0 deletions custom_components/foxess_modbus/entities/base_validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Validation"""

from abc import ABC
from abc import abstractmethod

Expand Down
12 changes: 6 additions & 6 deletions custom_components/foxess_modbus/entities/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions custom_components/foxess_modbus/entities/validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Validation"""

from .base_validator import BaseValidator
from .modbus_charge_period_sensors import is_time_value_valid

Expand Down

0 comments on commit 7682419

Please sign in to comment.