Skip to content

Commit

Permalink
Use ruff as a code formatter (#500)
Browse files Browse the repository at this point in the history
* Use ruff as a code formatter

* Improve wording
  • Loading branch information
bieniu committed Nov 5, 2023
1 parent 208bd43 commit bb4bc8e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ jobs:
run: |
ruff python_scripts
- name: Check with Black Code Formatter
- name: Check the formatting with ruff
run: |
black python_scripts --check
ruff format --check python_scripts
14 changes: 3 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.3
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.4
hooks:
- id: ruff
args:
- --fix
files: ^((python_scripts)/.+)?[^/]+\.py$
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
args:
- --safe
- --quiet
- id: ruff-format
files: ^((python_scripts)/.+)?[^/]+\.py$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand Down
23 changes: 7 additions & 16 deletions python_scripts/shellies_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,8 @@ def mqtt_publish(topic, payload, retain, json=False):
roller_mode = True

ignored = [
element.lower() for element in data.get(CONF_IGNORED_DEVICES, []) # noqa: F821
element.lower()
for element in data.get(CONF_IGNORED_DEVICES, []) # noqa: F821
]
mac = data.get(CONF_MAC) # noqa: F821

Expand Down Expand Up @@ -2838,9 +2839,7 @@ def mqtt_publish(topic, payload, retain, json=False):
for sensor, sensor_options in relay_binary_sensors.items():
config_topic = f"{disc_prefix}/binary_sensor/{dev_id}-{make_id(sensor)}-{relay_id}/config".encode(
"ascii", "ignore"
).decode(
"utf-8"
)
).decode("utf-8")
if device_config.get(f"relay-{relay_id}-name"):
sensor_name = f"{device_config[f'relay-{relay_id}-name']} {format_entity_name(sensor)}"
else:
Expand Down Expand Up @@ -2982,9 +2981,7 @@ def mqtt_publish(topic, payload, retain, json=False):
for input_id in range(inputs):
config_topic = f"{disc_prefix}/device_automation/{dev_id}-input-{input_id}/button_release/config".encode(
"ascii", "ignore"
).decode(
"utf-8"
)
).decode("utf-8")
topic = f"shellies/{dev_id}/input/{input_id}"
payload = {
KEY_AUTOMATION_TYPE: VALUE_TRIGGER,
Expand All @@ -3004,9 +3001,7 @@ def mqtt_publish(topic, payload, retain, json=False):
for event in inputs_types:
config_topic = f"{disc_prefix}/device_automation/{dev_id}-input-{input_id}/{event}/config".encode(
"ascii", "ignore"
).decode(
"utf-8"
)
).decode("utf-8")
payload = {
KEY_AUTOMATION_TYPE: VALUE_TRIGGER,
KEY_TOPIC: topic,
Expand Down Expand Up @@ -3239,9 +3234,7 @@ def mqtt_publish(topic, payload, retain, json=False):
for sensor, sensor_options in light_binary_sensors.items():
config_topic = f"{disc_prefix}/binary_sensor/{dev_id}-color-{sensor}-{light_id}/config".encode(
"ascii", "ignore"
).decode(
"utf-8"
)
).decode("utf-8")
if mode == LIGHT_COLOR:
payload = {
KEY_NAME: f"{format_entity_name(sensor)} {light_id}",
Expand Down Expand Up @@ -3370,9 +3363,7 @@ def mqtt_publish(topic, payload, retain, json=False):
for sensor, sensor_options in light_binary_sensors.items():
config_topic = f"{disc_prefix}/binary_sensor/{dev_id}-white-{sensor}-{light_id}/config".encode(
"ascii", "ignore"
).decode(
"utf-8"
)
).decode("utf-8")
if mode != LIGHT_COLOR:
payload = {
KEY_NAME: f"{format_entity_name(sensor)} {light_id}",
Expand Down
3 changes: 1 addition & 2 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
black==23.10.1
ruff==0.1.3
ruff==0.1.4
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ignore = [
"FBT002", # Boolean default value in function definition
"FBT003", # Boolean positional value in function call
"INP001", # Add an `__init__.py`
"ISC001", # Single line implicit string concatenation
"TRY003", # Avoid specifying long messages outside the exception class
]

Expand Down

0 comments on commit bb4bc8e

Please sign in to comment.