diff --git a/supervisor/homeassistant/secrets.py b/supervisor/homeassistant/secrets.py index 467552eae..b29d6dda6 100644 --- a/supervisor/homeassistant/secrets.py +++ b/supervisor/homeassistant/secrets.py @@ -5,6 +5,7 @@ from pathlib import Path from typing import Dict, Optional, Union from ..coresys import CoreSys, CoreSysAttributes +from ..exceptions import YamlFileError from ..jobs.const import JobExecutionLimit from ..jobs.decorator import Job from ..utils.yaml import read_yaml_file @@ -48,7 +49,16 @@ class HomeAssistantSecrets(CoreSysAttributes): return # Read secrets - secrets = await self.sys_run_in_executor(read_yaml_file, self.path_secrets) + try: + secrets = await self.sys_run_in_executor(read_yaml_file, self.path_secrets) + except YamlFileError as err: + _LOGGER.warning("Can't read Home Assistant secrets: %s", err) + return + + if not isinstance(secrets, dict): + return + + # Process secrets self.secrets = { k: v for k, v in secrets.items() if isinstance(v, (bool, float, int, str)) } diff --git a/supervisor/utils/json.py b/supervisor/utils/json.py index 57959a356..d4b22caa7 100644 --- a/supervisor/utils/json.py +++ b/supervisor/utils/json.py @@ -39,8 +39,9 @@ def write_json_file(jsonfile: Path, data: Any) -> None: fp.write(json.dumps(data, indent=2, cls=JSONEncoder)) jsonfile.chmod(0o600) except (OSError, ValueError, TypeError) as err: - _LOGGER.error("Can't write %s: %s", jsonfile, err) - raise JsonFileError() from err + raise JsonFileError( + f"Can't write {jsonfile!s}: {err!s}", _LOGGER.error + ) from err def read_json_file(jsonfile: Path) -> Any: @@ -48,5 +49,6 @@ def read_json_file(jsonfile: Path) -> Any: try: return json.loads(jsonfile.read_text()) except (OSError, ValueError, TypeError, UnicodeDecodeError) as err: - _LOGGER.error("Can't read json from %s: %s", jsonfile, err) - raise JsonFileError() from err + raise JsonFileError( + f"Can't read json from {jsonfile!s}: {err!s}", _LOGGER.error + ) from err diff --git a/supervisor/utils/yaml.py b/supervisor/utils/yaml.py index 49f9cd78d..131a3c08c 100644 --- a/supervisor/utils/yaml.py +++ b/supervisor/utils/yaml.py @@ -19,8 +19,9 @@ def read_yaml_file(path: Path) -> dict: return _YAML.load(path) or {} except (YAMLError, AttributeError) as err: - _LOGGER.error("Can't read YAML file %s - %s", path, err) - raise YamlFileError() from err + raise YamlFileError( + f"Can't read YAML file {path!s} - {err!s}", _LOGGER.error + ) from err def write_yaml_file(path: Path, data: dict) -> None: @@ -30,5 +31,4 @@ def write_yaml_file(path: Path, data: dict) -> None: _YAML.dump(data, fp) path.chmod(0o600) except (YAMLError, OSError, ValueError, TypeError) as err: - _LOGGER.error("Can't write %s: %s", path, err) - raise YamlFileError() from err + raise YamlFileError(f"Can't write {path!s}: {err!s}", _LOGGER.error) from err