mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-13 20:26:29 +00:00
Make host feature cheaper to call and relay on it (#2266)
This commit is contained in:
parent
2f9fc39b72
commit
3dd3340e35
@ -43,7 +43,7 @@ class APIHost(CoreSysAttributes):
|
||||
ATTR_DISK_FREE: self.sys_host.info.free_space,
|
||||
ATTR_DISK_TOTAL: self.sys_host.info.total_space,
|
||||
ATTR_DISK_USED: self.sys_host.info.used_space,
|
||||
ATTR_FEATURES: self.sys_host.supported_features,
|
||||
ATTR_FEATURES: self.sys_host.features,
|
||||
ATTR_HOSTNAME: self.sys_host.info.hostname,
|
||||
ATTR_KERNEL: self.sys_host.info.kernel,
|
||||
ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system,
|
||||
|
@ -39,7 +39,7 @@ class APIInfo(CoreSysAttributes):
|
||||
ATTR_DOCKER: self.sys_docker.info.version,
|
||||
ATTR_HOSTNAME: self.sys_host.info.hostname,
|
||||
ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system,
|
||||
ATTR_FEATURES: self.sys_host.supported_features,
|
||||
ATTR_FEATURES: self.sys_host.features,
|
||||
ATTR_MACHINE: self.sys_machine,
|
||||
ATTR_ARCH: self.sys_arch.default,
|
||||
ATTR_SUPPORTED_ARCH: self.sys_arch.supported,
|
||||
|
@ -55,3 +55,5 @@ class DBusManager(CoreSysAttributes):
|
||||
_LOGGER.error(
|
||||
"No D-Bus support on Host. Disabled any kind of host control!"
|
||||
)
|
||||
|
||||
self.sys_host.supported_features.cache_clear()
|
||||
|
@ -107,6 +107,7 @@ class HassOS(CoreSysAttributes):
|
||||
return
|
||||
else:
|
||||
self._available = True
|
||||
self.sys_host.supported_features.cache_clear()
|
||||
|
||||
# Store meta data
|
||||
self._version = cpe.get_version()[0]
|
||||
|
@ -1,6 +1,8 @@
|
||||
"""Host function like audio, D-Bus or systemd."""
|
||||
from contextlib import suppress
|
||||
from functools import lru_cache
|
||||
import logging
|
||||
from typing import List
|
||||
|
||||
from ..const import HostFeature
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
@ -60,7 +62,12 @@ class HostManager(CoreSysAttributes):
|
||||
return self._sound
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
def features(self) -> List[HostFeature]:
|
||||
"""Return a list of host features."""
|
||||
return self.supported_features()
|
||||
|
||||
@lru_cache
|
||||
def supported_features(self) -> List[HostFeature]:
|
||||
"""Return a list of supported host features."""
|
||||
features = []
|
||||
|
||||
@ -95,6 +102,7 @@ class HostManager(CoreSysAttributes):
|
||||
await self.sound.update()
|
||||
|
||||
_LOGGER.info("Host information reload completed")
|
||||
self.supported_features.cache_clear() # pylint: disable=no-member
|
||||
|
||||
async def load(self):
|
||||
"""Load host information."""
|
||||
|
@ -1,7 +1,7 @@
|
||||
"""A collection of tasks."""
|
||||
import logging
|
||||
|
||||
from ..const import AddonState
|
||||
from ..const import AddonState, HostFeature
|
||||
from ..coresys import CoreSysAttributes
|
||||
from ..exceptions import (
|
||||
AddonsError,
|
||||
@ -457,7 +457,7 @@ class Tasks(CoreSysAttributes):
|
||||
# Check connectivity
|
||||
try:
|
||||
await self.sys_supervisor.check_connectivity()
|
||||
if self.sys_dbus.network.is_connected:
|
||||
if HostFeature.NETWORK in self.sys_host.features:
|
||||
await self.sys_host.network.check_connectivity()
|
||||
finally:
|
||||
self._cache["connectivity"] = 0
|
||||
|
@ -26,4 +26,4 @@ class EvaluateNetworkManager(EvaluateBase):
|
||||
|
||||
async def evaluate(self):
|
||||
"""Run evaluation."""
|
||||
return HostFeature.NETWORK not in self.sys_host.supported_features
|
||||
return HostFeature.NETWORK not in self.sys_host.features
|
||||
|
@ -27,7 +27,7 @@ class EvaluateSystemd(EvaluateBase):
|
||||
async def evaluate(self):
|
||||
"""Run evaluation."""
|
||||
return any(
|
||||
feature not in self.sys_host.supported_features
|
||||
feature not in self.sys_host.features
|
||||
for feature in (
|
||||
HostFeature.HOSTNAME,
|
||||
HostFeature.SERVICES,
|
||||
|
@ -4,8 +4,11 @@
|
||||
|
||||
def test_supported_features(coresys):
|
||||
"""Test host features."""
|
||||
assert "network" in coresys.host.supported_features
|
||||
assert "network" in coresys.host.features
|
||||
|
||||
coresys._dbus.network.is_connected = False
|
||||
|
||||
assert "network" not in coresys.host.supported_features
|
||||
assert "network" in coresys.host.features
|
||||
|
||||
coresys.host.supported_features.cache_clear()
|
||||
assert "network" not in coresys.host.features
|
||||
|
@ -19,6 +19,8 @@ async def test_evaluation(coresys: CoreSys):
|
||||
assert network_manager.reason in coresys.resolution.unsupported
|
||||
|
||||
coresys.dbus.network.is_connected = True
|
||||
coresys.host.supported_features.cache_clear()
|
||||
|
||||
await network_manager()
|
||||
assert network_manager.reason not in coresys.resolution.unsupported
|
||||
|
||||
|
@ -16,11 +16,11 @@ async def test_evaluation(coresys: CoreSys):
|
||||
|
||||
coresys._host = MagicMock()
|
||||
|
||||
coresys.host.supported_features = [HostFeature.HOSTNAME]
|
||||
coresys.host.features = [HostFeature.HOSTNAME]
|
||||
await systemd()
|
||||
assert systemd.reason in coresys.resolution.unsupported
|
||||
|
||||
coresys.host.supported_features = [
|
||||
coresys.host.features = [
|
||||
HostFeature.SERVICES,
|
||||
HostFeature.SHUTDOWN,
|
||||
HostFeature.REBOOT,
|
||||
|
Loading…
x
Reference in New Issue
Block a user