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 <pvizeli@syshack.ch>

* Check dbus connection isntead of supported features

Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
This commit is contained in:
Joakim Sørensen 2020-11-13 00:07:05 +01:00 committed by GitHub
parent a18b706f99
commit 784c5d3b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 36 additions and 60 deletions

View File

@ -17,7 +17,7 @@ from ..const import (
ATTR_ENABLED, ATTR_ENABLED,
ATTR_FREQUENCY, ATTR_FREQUENCY,
ATTR_GATEWAY, ATTR_GATEWAY,
ATTR_HOST_CONNECTIVITY, ATTR_HOST_INTERNET,
ATTR_INTERFACE, ATTR_INTERFACE,
ATTR_INTERFACES, ATTR_INTERFACES,
ATTR_IPV4, ATTR_IPV4,
@ -161,7 +161,7 @@ class APINetwork(CoreSysAttributes):
ATTR_GATEWAY: str(self.sys_docker.network.gateway), ATTR_GATEWAY: str(self.sys_docker.network.gateway),
ATTR_DNS: str(self.sys_docker.network.dns), 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, ATTR_SUPERVISOR_INTERNET: self.sys_supervisor.connectivity,
} }

View File

@ -151,8 +151,8 @@ ATTR_HEALTHY = "healthy"
ATTR_HOMEASSISTANT = "homeassistant" ATTR_HOMEASSISTANT = "homeassistant"
ATTR_HOMEASSISTANT_API = "homeassistant_api" ATTR_HOMEASSISTANT_API = "homeassistant_api"
ATTR_HOST = "host" ATTR_HOST = "host"
ATTR_HOST_CONNECTIVITY = "host_connectivity"
ATTR_HOST_DBUS = "host_dbus" ATTR_HOST_DBUS = "host_dbus"
ATTR_HOST_INTERNET = "host_internet"
ATTR_HOST_IPC = "host_ipc" ATTR_HOST_IPC = "host_ipc"
ATTR_HOST_NETWORK = "host_network" ATTR_HOST_NETWORK = "host_network"
ATTR_HOST_PID = "host_pid" ATTR_HOST_PID = "host_pid"

View File

