Skip to content

Commit

Permalink
Enable new ruff rules (#2514)
Browse files Browse the repository at this point in the history
* feat: enable more ruff rules

* feat: enable more ruff rules

* feat: enable more ruff rules

* fix: sort rules
  • Loading branch information
bramstroker authored Sep 21, 2024
1 parent 73135bb commit 3caed7e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion custom_components/powercalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ async def repair_none_config_entries_issue(hass: HomeAssistant) -> None:
for entity in entities:
entity_registry.async_remove(entity.entity_id)
try:
unique_id = f"{int(time.time() * 1000)}_{random.randint(1000, 9999)}"
unique_id = f"{int(time.time() * 1000)}_{random.randint(1000, 9999)}" # noqa: S311
object.__setattr__(entry, "unique_id", unique_id)
hass.config_entries._entries._index_entry(entry) # noqa
await hass.config_entries.async_remove(entry.entry_id)
Expand Down
17 changes: 9 additions & 8 deletions custom_components/powercalc/power_profile/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ async def factory(hass: HomeAssistant) -> ProfileLibrary:

@staticmethod
def create_loader(hass: HomeAssistant) -> Loader:
loaders: list[Loader] = []
for data_dir in [
os.path.join(hass.config.config_dir, LEGACY_CUSTOM_DATA_DIRECTORY),
os.path.join(hass.config.config_dir, CUSTOM_DATA_DIRECTORY),
os.path.join(os.path.dirname(__file__), "../custom_data"),
]:
if os.path.exists(data_dir):
loaders.append(LocalLoader(hass, data_dir))
loaders: list[Loader] = [
LocalLoader(hass, data_dir)
for data_dir in [
os.path.join(hass.config.config_dir, LEGACY_CUSTOM_DATA_DIRECTORY),
os.path.join(hass.config.config_dir, CUSTOM_DATA_DIRECTORY),
os.path.join(os.path.dirname(__file__), "../custom_data"),
]
if os.path.exists(data_dir)
]

global_config = hass.data[DOMAIN].get(DOMAIN_CONFIG, {})
disable_library_download: bool = bool(global_config.get(CONF_DISABLE_LIBRARY_DOWNLOAD, False))
Expand Down
6 changes: 2 additions & 4 deletions custom_components/powercalc/power_profile/power_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ def __init__(
self._matchers: list[SubProfileMatcher] = self._build_matchers()

def _build_matchers(self) -> list[SubProfileMatcher]:
matchers: list[SubProfileMatcher] = []
for matcher_config in self._config.matchers:
matchers.append(self._create_matcher(matcher_config))
return matchers
"""Create matchers from json config."""
return [self._create_matcher(matcher_config) for matcher_config in self._config.matchers]

def select_sub_profile(self, entity_state: State) -> str:
"""Dynamically tries to select a sub profile depending on the entity state.
Expand Down
3 changes: 2 additions & 1 deletion custom_components/powercalc/strategy/fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async def validate_config(self) -> None:
)

def get_entities_to_track(self) -> list[str | TrackTemplate]:
"""Return entities that should be tracked."""
track_templates: list[str | TrackTemplate] = []

if isinstance(self._power, Template):
Expand All @@ -85,6 +86,6 @@ def get_entities_to_track(self) -> list[str | TrackTemplate]:
if self._per_state_power:
for power in list(self._per_state_power.values()):
if isinstance(power, Template):
track_templates.append(TrackTemplate(power, None, None))
track_templates.append(TrackTemplate(power, None, None)) # noqa: PERF401

return track_templates
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ target-version = "py312"
exclude = ["utils/measure/playground", "utils/visualize", ".github", "docs"]

[tool.ruff.lint]
select = ["E", "W", "F", "B", "C", "I", "N", "UP", "ANN", "ASYNC", "BLE", "A", "COM", "C4", "G", "ISC", "NPY", "INP", "PIE", "T20", "PYI", "Q", "RET", "SLF", "SIM", "TID", "RUF", "SIM"]
ignore = ["ANN101"]
select = ["A", "ANN", "ASYNC", "B", "BLE", "C", "C4", "COM", "E", "F", "FIX", "FURB", "G", "ICN", "I", "INP", "ISC", "LOG", "N", "NPY", "PERF", "PIE", "PYI", "Q", "RET", "RUF", "S", "SIM", "SLF", "SLOT", "T10", "T20", "TID", "UP", "W"]
ignore = ["ANN101", "S101"]

[tool.ruff.lint.per-file-ignores]
"utils/measure/**.py" = ["T201"]
Expand Down
25 changes: 12 additions & 13 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,19 @@ async def test_repair_issue_with_none_sensors(hass: HomeAssistant) -> None:
"""
power_entry = await create_mocked_virtual_power_sensor_entry(hass, "Power")

none_entries = []
for _ in range(10):
none_entries.append(
await setup_config_entry(
hass,
{
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "None",
CONF_GROUP_MEMBER_SENSORS: [power_entry.entry_id],
},
"None",
"None",
),
none_entries = [
await setup_config_entry(
hass,
{
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "None",
CONF_GROUP_MEMBER_SENSORS: [power_entry.entry_id],
},
"None",
"None",
)
for _ in range(10)
]

for entry in none_entries:
assert hass.config_entries.async_get_entry(entry.entry_id)
Expand Down
14 changes: 7 additions & 7 deletions utils/measure/ocr/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from threading import Thread

import cv2
import numpy
import numpy as np
import pytesseract

logging.basicConfig(
Expand Down Expand Up @@ -87,7 +87,7 @@ def rate(self) -> float:
elapsed_time = time.perf_counter() - self.start_time
return self.iterations / elapsed_time

def render(self, frame: numpy.ndarray, rate: float) -> numpy.ndarray:
def render(self, frame: np.ndarray, rate: float) -> np.ndarray:
"""
Places text showing the iterations per second in the CV2 display loop.
Expand Down Expand Up @@ -156,7 +156,7 @@ def stop_process(self) -> None:

def capture_image(
self,
frame: numpy.ndarray | None = None,
frame: np.ndarray | None = None,
captures: int = 0,
) -> int:
"""
Expand Down Expand Up @@ -204,7 +204,7 @@ def register_mouse_callback(self) -> None:

# Method to track mouse events
def draw_rectangle(self, event: int, x: int, y: int) -> None:
x, y = numpy.int16([x, y])
x, y = np.int16([x, y])

if event == cv2.EVENT_LBUTTONDOWN:
# Start selection
Expand All @@ -222,7 +222,7 @@ def draw_rectangle(self, event: int, x: int, y: int) -> None:
x_start, y_start = self.drag_start
self.selection = (x_start, y_start, x, y)

def render(self, frame: numpy.ndarray) -> numpy.ndarray:
def render(self, frame: np.ndarray) -> np.ndarray:
if self.selection:
cv2.rectangle(
frame,
Expand Down Expand Up @@ -262,7 +262,7 @@ def render(self, frame: numpy.ndarray) -> numpy.ndarray:
def has_selection(self) -> bool:
return self.selection is not None

def get_cropped_frame(self, frame: numpy.ndarray) -> numpy.ndarray:
def get_cropped_frame(self, frame: np.ndarray) -> np.ndarray:
return frame[
self.selection[1] : self.selection[3],
self.selection[0] : self.selection[2],
Expand Down Expand Up @@ -342,7 +342,7 @@ def stop_process(self) -> None:
self.file.close()
self.stopped = True

def render(self, frame: numpy.ndarray) -> numpy.ndarray:
def render(self, frame: np.ndarray) -> np.ndarray:
if self.measurement is None:
return frame
return cv2.putText(
Expand Down
4 changes: 1 addition & 3 deletions utils/measure/runner/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def run(

@staticmethod
def _build_model_json_data(summary: dict) -> dict:
calibrate_list = []
for volume in summary:
calibrate_list.append(f"{volume} -> {summary[volume]}")
calibrate_list = [f"{volume} -> {summary[volume]}" for volume in summary]

return {
"device_type": "smart_speaker",
Expand Down

0 comments on commit 3caed7e

Please sign in to comment.