Skip to content

Commit

Permalink
Remove duplicate code in hassfest model (home-assistant#82203)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Nov 17, 2022
1 parent 146fe8f commit 89c7568
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions script/hassfest/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ 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)
_brand: dict[str, Any] | None = attr.ib(default=None)

@property
def brand(self) -> dict[str, Any]:
"""Guarded access to brand."""
assert self._brand is not None, "brand has not been loaded"
return self._brand

@property
def domain(self) -> str:
Expand All @@ -65,19 +71,16 @@ def domain(self) -> str:
@property
def name(self) -> str | None:
"""Return name of the integration."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("name")

@property
def integrations(self) -> list[str]:
"""Return the sub integrations of this brand."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("integrations", [])

@property
def iot_standards(self) -> list[str]:
"""Return list of supported IoT standards."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("iot_standards", [])

def load_brand(self, config: Config) -> None:
Expand All @@ -94,7 +97,7 @@ def load_brand(self, config: Config) -> None:
)
return

self.brand = brand
self._brand = brand


@attr.s
Expand Down Expand Up @@ -127,11 +130,17 @@ 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)
_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)

@property
def manifest(self) -> dict[str, Any]:
"""Guarded access to manifest."""
assert self._manifest is not None, "manifest has not been loaded"
return self._manifest

@property
def domain(self) -> str:
"""Integration domain."""
Expand All @@ -145,62 +154,52 @@ def core(self) -> bool:
@property
def disabled(self) -> str | None:
"""Return if integration is disabled."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("disabled")

@property
def name(self) -> str:
"""Return name of the integration."""
assert self.manifest is not None, "manifest has not been loaded"
name: str = self.manifest["name"]
return name

@property
def quality_scale(self) -> str | None:
"""Return quality scale of the integration."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("quality_scale")

@property
def config_flow(self) -> bool:
"""Return if the integration has a config flow."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("config_flow", False)

@property
def requirements(self) -> list[str]:
"""List of requirements."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("requirements", [])

@property
def dependencies(self) -> list[str]:
"""List of dependencies."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("dependencies", [])

@property
def supported_by(self) -> str:
"""Return the integration supported by this virtual integration."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("supported_by", {})

@property
def integration_type(self) -> str:
"""Get integration_type."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("integration_type", "hub")

@property
def iot_class(self) -> str | None:
"""Return the integration IoT Class."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("iot_class")

@property
def iot_standards(self) -> list[str]:
"""Return the IoT standard supported by this virtual integration."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("iot_standards", [])

def add_error(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -224,4 +223,4 @@ def load_manifest(self) -> None:
self.add_error("model", f"Manifest contains invalid JSON: {err}")
return

self.manifest = manifest
self._manifest = manifest
2 changes: 1 addition & 1 deletion tests/hassfest/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def integration():
"""Fixture for hassfest integration model."""
integration = Integration("")
integration.manifest = {
integration._manifest = {
"domain": "test",
"documentation": "https://example.com",
"name": "test",
Expand Down

0 comments on commit 89c7568

Please sign in to comment.