Make host feature cheaper to call and relay on it (#2266)

This commit is contained in:
Pascal Vizeli 2020-11-18 14:13:49 +01:00 committed by GitHub
parent 2f9fc39b72
commit 3dd3340e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 27 additions and 11 deletions

View File

@ -43,7 +43,7 @@ class APIHost(CoreSysAttributes):
ATTR_DISK_FREE: self.sys_host.info.free_space, ATTR_DISK_FREE: self.sys_host.info.free_space,
ATTR_DISK_TOTAL: self.sys_host.info.total_space, ATTR_DISK_TOTAL: self.sys_host.info.total_space,
ATTR_DISK_USED: self.sys_host.info.used_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_HOSTNAME: self.sys_host.info.hostname,
ATTR_KERNEL: self.sys_host.info.kernel, ATTR_KERNEL: self.sys_host.info.kernel,
ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system, ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system,

View File

@ -39,7 +39,7 @@ class APIInfo(CoreSysAttributes):
ATTR_DOCKER: self.sys_docker.info.version, ATTR_DOCKER: self.sys_docker.info.version,
ATTR_HOSTNAME: self.sys_host.info.hostname, ATTR_HOSTNAME: self.sys_host.info.hostname,
ATTR_OPERATING_SYSTEM: self.sys_host.info.operating_system, 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_MACHINE: self.sys_machine,
ATTR_ARCH: self.sys_arch.default, ATTR_ARCH: self.sys_arch.default,
ATTR_SUPPORTED_ARCH: self.sys_arch.supported, ATTR_SUPPORTED_ARCH: self.sys_arch.supported,

View File

@ -55,3 +55,5 @@ class DBusManager(CoreSysAttributes):
_LOGGER.error( _LOGGER.error(
"No D-Bus support on Host. Disabled any kind of host control!" "No D-Bus support on Host. Disabled any kind of host control!"
) )
self.sys_host.supported_features.cache_clear()

View File

@ -107,6 +107,7 @@ class HassOS(CoreSysAttributes):
return return
else: else:
self._available = True self._available = True
self.sys_host.supported_features.cache_clear()
# Store meta data # Store meta data
self._version = cpe.get_version()[0] self._version = cpe.get_version()[0]

View File

@ -1,6 +1,8 @@
"""Host function like audio, D-Bus or systemd.""" """Host function like audio, D-Bus or systemd."""
from contextlib import suppress from contextlib import suppress
from functools import lru_cache
import logging import logging
from typing import List
from ..const import HostFeature from ..const import HostFeature
from ..coresys import CoreSys, CoreSysAttributes from ..coresys import CoreSys, CoreSysAttributes
@ -60,7 +62,12 @@ class HostManager(CoreSysAttributes):
return self._sound return self._sound
@property @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.""" """Return a list of supported host features."""
features = [] features = []
@ -95,6 +102,7 @@ class HostManager(CoreSysAttributes):
await self.sound.update() await self.sound.update()
_LOGGER.info("Host information reload completed") _LOGGER.info("Host information reload completed")
self.supported_features.cache_clear() # pylint: disable=no-member
async def load(self): async def load(self):
"""Load host information.""" """Load host information."""

View File

@ -1,7 +1,7 @@
"""A collection of tasks.""" """A collection of tasks."""
import logging import logging
from ..const import AddonState from ..const import AddonState, HostFeature
from ..coresys import CoreSysAttributes from ..coresys import CoreSysAttributes
from ..exceptions import ( from ..exceptions import (
AddonsError, AddonsError,
@ -457,7 +457,7 @@ class Tasks(CoreSysAttributes):
# Check connectivity # Check connectivity
try: try:
await self.sys_supervisor.check_connectivity() 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() await self.sys_host.network.check_connectivity()
finally: finally:
self._cache["connectivity"] = 0 self._cache["connectivity"] = 0

View File

@ -26,4 +26,4 @@ class EvaluateNetworkManager(EvaluateBase):
async def evaluate(self): async def evaluate(self):
"""Run evaluation.""" """Run evaluation."""
return HostFeature.NETWORK not in self.sys_host.supported_features return HostFeature.NETWORK not in self.sys_host.features

View File

@ -27,7 +27,7 @@ class EvaluateSystemd(EvaluateBase):
async def evaluate(self): async def evaluate(self):
"""Run evaluation.""" """Run evaluation."""
return any( return any(
feature not in self.sys_host.supported_features feature not in self.sys_host.features
for feature in ( for feature in (
HostFeature.HOSTNAME, HostFeature.HOSTNAME,
HostFeature.SERVICES, HostFeature.SERVICES,

View File

@ -4,8 +4,11 @@
def test_supported_features(coresys): def test_supported_features(coresys):
"""Test host features.""" """Test host features."""
assert "network" in coresys.host.supported_features assert "network" in coresys.host.features
coresys._dbus.network.is_connected = False 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

View File

@ -19,6 +19,8 @@ async def test_evaluation(coresys: CoreSys):
assert network_manager.reason in coresys.resolution.unsupported assert network_manager.reason in coresys.resolution.unsupported
coresys.dbus.network.is_connected = True coresys.dbus.network.is_connected = True
coresys.host.supported_features.cache_clear()
await network_manager() await network_manager()
assert network_manager.reason not in coresys.resolution.unsupported assert network_manager.reason not in coresys.resolution.unsupported

View File

@ -16,11 +16,11 @@ async def test_evaluation(coresys: CoreSys):
coresys._host = MagicMock() coresys._host = MagicMock()
coresys.host.supported_features = [HostFeature.HOSTNAME] coresys.host.features = [HostFeature.HOSTNAME]
await systemd() await systemd()
assert systemd.reason in coresys.resolution.unsupported assert systemd.reason in coresys.resolution.unsupported
coresys.host.supported_features = [ coresys.host.features = [
HostFeature.SERVICES, HostFeature.SERVICES,
HostFeature.SHUTDOWN, HostFeature.SHUTDOWN,
HostFeature.REBOOT, HostFeature.REBOOT,