Set connectivity state on network tasks (#2686)

* Set connectivity state on network tasks

* target connectivity error
This commit is contained in:
Joakim Sørensen 2021-03-07 11:47:46 +01:00 committed by GitHub
parent 2b411b0bf9
commit 681a1ecff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 8 deletions

View File

@ -303,6 +303,10 @@ class PwnedError(HassioError):
"""Errors while checking pwned passwords."""
class PwnedConnectivityError(PwnedError):
"""Connectivity errors while checking pwned passwords."""
# docker/api

View File

@ -89,6 +89,7 @@ class HassOS(CoreSysAttributes):
return raucb
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
self.sys_supervisor.connectivity = False
_LOGGER.warning("Can't fetch versions from %s: %s", url, err)
except OSError as err:

View File

@ -8,6 +8,7 @@ from typing import List, Optional, Union
import attr
from ..const import ATTR_HOST_INTERNET
from ..coresys import CoreSys, CoreSysAttributes
from ..dbus.const import (
DBUS_NAME_NM_CONNECTION_ACTIVE_CHANGED,
@ -46,6 +47,16 @@ class NetworkManager(CoreSysAttributes):
"""Return true current connectivity state."""
return self._connectivity
@connectivity.setter
def connectivity(self, state: bool) -> None:
"""Set host connectivity state."""
if self._connectivity == state:
return
self._connectivity = state
self.sys_homeassistant.websocket.supervisor_update_event(
"network", {ATTR_HOST_INTERNET: state}
)
@property
def interfaces(self) -> List[Interface]:
"""Return a dictionary of active interfaces."""
@ -79,10 +90,10 @@ class NetworkManager(CoreSysAttributes):
# Check connectivity
try:
state = await self.sys_dbus.network.check_connectivity()
self._connectivity = state[0] == 4
self.connectivity = state[0] == 4
except DBusError as err:
_LOGGER.warning("Can't update connectivity information: %s", err)
self._connectivity = False
self.connectivity = False
def get(self, inet_name: str) -> Interface:
"""Return interface from interface name."""

View File

@ -4,7 +4,7 @@ from datetime import timedelta
from typing import List, Optional
from ...const import AddonState, CoreState
from ...exceptions import PwnedError
from ...exceptions import PwnedConnectivityError, PwnedError
from ...jobs.const import JobCondition, JobExecutionLimit
from ...jobs.decorator import Job
from ...utils.pwned import check_pwned_password
@ -34,6 +34,9 @@ class CheckAddonPwned(CheckBase):
try:
if not await check_pwned_password(self.sys_websession, secret):
continue
except PwnedConnectivityError:
self.sys_supervisor.connectivity = False
continue
except PwnedError:
continue

View File

@ -13,7 +13,7 @@ from awesomeversion import AwesomeVersion, AwesomeVersionException
from supervisor.jobs.decorator import Job, JobCondition
from .const import SUPERVISOR_VERSION, URL_HASSIO_APPARMOR
from .const import ATTR_SUPERVISOR_INTERNET, SUPERVISOR_VERSION, URL_HASSIO_APPARMOR
from .coresys import CoreSys, CoreSysAttributes
from .docker.stats import DockerStats
from .docker.supervisor import DockerSupervisor
@ -53,6 +53,16 @@ class Supervisor(CoreSysAttributes):
"""Return true if we are connected to the internet."""
return self._connectivity
@connectivity.setter
def connectivity(self, state: bool) -> None:
"""Set supervisor connectivity state."""
if self._connectivity == state:
return
self._connectivity = state
self.sys_homeassistant.websocket.supervisor_update_event(
"network", {ATTR_SUPERVISOR_INTERNET: state}
)
@property
def ip_address(self) -> IPv4Address:
"""Return IP of Supervisor instance."""
@ -98,6 +108,7 @@ class Supervisor(CoreSysAttributes):
data = await request.text()
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
self.sys_supervisor.connectivity = False
_LOGGER.warning("Can't fetch AppArmor profile: %s", err)
raise SupervisorError() from err
@ -198,6 +209,6 @@ class Supervisor(CoreSysAttributes):
"https://version.home-assistant.io/online.txt", timeout=timeout
)
except (ClientError, asyncio.TimeoutError):
self._connectivity = False
self.connectivity = False
else:
self._connectivity = True
self.connectivity = True

View File

@ -186,6 +186,7 @@ class Updater(FileConfiguration, CoreSysAttributes):
data = await request.json(content_type=None)
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
self.sys_supervisor.connectivity = False
_LOGGER.warning("Can't fetch versions from %s: %s", url, err)
raise UpdaterError() from err

View File

@ -5,7 +5,7 @@ import logging
import aiohttp
from ..exceptions import PwnedError
from ..exceptions import PwnedConnectivityError, PwnedError
_LOGGER: logging.Logger = logging.getLogger(__name__)
_API_CALL = "https://api.pwnedpasswords.com/range/{hash}"
@ -30,6 +30,6 @@ async def check_pwned_password(websession: aiohttp.ClientSession, sha1_pw: str)
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
_LOGGER.warning("Can't fetch hibp data: %s", err)
raise PwnedError() from err
raise PwnedConnectivityError() from err
return False