def validate_services(integration: Integration): """Validate services.""" # Find if integration uses services has_services = grep_dir(integration.path, "**/*.py", r"opp\.services\.(register|async_register)") if not has_services: return try: data = load_yaml(str(integration.path / "services.yaml")) except FileNotFoundError: integration.add_error("services", "Registers services but has no services.yaml") return except OpenPeerPowerError: integration.add_error( "services", "Registers services but unable to load services.yaml") return try: SERVICES_SCHEMA(data) except vol.Invalid as err: integration.add_error( "services", "Invalid services.yaml: {}".format(humanize_error(data, err)))
def validate_services(integration: Integration): """Validate services.""" try: data = load_yaml(str(integration.path / "services.yaml")) except FileNotFoundError: # Find if integration uses services has_services = grep_dir( integration.path, "**/*.py", r"(opp..services\.(register|async_register))|async_register_entity_service|async_register_admin_service", ) if has_services: integration.add_error( "services", "Registers services but has no services.yaml" ) return except OpenPeerPowerError: integration.add_error("services", "Unable to load services.yaml") return try: SERVICES_SCHEMA(data) except vol.Invalid as err: integration.add_error( "services", f"Invalid services.yaml: {humanize_error(data, err)}" )
def mock_load_blueprint(self, path: str) -> Blueprint: if path != blueprint_path: assert False, f"Unexpected blueprint {path}" return orig_load(self, path) return Blueprint(yaml.load_yaml(data_path), expected_domain=self.domain, path=path)
def test_default_blueprints(domain: str): """Validate a folder of blueprints.""" integration = importlib.import_module(f"openpeerpower.components.{domain}") blueprint_folder = pathlib.Path(integration.__file__).parent / BLUEPRINT_FOLDER items = list(blueprint_folder.glob("*")) assert len(items) > 0, "Folder cannot be empty" for fil in items: LOGGER.info("Processing %s", fil) assert fil.name.endswith(".yaml") data = yaml.load_yaml(fil) models.Blueprint(data, expected_domain=domain)
def _load_services_file(opp: OpenPeerPower, integration: Integration) -> JSON_TYPE: """Load services file for an integration.""" try: return load_yaml(str(integration.file_path / "services.yaml")) except FileNotFoundError: _LOGGER.warning("Unable to find services.yaml for the %s integration", integration.domain) return {} except OpenPeerPowerError: _LOGGER.warning("Unable to parse services.yaml for the %s integration", integration.domain) return {}
def _load_blueprint(self, blueprint_path) -> Blueprint: """Load a blueprint.""" try: blueprint_data = yaml.load_yaml(self.blueprint_folder / blueprint_path) except FileNotFoundError as err: raise FailedToLoad( self.domain, blueprint_path, FileNotFoundError(f"Unable to find {blueprint_path}"), ) from err except OpenPeerPowerError as err: raise FailedToLoad(self.domain, blueprint_path, err) from err return Blueprint(blueprint_data, expected_domain=self.domain, path=blueprint_path)
def _load_config(self, force): """Load the actual config.""" fname = self.opp.config.path(LOVELACE_CONFIG_FILE) # Check for a cached version of the config if not force and self._cache is not None: config, last_update = self._cache modtime = os.path.getmtime(fname) if config and last_update > modtime: return False, config is_updated = self._cache is not None try: config = load_yaml(fname) except FileNotFoundError: raise ConfigNotFound from None self._cache = (config, time.time()) return is_updated, config
def _load_config(self, force): """Load the actual config.""" # Check for a cached version of the config if not force and self._cache is not None: config, last_update = self._cache modtime = os.path.getmtime(self.path) if config and last_update > modtime: return False, config is_updated = self._cache is not None try: config = load_yaml(self.path, Secrets(Path(self.opp.config.config_dir))) except FileNotFoundError: raise ConfigNotFound from None self._cache = (config, time.time()) return is_updated, config
def load_yaml_config_file(config_path: str, secrets: Secrets | None = None) -> dict[Any, Any]: """Parse a YAML configuration file. Raises FileNotFoundError or OpenPeerPowerError. This method needs to run in an executor. """ conf_dict = load_yaml(config_path, secrets) if not isinstance(conf_dict, dict): msg = (f"The configuration file {os.path.basename(config_path)} " "does not contain a dictionary") _LOGGER.error(msg) raise OpenPeerPowerError(msg) # Convert values to dictionaries if they are None for key, value in conf_dict.items(): conf_dict[key] = value or {} return conf_dict
def test_no_key(): """Test item without a key.""" files = {YAML_CONFIG_FILE: "a: a\nnokeyhere"} with pytest.raises(OpenPeerPowerError), patch_yaml_files(files): yaml.load_yaml(YAML_CONFIG_FILE)
def _read(path): """Read YAML helper.""" if not os.path.isfile(path): return None return load_yaml(path)