Remove duplicate code in hassfest model (#82203)

This commit is contained in:
epenet 2022-11-17 09:10:37 +01:00 committed by GitHub
parent 146fe8f156
commit 89c7568515
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 18 deletions

View File

@ -55,7 +55,13 @@ class Brand:
return brands return brands
path: pathlib.Path = attr.ib() 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 @property
def domain(self) -> str: def domain(self) -> str:
@ -65,19 +71,16 @@ class Brand:
@property @property
def name(self) -> str | None: def name(self) -> str | None:
"""Return name of the integration.""" """Return name of the integration."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("name") return self.brand.get("name")
@property @property
def integrations(self) -> list[str]: def integrations(self) -> list[str]:
"""Return the sub integrations of this brand.""" """Return the sub integrations of this brand."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("integrations", []) return self.brand.get("integrations", [])
@property @property
def iot_standards(self) -> list[str]: def iot_standards(self) -> list[str]:
"""Return list of supported IoT standards.""" """Return list of supported IoT standards."""
assert self.brand is not None, "brand has not been loaded"
return self.brand.get("iot_standards", []) return self.brand.get("iot_standards", [])
def load_brand(self, config: Config) -> None: def load_brand(self, config: Config) -> None:
@ -94,7 +97,7 @@ class Brand:
) )
return return
self.brand = brand self._brand = brand
@attr.s @attr.s
@ -127,11 +130,17 @@ class Integration:
return integrations return integrations
path: pathlib.Path = attr.ib() 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) errors: list[Error] = attr.ib(factory=list)
warnings: list[Error] = attr.ib(factory=list) warnings: list[Error] = attr.ib(factory=list)
translated_name: bool = attr.ib(default=False) 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 @property
def domain(self) -> str: def domain(self) -> str:
"""Integration domain.""" """Integration domain."""
@ -145,62 +154,52 @@ class Integration:
@property @property
def disabled(self) -> str | None: def disabled(self) -> str | None:
"""Return if integration is disabled.""" """Return if integration is disabled."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("disabled") return self.manifest.get("disabled")
@property @property
def name(self) -> str: def name(self) -> str:
"""Return name of the integration.""" """Return name of the integration."""
assert self.manifest is not None, "manifest has not been loaded"
name: str = self.manifest["name"] name: str = self.manifest["name"]
return name return name
@property @property
def quality_scale(self) -> str | None: def quality_scale(self) -> str | None:
"""Return quality scale of the integration.""" """Return quality scale of the integration."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("quality_scale") return self.manifest.get("quality_scale")
@property @property
def config_flow(self) -> bool: def config_flow(self) -> bool:
"""Return if the integration has a config flow.""" """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) return self.manifest.get("config_flow", False)
@property @property
def requirements(self) -> list[str]: def requirements(self) -> list[str]:
"""List of requirements.""" """List of requirements."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("requirements", []) return self.manifest.get("requirements", [])
@property @property
def dependencies(self) -> list[str]: def dependencies(self) -> list[str]:
"""List of dependencies.""" """List of dependencies."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("dependencies", []) return self.manifest.get("dependencies", [])
@property @property
def supported_by(self) -> str: def supported_by(self) -> str:
"""Return the integration supported by this virtual integration.""" """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", {}) return self.manifest.get("supported_by", {})
@property @property
def integration_type(self) -> str: def integration_type(self) -> str:
"""Get integration_type.""" """Get integration_type."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("integration_type", "hub") return self.manifest.get("integration_type", "hub")
@property @property
def iot_class(self) -> str | None: def iot_class(self) -> str | None:
"""Return the integration IoT Class.""" """Return the integration IoT Class."""
assert self.manifest is not None, "manifest has not been loaded"
return self.manifest.get("iot_class") return self.manifest.get("iot_class")
@property @property
def iot_standards(self) -> list[str]: def iot_standards(self) -> list[str]:
"""Return the IoT standard supported by this virtual integration.""" """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", []) return self.manifest.get("iot_standards", [])
def add_error(self, *args: Any, **kwargs: Any) -> None: def add_error(self, *args: Any, **kwargs: Any) -> None:
@ -224,4 +223,4 @@ class Integration:
self.add_error("model", f"Manifest contains invalid JSON: {err}") self.add_error("model", f"Manifest contains invalid JSON: {err}")
return return
self.manifest = manifest self._manifest = manifest

View File

@ -13,7 +13,7 @@ from script.hassfest.model import Integration
def integration(): def integration():
"""Fixture for hassfest integration model.""" """Fixture for hassfest integration model."""
integration = Integration("") integration = Integration("")
integration.manifest = { integration._manifest = {
"domain": "test", "domain": "test",
"documentation": "https://example.com", "documentation": "https://example.com",
"name": "test", "name": "test",