Skip to content

Commit

Permalink
Migrate linting to ruff + black
Browse files Browse the repository at this point in the history
  • Loading branch information
trickeydan committed Jun 21, 2023
1 parent be3c297 commit 2ad5fec
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 329 deletions.
17 changes: 0 additions & 17 deletions .flake8

This file was deleted.

10 changes: 0 additions & 10 deletions .isort.cfg

This file was deleted.

10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ EXTRACODE:=
all: type test lint

lint:
$(CMD) flake8 $(PYMODULE) $(TESTS) $(EXTRACODE)
$(CMD) ruff $(PYMODULE) $(TESTS) $(EXTRACODE)
$(CMD) black --check $(PYMODULE) $(TESTS) $(EXTRACODE)

lint-fix:
$(CMD) ruff --fix $(PYMODULE) $(TESTS) $(EXTRACODE)
$(CMD) black $(PYMODULE) $(TESTS) $(EXTRACODE)

type:
$(CMD) mypy $(PYMODULE) $(TESTS) $(EXTRACODE)
Expand All @@ -19,8 +24,5 @@ test:
test-cov:
$(CMD) pytest --cov=$(PYMODULE) $(TESTS) --cov-report html

isort:
$(CMD) isort $(PYMODULE) $(TESTS) $(EXTRACODE)

clean:
git clean -Xdf # Delete all files in .gitignore
8 changes: 4 additions & 4 deletions hue2mqtt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class Config:
@classmethod
def _get_config_path(cls, config_str: Optional[str] = None) -> Path:
"""Check for a config file or search the filesystem for one."""
CONFIG_SEARCH_PATHS = [
config_search_paths = [
Path("hue2mqtt.toml"),
Path("/etc/hue2mqtt.toml"),
]
if config_str is None:
for path in CONFIG_SEARCH_PATHS:
for path in config_search_paths:
if path.exists() and path.is_file():
return path
else:
Expand All @@ -74,13 +74,13 @@ def _get_config_path(cls, config_str: Optional[str] = None) -> Path:
raise FileNotFoundError("Unable to find config file.")

@classmethod
def load(cls, config_str: Optional[str] = None) -> 'Hue2MQTTConfig':
def load(cls, config_str: Optional[str] = None) -> "Hue2MQTTConfig":
"""Load the config."""
config_path = cls._get_config_path(config_str)
with config_path.open("rb") as fh:
return cls.load_from_file(fh)

@classmethod
def load_from_file(cls, fh: IO[bytes]) -> 'Hue2MQTTConfig':
def load_from_file(cls, fh: IO[bytes]) -> "Hue2MQTTConfig":
"""Load the config from a file."""
return cls(**tomllib.load(fh))
4 changes: 3 additions & 1 deletion hue2mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ async def discover_bridge() -> None:
print("Your username is", bridge.username)
print("Please add these details to hue2mqtt.toml")
except LinkButtonNotPressed:
print("Error: Press the link button on the bridge before running discovery") # noqa: E501
print(
"Error: Press the link button on the bridge before running discovery",
)
sys.exit(1)
14 changes: 7 additions & 7 deletions hue2mqtt/hue2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
loop = asyncio.get_event_loop()


class Hue2MQTT():
class Hue2MQTT:
"""Hue to MQTT Bridge."""

config: Hue2MQTTConfig
Expand Down Expand Up @@ -198,19 +198,19 @@ async def handle_set_group(self, match: Match[str], payload: str) -> None:
async def main(self, websession: ClientSession) -> None:
"""Main method of the data component."""
# Publish initial info about lights
for id, light_raw in self._bridge.lights._items.items():
light = LightInfo(id=id, **light_raw.raw)
for idx, light_raw in self._bridge.lights._items.items():
light = LightInfo(id=idx, **light_raw.raw)
self.publish_light(light)

# Publish initial info about groups
for id, group_raw in self._bridge.groups._items.items():
group = GroupInfo(id=id, **group_raw.raw)
for idx, group_raw in self._bridge.groups._items.items():
group = GroupInfo(id=idx, **group_raw.raw)
self.publish_group(group)

# Publish initial info about sensors
for id, sensor_raw in self._bridge.sensors._items.items():
for idx, sensor_raw in self._bridge.sensors._items.items():
if "uniqueid" in sensor_raw.raw and "productname" in sensor_raw.raw:
sensor = SensorInfo(id=id, **sensor_raw.raw)
sensor = SensorInfo(id=idx, **sensor_raw.raw)
self.publish_sensor(sensor)
else:
LOGGER.debug(f"Ignoring virtual sensor: {sensor_raw.name}")
Expand Down
4 changes: 2 additions & 2 deletions hue2mqtt/mqtt/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def match(self, topic: str) -> Optional[Match[str]]:
return self.regex.match(topic)

@classmethod
def parse(cls, topic: str) -> 'Topic':
def parse(cls, topic: str) -> "Topic":
"""
Parse a string topic into a Topic instance.
Expand All @@ -48,7 +48,7 @@ def __str__(self) -> str:
return "/".join(str(p) for p in self.parts)

def __repr__(self) -> str:
return f"Topic(\"{self}\")"
return f'Topic("{self}")'

def __hash__(self) -> int:
return hash(repr(self))
Expand Down
14 changes: 7 additions & 7 deletions hue2mqtt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class LightState(LightBaseState):
class LightInfo(BaseModel):
"""Information about a light."""

id: int
id: int # noqa: A003
name: str
uniqueid: str
state: Optional[LightState]

manufacturername: str
modelid: str
productname: str
type: str
type: str # noqa: A003

swversion: str

Expand All @@ -69,11 +69,11 @@ class GroupState(BaseModel):
class GroupInfo(BaseModel):
"""Information about a light group."""

id: int
id: int # noqa: A003
name: str
lights: List[int]
sensors: List[int]
type: str
type: str # noqa: A003
state: GroupState

group_class: Optional[str] = Field(default=None, alias="class")
Expand Down Expand Up @@ -130,7 +130,7 @@ class HumiditySensorState(GenericSensorState):
class OpenCloseSensorState(GenericSensorState):
"""Information about the state of a sensor."""

open: Optional[str] = None
open: Optional[str] = None # noqa: A003


SensorState = create_model(
Expand All @@ -150,9 +150,9 @@ class OpenCloseSensorState(GenericSensorState):
class SensorInfo(BaseModel):
"""Information about a sensor."""

id: int
id: int # noqa: A003
name: str
type: str
type: str # noqa: A003
modelid: str
manufacturername: str

Expand Down
28 changes: 0 additions & 28 deletions mypy.ini

This file was deleted.

Loading

0 comments on commit 2ad5fec

Please sign in to comment.