From 784c5d3b7cdc0874c9ca3e8076300c1175e9f338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Fri, 13 Nov 2020 00:07:05 +0100 Subject: [PATCH] Use bool for host internet (#2247) * Use bool for host internet * Ignore host check if no network manager * Update supervisor/host/network.py Co-authored-by: Pascal Vizeli * Check dbus connection isntead of supported features Co-authored-by: Pascal Vizeli --- supervisor/api/network.py | 4 ++-- supervisor/const.py | 2 +- supervisor/core.py | 4 +--- supervisor/host/const.py | 10 ---------- supervisor/host/network.py | 24 ++++++++++++------------ supervisor/job/decorator.py | 7 +++---- tests/conftest.py | 3 +-- tests/host/test_connectivity.py | 30 ++++-------------------------- tests/job/test_job_decorator.py | 12 ++++++++++++ 9 files changed, 36 insertions(+), 60 deletions(-) diff --git a/supervisor/api/network.py b/supervisor/api/network.py index e0808eb29..d0e78bd5c 100644 --- a/supervisor/api/network.py +++ b/supervisor/api/network.py @@ -17,7 +17,7 @@ from ..const import ( ATTR_ENABLED, ATTR_FREQUENCY, ATTR_GATEWAY, - ATTR_HOST_CONNECTIVITY, + ATTR_HOST_INTERNET, ATTR_INTERFACE, ATTR_INTERFACES, ATTR_IPV4, @@ -161,7 +161,7 @@ class APINetwork(CoreSysAttributes): ATTR_GATEWAY: str(self.sys_docker.network.gateway), ATTR_DNS: str(self.sys_docker.network.dns), }, - ATTR_HOST_CONNECTIVITY: self.sys_host.network.connectivity, + ATTR_HOST_INTERNET: self.sys_host.network.connectivity, ATTR_SUPERVISOR_INTERNET: self.sys_supervisor.connectivity, } diff --git a/supervisor/const.py b/supervisor/const.py index 73378fd20..678d3f262 100644 --- a/supervisor/const.py +++ b/supervisor/const.py @@ -151,8 +151,8 @@ ATTR_HEALTHY = "healthy" ATTR_HOMEASSISTANT = "homeassistant" ATTR_HOMEASSISTANT_API = "homeassistant_api" ATTR_HOST = "host" -ATTR_HOST_CONNECTIVITY = "host_connectivity" ATTR_HOST_DBUS = "host_dbus" +ATTR_HOST_INTERNET = "host_internet" ATTR_HOST_IPC = "host_ipc" ATTR_HOST_NETWORK = "host_network" ATTR_HOST_PID = "host_pid" diff --git a/supervisor/core.py b/supervisor/core.py index 6ae9d9575..3a592092b 100644 --- a/supervisor/core.py +++ b/supervisor/core.py @@ -6,8 +6,6 @@ from typing import Awaitable, List, Optional import async_timeout -from supervisor.host.const import ConnectivityState - from .const import RUN_SUPERVISOR_STATE, AddonStartup, CoreState from .coresys import CoreSys, CoreSysAttributes from .exceptions import ( @@ -60,7 +58,7 @@ class Core(CoreSysAttributes): await self.sys_supervisor.load() # Check internet on startup - if not self.sys_host.network.connectivity == ConnectivityState.FULL: + if not self.sys_host.network.connectivity: await self.sys_host.network.check_connectivity() if not self.sys_supervisor.connectivity: diff --git a/supervisor/host/const.py b/supervisor/host/const.py index d078567ea..d8762c380 100644 --- a/supervisor/host/const.py +++ b/supervisor/host/const.py @@ -33,13 +33,3 @@ class WifiMode(str, Enum): MESH = "mesh" ADHOC = "adhoc" AP = "ap" - - -class ConnectivityState(int, Enum): - """Connectivity State.""" - - UNKNOWN = 0 - NONE = 1 - PORTAL = 2 - LIMITED = 3 - FULL = 4 diff --git a/supervisor/host/network.py b/supervisor/host/network.py index b54c040a8..5323bb646 100644 --- a/supervisor/host/network.py +++ b/supervisor/host/network.py @@ -27,13 +27,7 @@ from ..exceptions import ( HostNetworkNotFound, HostNotSupportedError, ) -from .const import ( - AuthMethod, - ConnectivityState, - InterfaceMethod, - InterfaceType, - WifiMode, -) +from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode _LOGGER: logging.Logger = logging.getLogger(__name__) @@ -44,10 +38,10 @@ class NetworkManager(CoreSysAttributes): def __init__(self, coresys: CoreSys): """Initialize system center handling.""" self.coresys: CoreSys = coresys - self._connectivity = ConnectivityState.FULL + self._connectivity: bool = None @property - def connectivity(self) -> ConnectivityState: + def connectivity(self) -> bool: """Return true current connectivity state.""" return self._connectivity @@ -74,11 +68,17 @@ class NetworkManager(CoreSysAttributes): async def check_connectivity(self): """Check the internet connection.""" + if not self.sys_dbus.network.is_connected: + self._connectivity = None + return try: state = await self.sys_dbus.network.check_connectivity() - self._connectivity = ConnectivityState(state[0]) - except (DBusError, IndexError): - self._connectivity = ConnectivityState.UNKNOWN + + # ConnectionState 4 == FULL (has internet) + # https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMConnectivityState + self._connectivity = state[0] == 4 + except DBusError: + self._connectivity = False def get(self, inet_name: str) -> Interface: """Return interface from interface name.""" diff --git a/supervisor/job/decorator.py b/supervisor/job/decorator.py index 1c833110b..02e76cac4 100644 --- a/supervisor/job/decorator.py +++ b/supervisor/job/decorator.py @@ -6,7 +6,6 @@ from typing import List, Optional from ..const import CoreState from ..coresys import CoreSys from ..exceptions import HassioError, JobException -from ..host.const import ConnectivityState from ..resolution.const import MINIMUM_FREE_SPACE_THRESHOLD, ContextType, IssueType _LOGGER: logging.Logger = logging.getLogger(__package__) @@ -97,9 +96,9 @@ class Job: if self._coresys.core.state == CoreState.RUNNING: await self._coresys.host.network.check_connectivity() await self._coresys.supervisor.check_connectivity() - if ( - not self._coresys.supervisor.connectivity - or self._coresys.host.network.connectivity != ConnectivityState.FULL + if not self._coresys.supervisor.connectivity or ( + self._coresys.host.network.connectivity is not None + and not self._coresys.host.network.connectivity ): _LOGGER.warning( "'%s' blocked from execution, no internet connection", diff --git a/tests/conftest.py b/tests/conftest.py index 4268acca3..0c86af4a3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,6 @@ from supervisor.bootstrap import initialize_coresys from supervisor.coresys import CoreSys from supervisor.dbus.network import NetworkManager from supervisor.docker import DockerAPI -from supervisor.host.const import ConnectivityState from supervisor.utils.gdbus import DBus from tests.common import exists_fixture, load_fixture, load_json_fixture @@ -139,7 +138,7 @@ async def coresys(loop, docker, network_manager, aiohttp_client) -> CoreSys: # Set internet state coresys_obj.supervisor._connectivity = True - coresys_obj.host.network._connectivity = ConnectivityState.FULL + coresys_obj.host.network._connectivity = True yield coresys_obj diff --git a/tests/host/test_connectivity.py b/tests/host/test_connectivity.py index ce8e4deeb..f252a7462 100644 --- a/tests/host/test_connectivity.py +++ b/tests/host/test_connectivity.py @@ -3,39 +3,17 @@ from unittest.mock import patch from supervisor.coresys import CoreSys -from supervisor.host.const import ConnectivityState -async def test_connectivity_unknown(coresys: CoreSys): +async def test_connectivity_not_connected(coresys: CoreSys): """Test host unknown connectivity.""" with patch("supervisor.utils.gdbus.DBus._send", return_value="[0]"): await coresys.host.network.check_connectivity() - assert coresys.host.network.connectivity == ConnectivityState.UNKNOWN + assert not coresys.host.network.connectivity -async def test_connectivity_none(coresys: CoreSys): - """Test host none connectivity.""" - with patch("supervisor.utils.gdbus.DBus._send", return_value="[1]"): - await coresys.host.network.check_connectivity() - assert coresys.host.network.connectivity == ConnectivityState.NONE - - -async def test_connectivity_portal(coresys: CoreSys): - """Test host portal connectivity.""" - with patch("supervisor.utils.gdbus.DBus._send", return_value="[2]"): - await coresys.host.network.check_connectivity() - assert coresys.host.network.connectivity == ConnectivityState.PORTAL - - -async def test_connectivity_limited(coresys: CoreSys): - """Test host limited connectivity.""" - with patch("supervisor.utils.gdbus.DBus._send", return_value="[3]"): - await coresys.host.network.check_connectivity() - assert coresys.host.network.connectivity == ConnectivityState.LIMITED - - -async def test_connectivity_full(coresys: CoreSys): +async def test_connectivity_connected(coresys: CoreSys): """Test host full connectivity.""" with patch("supervisor.utils.gdbus.DBus._send", return_value="[4]"): await coresys.host.network.check_connectivity() - assert coresys.host.network.connectivity == ConnectivityState.FULL + assert coresys.host.network.connectivity diff --git a/tests/job/test_job_decorator.py b/tests/job/test_job_decorator.py index e8b20fa4d..2a298cd20 100644 --- a/tests/job/test_job_decorator.py +++ b/tests/job/test_job_decorator.py @@ -44,11 +44,23 @@ async def test_internet(coresys: CoreSys): return True test = TestClass(coresys) + + coresys.host.network._connectivity = True + coresys.supervisor._connectivity = True assert await test.execute() + coresys.host.network._connectivity = True coresys.supervisor._connectivity = False assert not await test.execute() + coresys.host.network._connectivity = None + coresys.supervisor._connectivity = True + assert await test.execute() + + coresys.host.network._connectivity = False + coresys.supervisor._connectivity = True + assert not await test.execute() + async def test_free_space(coresys: CoreSys): """Test the free_space decorator."""