Skip to content

Commit 5e58ac7

Browse files
authored
Finish enabling ruff rules (#327)
1 parent e98909b commit 5e58ac7

19 files changed

+80
-57
lines changed

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ ignore = [
2828
"UP038", # Use `X | Y` in `isinstance` is slower
2929
"S603", # check for execution of untrusted input
3030
"S105", # possible hard coded creds
31+
"TID252", # not for this lib
32+
"TRY003", # nice but too many to fix,
33+
"G201", # too noisy
34+
"PLR2004", # too many to fix
3135
]
3236
select = [
37+
"ASYNC", # async rules
3338
"B", # flake8-bugbear
3439
"D", # flake8-docstrings
3540
"C4", # flake8-comprehensions
@@ -40,6 +45,24 @@ select = [
4045
"UP", # pyupgrade
4146
"I", # isort
4247
"RUF", # ruff specific
48+
"FLY", # flynt
49+
"G", # flake8-logging-format ,
50+
"PERF", # Perflint
51+
"PGH", # pygrep-hooks
52+
"PIE", # flake8-pie
53+
"PL", # pylint
54+
"PT", # flake8-pytest-style
55+
"PTH", # flake8-pathlib
56+
"PYI", # flake8-pyi
57+
"RET", # flake8-return
58+
"RSE", # flake8-raise ,
59+
"SIM", # flake8-simplify
60+
"SLF", # flake8-self
61+
"SLOT", # flake8-slots
62+
"T100", # Trace found: {name} used
63+
"T20", # flake8-print
64+
"TID", # Tidy imports
65+
"TRY", # tryceratops
4366
]
4467

4568
[tool.ruff.lint.per-file-ignores]
@@ -50,10 +73,15 @@ select = [
5073
"D103",
5174
"D104",
5275
"S101",
76+
"SLF001",
77+
"PLR2004",
5378
]
5479
"setup.py" = ["D100"]
5580
"conftest.py" = ["D100"]
5681
"docs/conf.py" = ["D100"]
82+
"scripts/**/*" = [
83+
"T201"
84+
]
5785

5886
[tool.ruff.lint.isort]
5987
known-first-party = ["pySwitchbot", "tests"]

scripts/get_encryption_key.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@
88
def main():
99
if len(sys.argv) < 3:
1010
print(f"Usage: {sys.argv[0]} <device_mac> <username> [<password>]")
11-
exit(1)
11+
sys.exit(1)
1212

13-
if len(sys.argv) == 3:
14-
password = getpass.getpass()
15-
else:
16-
password = sys.argv[3]
13+
password = getpass.getpass() if len(sys.argv) == 3 else sys.argv[3]
1714

1815
try:
1916
result = SwitchbotLock.retrieve_encryption_key(
2017
sys.argv[1], sys.argv[2], password
2118
)
2219
except RuntimeError as e:
2320
print(e)
24-
exit(1)
21+
sys.exit(1)
2522

2623
print("Key ID: " + result["key_id"])
2724
print("Encryption key: " + result["encryption_key"])

switchbot/adv_parser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,8 @@ def parse_advertisement_data(
285285
_mfr_id,
286286
model,
287287
)
288-
except Exception as err: # pylint: disable=broad-except
289-
_LOGGER.exception(
290-
"Failed to parse advertisement data: %s: %s", advertisement_data, err
291-
)
288+
except Exception: # pylint: disable=broad-except
289+
_LOGGER.exception("Failed to parse advertisement data: %s", advertisement_data)
292290
return None
293291

294292
if not data:

switchbot/adv_parsers/hub2.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]
3030
if _temp_c == 0 and humidity == 0:
3131
return {}
3232

33-
_wohub2_data = {
33+
return {
3434
# Data should be flat, but we keep the original structure for now
3535
"temp": {"c": _temp_c, "f": _temp_f},
3636
"temperature": _temp_c,
@@ -40,8 +40,6 @@ def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]
4040
"illuminance": calculate_light_intensity(light_level),
4141
}
4242

43-
return _wohub2_data
44-
4543

4644
def calculate_light_intensity(light_level: int) -> int:
4745
"""

switchbot/adv_parsers/hubmini_matter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ def process_hubmini_matter(
2828
if _temp_c == 0 and humidity == 0:
2929
return {}
3030

31-
paraser_data = {
31+
return {
3232
"temp": {"c": _temp_c, "f": _temp_f},
3333
"temperature": _temp_c,
3434
"fahrenheit": bool(temp_data[2] & 0b10000000),
3535
"humidity": humidity,
3636
}
37-
return paraser_data

switchbot/adv_parsers/meter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str,
3535
if _temp_c == 0 and humidity == 0 and battery == 0:
3636
return {}
3737

38-
_wosensorth_data = {
38+
return {
3939
# Data should be flat, but we keep the original structure for now
4040
"temp": {"c": _temp_c, "f": _temp_f},
4141
"temperature": _temp_c,
@@ -44,8 +44,6 @@ def process_wosensorth(data: bytes | None, mfr_data: bytes | None) -> dict[str,
4444
"battery": battery,
4545
}
4646

47-
return _wosensorth_data
48-
4947

5048
def process_wosensorth_c(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
5149
"""Process woSensorTH/Temp sensor services data with CO2."""

switchbot/const/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from __future__ import annotations
44

55
from ..enum import StrEnum
6-
from .fan import FanMode as FanMode
6+
from .fan import FanMode
77

88
# Preserve old LockStatus export for backwards compatibility
9-
from .lock import LockStatus as LockStatus
9+
from .lock import LockStatus
1010

1111
DEFAULT_RETRY_COUNT = 3
1212
DEFAULT_RETRY_TIMEOUT = 1
@@ -67,3 +67,16 @@ class SwitchbotModel(StrEnum):
6767
ROLLER_SHADE = "Roller Shade"
6868
HUBMINI_MATTER = "HubMini Matter"
6969
CIRCULATOR_FAN = "Circulator Fan"
70+
71+
72+
__all__ = [
73+
"DEFAULT_RETRY_COUNT",
74+
"DEFAULT_RETRY_TIMEOUT",
75+
"DEFAULT_SCAN_TIMEOUT",
76+
"FanMode",
77+
"LockStatus",
78+
"SwitchbotAccountConnectionError",
79+
"SwitchbotApiError",
80+
"SwitchbotAuthenticationError",
81+
"SwitchbotModel",
82+
]

switchbot/devices/blind_tilt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ async def close(self) -> bool:
100100
"""Send close command."""
101101
if self.get_position() > 50:
102102
return await self.close_up()
103-
else:
104-
return await self.close_down()
103+
return await self.close_down()
105104

106105
def get_position(self) -> Any:
107106
"""Return cached tilt (0-100) of Blind Tilt."""

switchbot/devices/device.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def _update_parsed_data(self, new_data: dict[str, Any]) -> bool:
646646
"""
647647
if not self._sb_adv_data:
648648
_LOGGER.exception("No advertisement data to update")
649-
return
649+
return None
650650
old_data = self._sb_adv_data.data.get("data") or {}
651651
merged_data = _merge_data(old_data, new_data)
652652
if merged_data == old_data:
@@ -688,9 +688,7 @@ def poll_needed(self, seconds_since_last_poll: float | None) -> bool:
688688
):
689689
return False
690690
time_since_last_full_update = time.monotonic() - self._last_full_update
691-
if time_since_last_full_update < PASSIVE_POLL_INTERVAL:
692-
return False
693-
return True
691+
return not time_since_last_full_update < PASSIVE_POLL_INTERVAL
694692

695693

696694
class SwitchbotDevice(SwitchbotBaseDevice):
@@ -723,11 +721,11 @@ def __init__(
723721
"""Switchbot base class constructor for encrypted devices."""
724722
if len(key_id) == 0:
725723
raise ValueError("key_id is missing")
726-
elif len(key_id) != 2:
724+
if len(key_id) != 2:
727725
raise ValueError("key_id is invalid")
728726
if len(encryption_key) == 0:
729727
raise ValueError("encryption_key is missing")
730-
elif len(encryption_key) != 32:
728+
if len(encryption_key) != 32:
731729
raise ValueError("encryption_key is invalid")
732730
self._key_id = key_id
733731
self._encryption_key = bytearray.fromhex(encryption_key)

switchbot/devices/evaporative_humidifier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def set_mode(
117117
"""Set device mode."""
118118
if mode == HumidifierMode.DRYING_FILTER:
119119
return await self.start_drying_filter()
120-
elif mode not in MODES_COMMANDS:
120+
if mode not in MODES_COMMANDS:
121121
raise ValueError("Invalid mode")
122122

123123
command = COMMAND_SET_MODE + MODES_COMMANDS[mode]

0 commit comments

Comments
 (0)