mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-12 11:46:31 +00:00
Fix error handling
This commit is contained in:
parent
b486883ff6
commit
e5e25c895f
@ -39,8 +39,8 @@ class Systemd:
|
|||||||
return self.dbus.Manager.Reboot()
|
return self.dbus.Manager.Reboot()
|
||||||
|
|
||||||
@dbus_connected
|
@dbus_connected
|
||||||
def shutdown(self):
|
def power_off(self):
|
||||||
"""Shutdown host computer.
|
"""Power off host computer.
|
||||||
|
|
||||||
Return a coroutine.
|
Return a coroutine.
|
||||||
"""
|
"""
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Utils for dbus."""
|
"""Utils for dbus."""
|
||||||
|
|
||||||
from ..exceptions import HassioNotSupportedError
|
from ..exceptions import DBusNotConnectedError
|
||||||
|
|
||||||
|
|
||||||
def dbus_connected(method):
|
def dbus_connected(method):
|
||||||
@ -8,7 +8,7 @@ def dbus_connected(method):
|
|||||||
def wrap_dbus(self, *args, **kwargs):
|
def wrap_dbus(self, *args, **kwargs):
|
||||||
"""Check if dbus is connected before call a method."""
|
"""Check if dbus is connected before call a method."""
|
||||||
if self.dbus is None:
|
if self.dbus is None:
|
||||||
raise HassioNotSupportedError(f"{self!s} not connected to dbus!")
|
raise DBusNotConnectedError(f"{self!s} not connected to dbus!")
|
||||||
return self.method(*args, **kwargs)
|
return self.method(*args, **kwargs)
|
||||||
|
|
||||||
return wrap_dbus
|
return wrap_dbus
|
||||||
|
@ -16,6 +16,18 @@ class HassioNotSupportedError(HassioError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Host
|
||||||
|
|
||||||
|
class HostError(HassioError):
|
||||||
|
"""Internal Host error."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class HostNotSupportedError(HassioNotSupportedError):
|
||||||
|
"""Host function is not supprted."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# utils/gdbus
|
# utils/gdbus
|
||||||
|
|
||||||
class DBusError(HassioError):
|
class DBusError(HassioError):
|
||||||
@ -23,16 +35,15 @@ class DBusError(HassioError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DBusNotConnectedError(HassioNotSupportedError):
|
||||||
|
"""DBus is not connected and call a method."""
|
||||||
|
|
||||||
|
|
||||||
class DBusFatalError(DBusError):
|
class DBusFatalError(DBusError):
|
||||||
"""DBus call going wrong."""
|
"""DBus call going wrong."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DBusReturnError(DBusError):
|
|
||||||
"""DBus return error."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class DBusParseError(DBusError):
|
class DBusParseError(DBusError):
|
||||||
"""DBus parse error."""
|
"""DBus parse error."""
|
||||||
pass
|
pass
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
"""Power control for host."""
|
"""Power control for host."""
|
||||||
|
import logging
|
||||||
|
|
||||||
from ..coresys import CoreSysAttributes
|
from ..coresys import CoreSysAttributes
|
||||||
from ..exceptions import HassioNotSupportedError
|
from ..exceptions import HostNotSupportedError
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class PowerControl(CoreSysAttributes):
|
class PowerControl(CoreSysAttributes):
|
||||||
@ -14,11 +17,14 @@ class PowerControl(CoreSysAttributes):
|
|||||||
def _check_systemd(self):
|
def _check_systemd(self):
|
||||||
"""Check if systemd is connect or raise error."""
|
"""Check if systemd is connect or raise error."""
|
||||||
if not self.sys_dbus.systemd.is_connected:
|
if not self.sys_dbus.systemd.is_connected:
|
||||||
raise HassioNotSupportedError("No systemd connections")
|
_LOGGER.error("No systemd dbus connection available")
|
||||||
|
raise HostNotSupportedError()
|
||||||
|
|
||||||
async def reboot(self):
|
async def reboot(self):
|
||||||
"""Reboot host system."""
|
"""Reboot host system."""
|
||||||
self._check_systemd()
|
self._check_systemd()
|
||||||
|
|
||||||
|
_LOGGER.info("Initialize host reboot over systemd")
|
||||||
try:
|
try:
|
||||||
await self.sys_core.shutdown()
|
await self.sys_core.shutdown()
|
||||||
finally:
|
finally:
|
||||||
@ -27,7 +33,9 @@ class PowerControl(CoreSysAttributes):
|
|||||||
async def shutdown(self):
|
async def shutdown(self):
|
||||||
"""Shutdown host system."""
|
"""Shutdown host system."""
|
||||||
self._check_systemd()
|
self._check_systemd()
|
||||||
|
|
||||||
|
_LOGGER.info("Initialize host power off over systemd")
|
||||||
try:
|
try:
|
||||||
await self.sys_core.shutdown()
|
await self.sys_core.shutdown()
|
||||||
finally:
|
finally:
|
||||||
await self.sys_dbus.systemd.shutdown()
|
await self.sys_dbus.systemd.power_off()
|
||||||
|
@ -4,7 +4,7 @@ import logging
|
|||||||
import shlex
|
import shlex
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
from ..exceptions import DBusFatalError, DBusReturnError, DBusParseError
|
from ..exceptions import DBusFatalError, DBusFatalError, DBusParseError
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -47,8 +47,8 @@ class DBus:
|
|||||||
try:
|
try:
|
||||||
xml = ET.fromstring(data)
|
xml = ET.fromstring(data)
|
||||||
except ET.ParseError as err:
|
except ET.ParseError as err:
|
||||||
raise DBusParseError(
|
_LOGGER.error("Can't parse introspect data: %s", err)
|
||||||
f"Can't parse introspect data: {err!s}") from None
|
raise DBusParseError() from None
|
||||||
|
|
||||||
# Read available methods
|
# Read available methods
|
||||||
for interface in xml.findall("/node/interface"):
|
for interface in xml.findall("/node/interface"):
|
||||||
@ -91,11 +91,13 @@ class DBus:
|
|||||||
|
|
||||||
data, _ = await proc.communicate()
|
data, _ = await proc.communicate()
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
raise DBusFatalError(f"DBus fatal error: {err!s}") from None
|
_LOGGER.error("DBus fatal error: %s", err)
|
||||||
|
raise DBusFatalError() from None
|
||||||
|
|
||||||
# Success?
|
# Success?
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
raise DBusReturnError(f"DBus return error: {data!s}")
|
_LOGGER.error("DBus return error: %s", data)
|
||||||
|
raise DBusFatalError()
|
||||||
|
|
||||||
# End
|
# End
|
||||||
return data.decode()
|
return data.decode()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user