1
mirror of https://github.com/home-assistant/supervisor synced 2024-08-02 22:09:57 +02:00
ha-supervisor/supervisor/config.py
Pascal Vizeli ad988f2a24
Add diagnostics support (#1870)
* Add diagnostics support

Signed-off-by: Pascal Vizeli <pvizeli@syshack.ch>

* add aditional data

* Fix handling

* Better states

* Fix opt

* Update supervisor/bootstrap.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* Only events on supported systems

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
2020-08-05 14:54:03 +02:00

279 lines
8.2 KiB
Python

"""Bootstrap Supervisor."""
from datetime import datetime
import logging
import os
from pathlib import Path, PurePath
from typing import List, Optional
from .const import (
ATTR_ADDONS_CUSTOM_LIST,
ATTR_DEBUG,
ATTR_DEBUG_BLOCK,
ATTR_DIAGNOSTICS,
ATTR_LAST_BOOT,
ATTR_LOGGING,
ATTR_TIMEZONE,
ATTR_VERSION,
ATTR_WAIT_BOOT,
ENV_SUPERVISOR_SHARE,
FILE_HASSIO_CONFIG,
SUPERVISOR_DATA,
LogLevel,
)
from .utils.dt import parse_datetime
from .utils.json import JsonConfig
from .validate import SCHEMA_SUPERVISOR_CONFIG
_LOGGER: logging.Logger = logging.getLogger(__name__)
HOMEASSISTANT_CONFIG = PurePath("homeassistant")
HASSIO_SSL = PurePath("ssl")
ADDONS_CORE = PurePath("addons/core")
ADDONS_LOCAL = PurePath("addons/local")
ADDONS_GIT = PurePath("addons/git")
ADDONS_DATA = PurePath("addons/data")
BACKUP_DATA = PurePath("backup")
SHARE_DATA = PurePath("share")
TMP_DATA = PurePath("tmp")
APPARMOR_DATA = PurePath("apparmor")
DNS_DATA = PurePath("dns")
AUDIO_DATA = PurePath("audio")
DEFAULT_BOOT_TIME = datetime.utcfromtimestamp(0).isoformat()
class CoreConfig(JsonConfig):
"""Hold all core config data."""
def __init__(self):
"""Initialize config object."""
super().__init__(FILE_HASSIO_CONFIG, SCHEMA_SUPERVISOR_CONFIG)
@property
def timezone(self) -> str:
"""Return system timezone."""
return self._data[ATTR_TIMEZONE]
@timezone.setter
def timezone(self, value: str) -> None:
"""Set system timezone."""
self._data[ATTR_TIMEZONE] = value
@property
def version(self) -> str:
"""Return config version."""
return self._data[ATTR_VERSION]
@version.setter
def version(self, value: str) -> None:
"""Set config version."""
self._data[ATTR_VERSION] = value
@property
def wait_boot(self) -> int:
"""Return wait time for auto boot stages."""
return self._data[ATTR_WAIT_BOOT]
@wait_boot.setter
def wait_boot(self, value: int) -> None:
"""Set wait boot time."""
self._data[ATTR_WAIT_BOOT] = value
@property
def debug(self) -> bool:
"""Return True if ptvsd is enabled."""
return self._data[ATTR_DEBUG]
@debug.setter
def debug(self, value: bool) -> None:
"""Set debug mode."""
self._data[ATTR_DEBUG] = value
@property
def debug_block(self) -> bool:
"""Return True if ptvsd should waiting."""
return self._data[ATTR_DEBUG_BLOCK]
@debug_block.setter
def debug_block(self, value: bool) -> None:
"""Set debug wait mode."""
self._data[ATTR_DEBUG_BLOCK] = value
@property
def diagnostics(self) -> Optional[bool]:
"""Return bool if diagnostics is set otherwise None."""
return self._data[ATTR_DIAGNOSTICS]
@diagnostics.setter
def diagnostics(self, value: bool) -> None:
"""Set diagnostics settings."""
self._data[ATTR_DIAGNOSTICS] = value
@property
def logging(self) -> LogLevel:
"""Return log level of system."""
return self._data[ATTR_LOGGING]
@logging.setter
def logging(self, value: LogLevel) -> None:
"""Set system log level."""
self._data[ATTR_LOGGING] = value
self.modify_log_level()
def modify_log_level(self) -> None:
"""Change log level."""
lvl = getattr(logging, str(self.logging.value).upper())
logging.getLogger("supervisor").setLevel(lvl)
@property
def last_boot(self) -> datetime:
"""Return last boot datetime."""
boot_str = self._data.get(ATTR_LAST_BOOT, DEFAULT_BOOT_TIME)
boot_time = parse_datetime(boot_str)
if not boot_time:
return datetime.utcfromtimestamp(1)
return boot_time
@last_boot.setter
def last_boot(self, value: datetime) -> None:
"""Set last boot datetime."""
self._data[ATTR_LAST_BOOT] = value.isoformat()
@property
def path_supervisor(self) -> Path:
"""Return Supervisor data path."""
return SUPERVISOR_DATA
@property
def path_extern_supervisor(self) -> PurePath:
"""Return Supervisor data path external for Docker."""
return PurePath(os.environ[ENV_SUPERVISOR_SHARE])
@property
def path_extern_homeassistant(self) -> str:
"""Return config path external for Docker."""
return str(PurePath(self.path_extern_supervisor, HOMEASSISTANT_CONFIG))
@property
def path_homeassistant(self) -> Path:
"""Return config path inside supervisor."""
return Path(SUPERVISOR_DATA, HOMEASSISTANT_CONFIG)
@property
def path_extern_ssl(self) -> str:
"""Return SSL path external for Docker."""
return str(PurePath(self.path_extern_supervisor, HASSIO_SSL))
@property
def path_ssl(self) -> Path:
"""Return SSL path inside supervisor."""
return Path(SUPERVISOR_DATA, HASSIO_SSL)
@property
def path_addons_core(self) -> Path:
"""Return git path for core Add-ons."""
return Path(SUPERVISOR_DATA, ADDONS_CORE)
@property
def path_addons_git(self) -> Path:
"""Return path for Git Add-on."""
return Path(SUPERVISOR_DATA, ADDONS_GIT)
@property
def path_addons_local(self) -> Path:
"""Return path for custom Add-ons."""
return Path(SUPERVISOR_DATA, ADDONS_LOCAL)
@property
def path_extern_addons_local(self) -> PurePath:
"""Return path for custom Add-ons."""
return PurePath(self.path_extern_supervisor, ADDONS_LOCAL)
@property
def path_addons_data(self) -> Path:
"""Return root Add-on data folder."""
return Path(SUPERVISOR_DATA, ADDONS_DATA)
@property
def path_extern_addons_data(self) -> PurePath:
"""Return root add-on data folder external for Docker."""
return PurePath(self.path_extern_supervisor, ADDONS_DATA)
@property
def path_audio(self) -> Path:
"""Return root audio data folder."""
return Path(SUPERVISOR_DATA, AUDIO_DATA)
@property
def path_extern_audio(self) -> PurePath:
"""Return root audio data folder external for Docker."""
return PurePath(self.path_extern_supervisor, AUDIO_DATA)
@property
def path_tmp(self) -> Path:
"""Return Supervisor temp folder."""
return Path(SUPERVISOR_DATA, TMP_DATA)
@property
def path_extern_tmp(self) -> PurePath:
"""Return Supervisor temp folder for Docker."""
return PurePath(self.path_extern_supervisor, TMP_DATA)
@property
def path_backup(self) -> Path:
"""Return root backup data folder."""
return Path(SUPERVISOR_DATA, BACKUP_DATA)
@property
def path_extern_backup(self) -> PurePath:
"""Return root backup data folder external for Docker."""
return PurePath(self.path_extern_supervisor, BACKUP_DATA)
@property
def path_share(self) -> Path:
"""Return root share data folder."""
return Path(SUPERVISOR_DATA, SHARE_DATA)
@property
def path_apparmor(self) -> Path:
"""Return root Apparmor profile folder."""
return Path(SUPERVISOR_DATA, APPARMOR_DATA)
@property
def path_extern_share(self) -> PurePath:
"""Return root share data folder external for Docker."""
return PurePath(self.path_extern_supervisor, SHARE_DATA)
@property
def path_extern_dns(self) -> str:
"""Return dns path external for Docker."""
return str(PurePath(self.path_extern_supervisor, DNS_DATA))
@property
def path_dns(self) -> Path:
"""Return dns path inside supervisor."""
return Path(SUPERVISOR_DATA, DNS_DATA)
@property
def addons_repositories(self) -> List[str]:
"""Return list of custom Add-on repositories."""
return self._data[ATTR_ADDONS_CUSTOM_LIST]
def add_addon_repository(self, repo: str) -> None:
"""Add a custom repository to list."""
if repo in self._data[ATTR_ADDONS_CUSTOM_LIST]:
return
self._data[ATTR_ADDONS_CUSTOM_LIST].append(repo)
def drop_addon_repository(self, repo: str) -> None:
"""Remove a custom repository from list."""
if repo not in self._data[ATTR_ADDONS_CUSTOM_LIST]:
return
self._data[ATTR_ADDONS_CUSTOM_LIST].remove(repo)