Skip to content

Commit

Permalink
Replace attrs with dataclasses in hassfest (home-assistant#84475)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Dec 23, 2022
1 parent 94755a5 commit 139dd22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
45 changes: 22 additions & 23 deletions script/hassfest/model.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
"""Models for manifest validator."""
from __future__ import annotations

from dataclasses import dataclass, field
import json
import pathlib
from typing import Any

import attr


@attr.s
@dataclass
class Error:
"""Error validating an integration."""

plugin: str = attr.ib()
error: str = attr.ib()
fixable: bool = attr.ib(default=False)
plugin: str
error: str
fixable: bool = False

def __str__(self) -> str:
"""Represent error as string."""
return f"[{self.plugin.upper()}] {self.error}"


@attr.s
@dataclass
class Config:
"""Config for the run."""

specific_integrations: list[pathlib.Path] | None = attr.ib()
root: pathlib.Path = attr.ib()
action: str = attr.ib()
requirements: bool = attr.ib()
errors: list[Error] = attr.ib(factory=list)
cache: dict[str, Any] = attr.ib(factory=dict)
plugins: set[str] = attr.ib(factory=set)
specific_integrations: list[pathlib.Path] | None
root: pathlib.Path
action: str
requirements: bool
errors: list[Error] = field(default_factory=list)
cache: dict[str, Any] = field(default_factory=dict)
plugins: set[str] = field(default_factory=set)

def add_error(self, *args: Any, **kwargs: Any) -> None:
"""Add an error."""
self.errors.append(Error(*args, **kwargs))


@attr.s
@dataclass
class Brand:
"""Represent a brand in our validator."""

Expand All @@ -54,8 +53,8 @@ def load_dir(cls, path: pathlib.Path, config: Config) -> dict[str, Brand]:

return brands

path: pathlib.Path = attr.ib()
_brand: dict[str, Any] | None = attr.ib(default=None)
path: pathlib.Path
_brand: dict[str, Any] | None = None

@property
def brand(self) -> dict[str, Any]:
Expand Down Expand Up @@ -100,7 +99,7 @@ def load_brand(self, config: Config) -> None:
self._brand = brand


@attr.s
@dataclass
class Integration:
"""Represent an integration in our validator."""

Expand Down Expand Up @@ -129,11 +128,11 @@ def load_dir(cls, path: pathlib.Path) -> dict[str, Integration]:

return integrations

path: pathlib.Path = attr.ib()
_manifest: dict[str, Any] | None = attr.ib(default=None)
errors: list[Error] = attr.ib(factory=list)
warnings: list[Error] = attr.ib(factory=list)
translated_name: bool = attr.ib(default=False)
path: pathlib.Path
_manifest: dict[str, Any] | None = None
errors: list[Error] = field(default_factory=list)
warnings: list[Error] = field(default_factory=list)
translated_name: bool = False

@property
def manifest(self) -> dict[str, Any]:
Expand Down
2 changes: 1 addition & 1 deletion tests/hassfest/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def integration():
"""Fixture for hassfest integration model."""
integration = Integration(
path=Path("homeassistant/components/test"),
manifest={
_manifest={
"domain": "test",
"documentation": "https://example.com",
"name": "test",
Expand Down

0 comments on commit 139dd22

Please sign in to comment.