Skip to content

Commit

Permalink
Merge pull request #1 from NitorCreations/ruff
Browse files Browse the repository at this point in the history
Ruff
  • Loading branch information
Jalle19 authored Sep 6, 2024
2 parents 9ac54a5 + 8d96a84 commit 41a4732
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Ruff
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
8 changes: 4 additions & 4 deletions custom_components/extron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, DOMAIN
from homeassistant.core import DOMAIN, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import format_mac, DeviceInfo
from homeassistant.helpers.device_registry import DeviceInfo, format_mac

from custom_components.extron.extron import ExtronDevice, DeviceType, AuthenticationFailed
from custom_components.extron.extron import AuthenticationError, ExtronDevice

PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.SENSOR, Platform.BUTTON]

Expand Down Expand Up @@ -52,7 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try:
device = ExtronDevice(entry.data['host'], entry.data['port'], entry.data['password'])
await device.connect()
except AuthenticationFailed as e:
except AuthenticationError as e:
raise ConfigEntryNotReady('Invalid credentials') from e
except Exception as e:
raise ConfigEntryNotReady('Unable to connect') from e
Expand Down
4 changes: 2 additions & 2 deletions custom_components/extron/button.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging

from homeassistant.components.button import ButtonEntity, ButtonDeviceClass
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceInfo

from custom_components.extron import ExtronDevice, DeviceInformation, ExtronConfigEntryRuntimeData
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData, ExtronDevice

logger = logging.getLogger(__name__)

Expand Down
9 changes: 5 additions & 4 deletions custom_components/extron/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
from __future__ import annotations

import logging

from typing import Any

import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.helpers.selector import selector

from .const import DOMAIN, CONF_HOST, CONF_PORT, CONF_PASSWORD, CONF_DEVICE_TYPE
from .extron import DeviceType, ExtronDevice, AuthenticationFailed
from .const import CONF_DEVICE_TYPE, CONF_HOST, CONF_PASSWORD, CONF_PORT, DOMAIN
from .extron import AuthenticationError, DeviceType, ExtronDevice

_LOGGER = logging.getLogger(__name__)

Expand All @@ -37,7 +38,7 @@ class ExtronConfigFlow(ConfigFlow, domain=DOMAIN):

VERSION = 1

async def async_step_user(self, user_input: dict[str, Any] | None = None):
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
Expand All @@ -52,7 +53,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None):

# Disconnect, we'll connect again later, this was just for validation
await extron_device.disconnect()
except AuthenticationFailed:
except AuthenticationError:
errors["base"] = "invalid_auth"
except Exception:
errors["base"] = "cannot_connect"
Expand Down
10 changes: 5 additions & 5 deletions custom_components/extron/extron.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
import logging

from asyncio import StreamReader, StreamWriter
from asyncio.exceptions import TimeoutError
from enum import Enum
from typing import Optional

logger = logging.getLogger(__name__)

Expand All @@ -14,7 +14,7 @@ class DeviceType(Enum):
UNKNOWN = 'unknown'


class AuthenticationFailed(Exception):
class AuthenticationError(Exception):
pass


Expand All @@ -23,8 +23,8 @@ def __init__(self, host: str, port: int, password: str) -> None:
self._host = host
self._port = port
self._password = password
self._reader: Optional[StreamReader] = None
self._writer: Optional[StreamWriter] = None
self._reader: StreamReader | None = None
self._writer: StreamWriter | None = None
self._semaphore = asyncio.Semaphore()
self._connected = False

Expand Down Expand Up @@ -53,7 +53,7 @@ async def connect(self):
self._connected = True
logger.info(f'Connected and authenticated to {self._host}:{self._port}')
except TimeoutError:
raise AuthenticationFailed()
raise AuthenticationError()

async def disconnect(self):
self._connected = False
Expand Down
8 changes: 3 additions & 5 deletions custom_components/extron/media_player.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import logging

from homeassistant.components.media_player import MediaPlayerEntity, MediaPlayerEntityFeature, \
MediaPlayerState
from homeassistant.components.media_player import MediaPlayerEntity, MediaPlayerEntityFeature, MediaPlayerState
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity import DeviceInfo

from custom_components.extron import ExtronConfigEntryRuntimeData, DeviceInformation
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
from custom_components.extron.const import CONF_DEVICE_TYPE
from custom_components.extron.extron import DeviceType, SurroundSoundProcessor, HDMISwitcher, \
ExtronDevice
from custom_components.extron.extron import DeviceType, ExtronDevice, HDMISwitcher, SurroundSoundProcessor

logger = logging.getLogger(__name__)

Expand Down
7 changes: 4 additions & 3 deletions custom_components/extron/sensor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import logging

from datetime import date, datetime
from decimal import Decimal

from homeassistant.components.sensor import SensorEntity, SensorDeviceClass, SensorStateClass
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity, SensorStateClass
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.typing import StateType

from custom_components.extron import ExtronConfigEntryRuntimeData, DeviceInformation
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
from custom_components.extron.const import CONF_DEVICE_TYPE
from custom_components.extron.extron import SurroundSoundProcessor, DeviceType
from custom_components.extron.extron import DeviceType, SurroundSoundProcessor

logger = logging.getLogger(__name__)

Expand Down
100 changes: 100 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[tool.ruff]
# https://github.com/astral-sh/ruff#configuration
include = ["*.py", "*.pyi", "**/pyproject.toml"]
extend-include = ["*.ipynb"]
target-version = "py311"
line-length = 120

[tool.ruff.lint]
exclude = [
"__pypackages__",
"_build",
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"*.ipynb",
"buck-out",
"build",
"dist",
"node_modules",
"venv*",
]
ignore = []
per-file-ignores = {}
# https://docs.astral.sh/ruff/rules/
select = ["E4", "E7", "E9", "F", "W", "N", "UP", "I"]

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"I",
"N",
"Q",
"S",
"T",
"W",
"ANN",
"ARG",
"BLE",
"COM",
"DJ",
"DTZ",
"EM",
"ERA",
"EXE",
"FBT",
"ICN",
"INP",
"ISC",
"NPY",
"PD",
"PGH",
"PIE",
"PL",
"PT",
"PTH",
"PYI",
"RET",
"RSE",
"RUF",
"SIM",
"SLF",
"TCH",
"TID",
"TRY",
"UP",
"YTT",
]
unfixable = []

[tool.ruff.lint.isort]
# https://docs.astral.sh/ruff/settings/#lintisort
combine-as-imports = true
lines-between-types = 1
order-by-type = true
known-first-party = ["src"]
section-order = [
"future",
"standard-library",
"third-party",
"first-party",
"local-folder",
]

0 comments on commit 41a4732

Please sign in to comment.