mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-04-19 18:57:16 +00:00
Complete typehinting in CoreSys (#1745)
* Complete typehinting in CoreSys * Improve Addon model abstraction, solving Pylint warning
This commit is contained in:
parent
33bbd92d9b
commit
6caec79958
2
pylintrc
2
pylintrc
@ -2,6 +2,8 @@
|
||||
reports=no
|
||||
jobs=2
|
||||
|
||||
good-names=id,i,j,k,ex,Run,_,fp,T
|
||||
|
||||
# Reasons disabled:
|
||||
# format - handled by black
|
||||
# locally-disabled - it spams too much
|
||||
|
@ -71,9 +71,8 @@ class Addon(AddonModel):
|
||||
|
||||
def __init__(self, coresys: CoreSys, slug: str):
|
||||
"""Initialize data holder."""
|
||||
self.coresys: CoreSys = coresys
|
||||
super().__init__(coresys, slug)
|
||||
self.instance: DockerAddon = DockerAddon(coresys, self)
|
||||
self.slug: str = slug
|
||||
|
||||
async def load(self) -> None:
|
||||
"""Async initialize of object."""
|
||||
|
@ -65,7 +65,7 @@ from ..const import (
|
||||
SECURITY_PROFILE,
|
||||
AddonStages,
|
||||
)
|
||||
from ..coresys import CoreSysAttributes
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
from .validate import RE_SERVICE, RE_VOLUME, schema_ui_options, validate_options
|
||||
|
||||
Data = Dict[str, Any]
|
||||
@ -74,7 +74,10 @@ Data = Dict[str, Any]
|
||||
class AddonModel(CoreSysAttributes):
|
||||
"""Add-on Data layout."""
|
||||
|
||||
slug: str = None
|
||||
def __init__(self, coresys: CoreSys, slug: str):
|
||||
"""Initialize data holder."""
|
||||
self.coresys: CoreSys = coresys
|
||||
self.slug: str = slug
|
||||
|
||||
@property
|
||||
def data(self) -> Data:
|
||||
|
@ -2,7 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Optional, TypeVar
|
||||
|
||||
import aiohttp
|
||||
|
||||
@ -35,6 +35,9 @@ if TYPE_CHECKING:
|
||||
from .plugins import PluginManager
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class CoreSys:
|
||||
"""Class that handle all shared data."""
|
||||
|
||||
@ -82,6 +85,8 @@ class CoreSys:
|
||||
@property
|
||||
def dev(self) -> bool:
|
||||
"""Return True if we run dev mode."""
|
||||
if self._updater is None:
|
||||
return False
|
||||
return self._updater.channel == UpdateChannels.DEV
|
||||
|
||||
@property
|
||||
@ -127,10 +132,12 @@ class CoreSys:
|
||||
@property
|
||||
def core(self) -> Core:
|
||||
"""Return core object."""
|
||||
if self._core is None:
|
||||
raise RuntimeError("Core not set!")
|
||||
return self._core
|
||||
|
||||
@core.setter
|
||||
def core(self, value: Core):
|
||||
def core(self, value: Core) -> None:
|
||||
"""Set a Core object."""
|
||||
if self._core:
|
||||
raise RuntimeError("Core already set!")
|
||||
@ -139,10 +146,12 @@ class CoreSys:
|
||||
@property
|
||||
def plugins(self) -> PluginManager:
|
||||
"""Return PluginManager object."""
|
||||
if self._plugins is None:
|
||||
raise RuntimeError("PluginManager not set!")
|
||||
return self._plugins
|
||||
|
||||
@plugins.setter
|
||||
def plugins(self, value: PluginManager):
|
||||
def plugins(self, value: PluginManager) -> None:
|
||||
"""Set a PluginManager object."""
|
||||
if self._plugins:
|
||||
raise RuntimeError("PluginManager already set!")
|
||||
@ -151,10 +160,12 @@ class CoreSys:
|
||||
@property
|
||||
def arch(self) -> CpuArch:
|
||||
"""Return CpuArch object."""
|
||||
if self._arch is None:
|
||||
raise RuntimeError("CpuArch not set!")
|
||||
return self._arch
|
||||
|
||||
@arch.setter
|
||||
def arch(self, value: CpuArch):
|
||||
def arch(self, value: CpuArch) -> None:
|
||||
"""Set a CpuArch object."""
|
||||
if self._arch:
|
||||
raise RuntimeError("CpuArch already set!")
|
||||
@ -163,10 +174,12 @@ class CoreSys:
|
||||
@property
|
||||
def auth(self) -> Auth:
|
||||
"""Return Auth object."""
|
||||
if self._auth is None:
|
||||
raise RuntimeError("Auth not set!")
|
||||
return self._auth
|
||||
|
||||
@auth.setter
|
||||
def auth(self, value: Auth):
|
||||
def auth(self, value: Auth) -> None:
|
||||
"""Set a Auth object."""
|
||||
if self._auth:
|
||||
raise RuntimeError("Auth already set!")
|
||||
@ -175,10 +188,12 @@ class CoreSys:
|
||||
@property
|
||||
def homeassistant(self) -> HomeAssistant:
|
||||
"""Return Home Assistant object."""
|
||||
if self._homeassistant is None:
|
||||
raise RuntimeError("Home Assistant not set!")
|
||||
return self._homeassistant
|
||||
|
||||
@homeassistant.setter
|
||||
def homeassistant(self, value: HomeAssistant):
|
||||
def homeassistant(self, value: HomeAssistant) -> None:
|
||||
"""Set a HomeAssistant object."""
|
||||
if self._homeassistant:
|
||||
raise RuntimeError("Home Assistant already set!")
|
||||
@ -187,10 +202,12 @@ class CoreSys:
|
||||
@property
|
||||
def supervisor(self) -> Supervisor:
|
||||
"""Return Supervisor object."""
|
||||
if self._supervisor is None:
|
||||
raise RuntimeError("Supervisor not set!")
|
||||
return self._supervisor
|
||||
|
||||
@supervisor.setter
|
||||
def supervisor(self, value: Supervisor):
|
||||
def supervisor(self, value: Supervisor) -> None:
|
||||
"""Set a Supervisor object."""
|
||||
if self._supervisor:
|
||||
raise RuntimeError("Supervisor already set!")
|
||||
@ -199,10 +216,12 @@ class CoreSys:
|
||||
@property
|
||||
def api(self) -> RestAPI:
|
||||
"""Return API object."""
|
||||
if self._api is None:
|
||||
raise RuntimeError("API not set!")
|
||||
return self._api
|
||||
|
||||
@api.setter
|
||||
def api(self, value: RestAPI):
|
||||
def api(self, value: RestAPI) -> None:
|
||||
"""Set an API object."""
|
||||
if self._api:
|
||||
raise RuntimeError("API already set!")
|
||||
@ -211,10 +230,12 @@ class CoreSys:
|
||||
@property
|
||||
def updater(self) -> Updater:
|
||||
"""Return Updater object."""
|
||||
if self._updater is None:
|
||||
raise RuntimeError("Updater not set!")
|
||||
return self._updater
|
||||
|
||||
@updater.setter
|
||||
def updater(self, value: Updater):
|
||||
def updater(self, value: Updater) -> None:
|
||||
"""Set a Updater object."""
|
||||
if self._updater:
|
||||
raise RuntimeError("Updater already set!")
|
||||
@ -223,10 +244,12 @@ class CoreSys:
|
||||
@property
|
||||
def secrets(self) -> SecretsManager:
|
||||
"""Return SecretsManager object."""
|
||||
if self._secrets is None:
|
||||
raise RuntimeError("SecretsManager not set!")
|
||||
return self._secrets
|
||||
|
||||
@secrets.setter
|
||||
def secrets(self, value: SecretsManager):
|
||||
def secrets(self, value: SecretsManager) -> None:
|
||||
"""Set a Updater object."""
|
||||
if self._secrets:
|
||||
raise RuntimeError("SecretsManager already set!")
|
||||
@ -235,10 +258,12 @@ class CoreSys:
|
||||
@property
|
||||
def addons(self) -> AddonManager:
|
||||
"""Return AddonManager object."""
|
||||
if self._addons is None:
|
||||
raise RuntimeError("AddonManager not set!")
|
||||
return self._addons
|
||||
|
||||
@addons.setter
|
||||
def addons(self, value: AddonManager):
|
||||
def addons(self, value: AddonManager) -> None:
|
||||
"""Set a AddonManager object."""
|
||||
if self._addons:
|
||||
raise RuntimeError("AddonManager already set!")
|
||||
@ -247,10 +272,12 @@ class CoreSys:
|
||||
@property
|
||||
def store(self) -> StoreManager:
|
||||
"""Return StoreManager object."""
|
||||
if self._store is None:
|
||||
raise RuntimeError("StoreManager not set!")
|
||||
return self._store
|
||||
|
||||
@store.setter
|
||||
def store(self, value: StoreManager):
|
||||
def store(self, value: StoreManager) -> None:
|
||||
"""Set a StoreManager object."""
|
||||
if self._store:
|
||||
raise RuntimeError("StoreManager already set!")
|
||||
@ -259,10 +286,12 @@ class CoreSys:
|
||||
@property
|
||||
def snapshots(self) -> SnapshotManager:
|
||||
"""Return SnapshotManager object."""
|
||||
if self._snapshots is None:
|
||||
raise RuntimeError("SnapshotManager not set!")
|
||||
return self._snapshots
|
||||
|
||||
@snapshots.setter
|
||||
def snapshots(self, value: SnapshotManager):
|
||||
def snapshots(self, value: SnapshotManager) -> None:
|
||||
"""Set a SnapshotManager object."""
|
||||
if self._snapshots:
|
||||
raise RuntimeError("SnapshotsManager already set!")
|
||||
@ -271,10 +300,12 @@ class CoreSys:
|
||||
@property
|
||||
def tasks(self) -> Tasks:
|
||||
"""Return Tasks object."""
|
||||
if self._tasks is None:
|
||||
raise RuntimeError("Tasks not set!")
|
||||
return self._tasks
|
||||
|
||||
@tasks.setter
|
||||
def tasks(self, value: Tasks):
|
||||
def tasks(self, value: Tasks) -> None:
|
||||
"""Set a Tasks object."""
|
||||
if self._tasks:
|
||||
raise RuntimeError("Tasks already set!")
|
||||
@ -283,10 +314,12 @@ class CoreSys:
|
||||
@property
|
||||
def services(self) -> ServiceManager:
|
||||
"""Return ServiceManager object."""
|
||||
if self._services is None:
|
||||
raise RuntimeError("Services not set!")
|
||||
return self._services
|
||||
|
||||
@services.setter
|
||||
def services(self, value: ServiceManager):
|
||||
def services(self, value: ServiceManager) -> None:
|
||||
"""Set a ServiceManager object."""
|
||||
if self._services:
|
||||
raise RuntimeError("Services already set!")
|
||||
@ -295,10 +328,12 @@ class CoreSys:
|
||||
@property
|
||||
def discovery(self) -> Discovery:
|
||||
"""Return ServiceManager object."""
|
||||
if self._discovery is None:
|
||||
raise RuntimeError("Discovery not set!")
|
||||
return self._discovery
|
||||
|
||||
@discovery.setter
|
||||
def discovery(self, value: Discovery):
|
||||
def discovery(self, value: Discovery) -> None:
|
||||
"""Set a Discovery object."""
|
||||
if self._discovery:
|
||||
raise RuntimeError("Discovery already set!")
|
||||
@ -307,10 +342,12 @@ class CoreSys:
|
||||
@property
|
||||
def dbus(self) -> DBusManager:
|
||||
"""Return DBusManager object."""
|
||||
if self._dbus is None:
|
||||
raise RuntimeError("DBusManager not set!")
|
||||
return self._dbus
|
||||
|
||||
@dbus.setter
|
||||
def dbus(self, value: DBusManager):
|
||||
def dbus(self, value: DBusManager) -> None:
|
||||
"""Set a DBusManager object."""
|
||||
if self._dbus:
|
||||
raise RuntimeError("DBusManager already set!")
|
||||
@ -319,10 +356,12 @@ class CoreSys:
|
||||
@property
|
||||
def host(self) -> HostManager:
|
||||
"""Return HostManager object."""
|
||||
if self._host is None:
|
||||
raise RuntimeError("HostManager not set!")
|
||||
return self._host
|
||||
|
||||
@host.setter
|
||||
def host(self, value: HostManager):
|
||||
def host(self, value: HostManager) -> None:
|
||||
"""Set a HostManager object."""
|
||||
if self._host:
|
||||
raise RuntimeError("HostManager already set!")
|
||||
@ -331,10 +370,12 @@ class CoreSys:
|
||||
@property
|
||||
def hwmonitor(self) -> HwMonitor:
|
||||
"""Return HwMonitor object."""
|
||||
if self._hwmonitor is None:
|
||||
raise RuntimeError("HwMonitor not set!")
|
||||
return self._hwmonitor
|
||||
|
||||
@hwmonitor.setter
|
||||
def hwmonitor(self, value: HwMonitor):
|
||||
def hwmonitor(self, value: HwMonitor) -> None:
|
||||
"""Set a HwMonitor object."""
|
||||
if self._hwmonitor:
|
||||
raise RuntimeError("HwMonitor already set!")
|
||||
@ -343,10 +384,12 @@ class CoreSys:
|
||||
@property
|
||||
def ingress(self) -> Ingress:
|
||||
"""Return Ingress object."""
|
||||
if self._ingress is None:
|
||||
raise RuntimeError("Ingress not set!")
|
||||
return self._ingress
|
||||
|
||||
@ingress.setter
|
||||
def ingress(self, value: Ingress):
|
||||
def ingress(self, value: Ingress) -> None:
|
||||
"""Set a Ingress object."""
|
||||
if self._ingress:
|
||||
raise RuntimeError("Ingress already set!")
|
||||
@ -355,10 +398,12 @@ class CoreSys:
|
||||
@property
|
||||
def hassos(self) -> HassOS:
|
||||
"""Return HassOS object."""
|
||||
if self._hassos is None:
|
||||
raise RuntimeError("HassOS not set!")
|
||||
return self._hassos
|
||||
|
||||
@hassos.setter
|
||||
def hassos(self, value: HassOS):
|
||||
def hassos(self, value: HassOS) -> None:
|
||||
"""Set a HassOS object."""
|
||||
if self._hassos:
|
||||
raise RuntimeError("HassOS already set!")
|
||||
@ -370,7 +415,7 @@ class CoreSys:
|
||||
return self._machine
|
||||
|
||||
@machine.setter
|
||||
def machine(self, value: str):
|
||||
def machine(self, value: str) -> None:
|
||||
"""Set a machine type string."""
|
||||
if self._machine:
|
||||
raise RuntimeError("Machine type already set!")
|
||||
@ -382,7 +427,7 @@ class CoreSys:
|
||||
return self._machine_id
|
||||
|
||||
@machine_id.setter
|
||||
def machine_id(self, value: str):
|
||||
def machine_id(self, value: str) -> None:
|
||||
"""Set a machine-id type string."""
|
||||
if self._machine_id:
|
||||
raise RuntimeError("Machine-ID type already set!")
|
||||
@ -390,17 +435,17 @@ class CoreSys:
|
||||
|
||||
|
||||
class CoreSysAttributes:
|
||||
"""Inheret basic CoreSysAttributes."""
|
||||
"""Inherit basic CoreSysAttributes."""
|
||||
|
||||
coresys = None
|
||||
coresys: CoreSys
|
||||
|
||||
@property
|
||||
def sys_machine(self) -> str:
|
||||
def sys_machine(self) -> Optional[str]:
|
||||
"""Return running machine type of the Supervisor system."""
|
||||
return self.coresys.machine
|
||||
|
||||
@property
|
||||
def sys_dev(self) -> str:
|
||||
def sys_dev(self) -> bool:
|
||||
"""Return True if we run dev mode."""
|
||||
return self.coresys.dev
|
||||
|
||||
@ -410,7 +455,7 @@ class CoreSysAttributes:
|
||||
return self.coresys.timezone
|
||||
|
||||
@property
|
||||
def sys_machine_id(self) -> str:
|
||||
def sys_machine_id(self) -> Optional[str]:
|
||||
"""Return timezone."""
|
||||
return self.coresys.machine_id
|
||||
|
||||
@ -549,10 +594,12 @@ class CoreSysAttributes:
|
||||
"""Return HassOS object."""
|
||||
return self.coresys.hassos
|
||||
|
||||
def sys_run_in_executor(self, funct, *args) -> asyncio.Future:
|
||||
def sys_run_in_executor(
|
||||
self, funct: Callable[..., T], *args: Any
|
||||
) -> Coroutine[Any, Any, T]:
|
||||
"""Add an job to the executor pool."""
|
||||
return self.sys_loop.run_in_executor(None, funct, *args)
|
||||
|
||||
def sys_create_task(self, coroutine) -> asyncio.Task:
|
||||
def sys_create_task(self, coroutine: Coroutine) -> asyncio.Task:
|
||||
"""Create an async task."""
|
||||
return self.sys_loop.create_task(coroutine)
|
||||
|
@ -2,7 +2,6 @@
|
||||
import logging
|
||||
|
||||
from ..addons.model import AddonModel, Data
|
||||
from ..coresys import CoreSys
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
@ -10,11 +9,6 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
class AddonStore(AddonModel):
|
||||
"""Hold data for add-on inside Supervisor."""
|
||||
|
||||
def __init__(self, coresys: CoreSys, slug: str):
|
||||
"""Initialize data holder."""
|
||||
self.coresys: CoreSys = coresys
|
||||
self.slug: str = slug
|
||||
|
||||
@property
|
||||
def data(self) -> Data:
|
||||
"""Return add-on data/config."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user