Add support for integrations v2 (#78801)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Erik Montnemery
2022-09-28 17:31:48 +02:00
committed by GitHub
parent 7f08dd851e
commit b173ae7f44
10 changed files with 5623 additions and 12 deletions

View File

@@ -39,6 +39,62 @@ class Config:
self.errors.append(Error(*args, **kwargs))
@attr.s
class Brand:
"""Represent a brand in our validator."""
@classmethod
def load_dir(cls, path: pathlib.Path, config: Config):
"""Load all brands in a directory."""
assert path.is_dir()
brands = {}
for fil in path.iterdir():
brand = cls(fil)
brand.load_brand(config)
brands[brand.domain] = brand
return brands
path: pathlib.Path = attr.ib()
brand: dict[str, Any] | None = attr.ib(default=None)
@property
def domain(self) -> str:
"""Integration domain."""
return self.path.stem
@property
def name(self) -> str | None:
"""Return name of the integration."""
return self.brand.get("name")
@property
def integrations(self) -> list[str]:
"""Return the sub integrations of this brand."""
return self.brand.get("integrations")
@property
def iot_standards(self) -> list[str]:
"""Return list of supported IoT standards."""
return self.brand.get("iot_standards", [])
def load_brand(self, config: Config) -> None:
"""Load brand file."""
if not self.path.is_file():
config.add_error("model", f"Brand file {self.path} not found")
return
try:
brand = json.loads(self.path.read_text())
except ValueError as err:
config.add_error(
"model", f"Brand file {self.path.name} contains invalid JSON: {err}"
)
return
self.brand = brand
@attr.s
class Integration:
"""Represent an integration in our validator."""
@@ -71,6 +127,7 @@ class Integration:
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 domain(self) -> str:
@@ -122,6 +179,11 @@ class Integration:
"""Get integration_type."""
return self.manifest.get("integration_type", "integration")
@property
def iot_class(self) -> str | None:
"""Return the integration IoT Class."""
return self.manifest.get("iot_class")
def add_error(self, *args: Any, **kwargs: Any) -> None:
"""Add an error."""
self.errors.append(Error(*args, **kwargs))