@ -6,8 +6,6 @@ from typing import Awaitable, List, Optional
import async_timeout import async_timeout
from supervisor.host.const import ConnectivityState
from .const import RUN_SUPERVISOR_STATE, AddonStartup, CoreState from .const import RUN_SUPERVISOR_STATE, AddonStartup, CoreState
from .coresys import CoreSys, CoreSysAttributes from .coresys import CoreSys, CoreSysAttributes
from .exceptions import ( from .exceptions import (
@ -60,7 +58,7 @@ class Core(CoreSysAttributes):
await self.sys_supervisor.load() await self.sys_supervisor.load()
# Check internet on startup # 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() await self.sys_host.network.check_connectivity()
if not self.sys_supervisor.connectivity: if not self.sys_supervisor.connectivity:

View File

@ -33,13 +33,3 @@ class WifiMode(str, Enum):
MESH = "mesh" MESH = "mesh"
ADHOC = "adhoc" ADHOC = "adhoc"
AP = "ap" AP = "ap"
class ConnectivityState(int, Enum):
"""Connectivity State."""
UNKNOWN = 0
NONE = 1
PORTAL = 2
LIMITED = 3
FULL = 4

View File

@ -27,13 +27,7 @@ from ..exceptions import (
HostNetworkNotFound, HostNetworkNotFound,
HostNotSupportedError, HostNotSupportedError,
) )
from .const import ( from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode
AuthMethod,
ConnectivityState,
InterfaceMethod,
InterfaceType,
WifiMode,
)
_LOGGER: logging.Logger = logging.getLogger(__name__) _LOGGER: logging.Logger = logging.getLogger(__name__)
@ -44,10 +38,10 @@ class NetworkManager(CoreSysAttributes):
def __init__(self, coresys: CoreSys): def __init__(self, coresys: CoreSys):
"""Initialize system center handling.""" """Initialize system center handling."""
self.coresys: CoreSys = coresys self.coresys: CoreSys = coresys
self._connectivity = ConnectivityState.FULL self._connectivity: bool = None
@property @property
def connectivity(self) -> ConnectivityState: def connectivity(self) -> bool:
"""Return true current connectivity state.""" """Return true current connectivity state."""
return self._connectivity return self._connectivity
@ -74,11 +68,17 @@ class NetworkManager(CoreSysAttributes):
async def check_connectivity(self): async def check_connectivity(self):
"""Check the internet connection.""" """Check the internet connection."""
if not self.sys_dbus.network.is_connected:
self._connectivity = None
return
try: try:
state = await self.sys_dbus.network.check_connectivity() state = await self.sys_dbus.network.check_connectivity()
self._connectivity = ConnectivityState(state[0])
except (DBusError, IndexError): # ConnectionState 4 == FULL (has internet)
self._connectivity = ConnectivityState.UNKNOWN # 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: def get(self, inet_name: str) -> Interface:
"""Return interface from interface name.""" """Return interface from interface name."""

View File

@ -6,7 +6,6 @@ from typing import List, Optional
from ..const import CoreState from ..const import CoreState
from ..coresys import CoreSys from ..coresys import CoreSys
from ..exceptions import HassioError, JobException from ..exceptions import HassioError, JobException
from ..host.const import ConnectivityState
from ..resolution.const import MINIMUM_FREE_SPACE_THRESHOLD, ContextType, IssueType from ..resolution.const import MINIMUM_FREE_SPACE_THRESHOLD, ContextType, IssueType
_LOGGER: logging.Logger = logging.getLogger(__package__) _LOGGER: logging.Logger = logging.getLogger(__package__)
@ -97,9 +96,9 @@ class Job:
if self._coresys.core.state == CoreState.RUNNING: if self._coresys.core.state == CoreState.RUNNING:
await self._coresys.host.network.check_connectivity() await self._coresys.host.network.check_connectivity()
await self._coresys.supervisor.check_connectivity() await self._coresys.supervisor.check_connectivity()
if ( if not self._coresys.supervisor.connectivity or (
not self._coresys.supervisor.connectivity self._coresys.host.network.connectivity is not None
or self._coresys.host.network.connectivity != ConnectivityState.FULL and not self._coresys.host.network.connectivity
): ):
_LOGGER.warning( _LOGGER.warning(
"'%s' blocked from execution, no internet connection", "'%s' blocked from execution, no internet connection",

View File

@ -13,7 +13,6 @@ from supervisor.bootstrap import initialize_coresys
from supervisor.coresys import CoreSys from supervisor.coresys import CoreSys
from supervisor.dbus.network import NetworkManager from supervisor.dbus.network import NetworkManager
from supervisor.docker import DockerAPI from supervisor.docker import DockerAPI
from supervisor.host.const import ConnectivityState
from supervisor.utils.gdbus import DBus from supervisor.utils.gdbus import DBus
from tests.common import exists_fixture, load_fixture, load_json_fixture 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 # Set internet state
coresys_obj.supervisor._connectivity = True coresys_obj.supervisor._connectivity = True
coresys_obj.host.network._connectivity = ConnectivityState.FULL coresys_obj.host.network._connectivity = True
yield coresys_obj yield coresys_obj

View File

@ -3,39 +3,17 @@
from unittest.mock import patch from unittest.mock import patch
from supervisor.coresys import CoreSys 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.""" """Test host unknown connectivity."""
with patch("supervisor.utils.gdbus.DBus._send", return_value="[0]"): with patch("supervisor.utils.gdbus.DBus._send", return_value="[0]"):
await coresys.host.network.check_connectivity() 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): async def test_connectivity_connected(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):
"""Test host full connectivity.""" """Test host full connectivity."""
with patch("supervisor.utils.gdbus.DBus._send", return_value="[4]"): with patch("supervisor.utils.gdbus.DBus._send", return_value="[4]"):
await coresys.host.network.check_connectivity() await coresys.host.network.check_connectivity()
assert coresys.host.network.connectivity == ConnectivityState.FULL assert coresys.host.network.connectivity

View File

@ -44,11 +44,23 @@ async def test_internet(coresys: CoreSys):
return True return True
test = TestClass(coresys) test = TestClass(coresys)
coresys.host.network._connectivity = True
coresys.supervisor._connectivity = True
assert await test.execute() assert await test.execute()
coresys.host.network._connectivity = True
coresys.supervisor._connectivity = False coresys.supervisor._connectivity = False
assert not await test.execute() 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): async def test_free_space(coresys: CoreSys):
"""Test the free_space decorator.""" """Test the free_space decorator."""