improved errors raising for services

This commit is contained in:
Mathieu Broillet 2023-12-30 13:39:42 +01:00
parent b86d710ce3
commit 5e767f8b35
Signed by: mathieu
GPG Key ID: C0E9E0E95AF03319

View File

@ -3,6 +3,7 @@ import re
import fabric2 import fabric2
from fabric2 import Connection from fabric2 import Connection
from homeassistant.exceptions import HomeAssistantError
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -65,6 +66,7 @@ def get_operating_system_version(connection: Connection, is_unix=None):
def get_operating_system(connection: Connection): def get_operating_system(connection: Connection):
"""Return the running operating system type.""" """Return the running operating system type."""
# TODO: might be a better way to do this
result = connection.run("uname") result = connection.run("uname")
if result.return_code == 0: if result.return_code == 0:
return "Linux/Unix" return "Linux/Unix"
@ -88,7 +90,8 @@ def shutdown_system(connection: Connection, is_unix=None):
# Try a third method using systemctl command # Try a third method using systemctl command
result = connection.run("sudo systemctl poweroff") result = connection.run("sudo systemctl poweroff")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot shutdown system running at %s, all methods failed.", connection.host) raise HomeAssistantError(
f"Cannot shutdown system running at {connection.host}, all methods failed.")
else: else:
# First method using shutdown command # First method using shutdown command
@ -97,7 +100,7 @@ def shutdown_system(connection: Connection, is_unix=None):
# Try a second method using init command # Try a second method using init command
result = connection.run("wmic os where Primary=TRUE call Shutdown") result = connection.run("wmic os where Primary=TRUE call Shutdown")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot shutdown system running at %s, all methods failed.", connection.host) raise HomeAssistantError(f"Cannot shutdown system running at {connection.host}, all methods failed.")
connection.close() connection.close()
@ -118,7 +121,7 @@ def restart_system(connection: Connection, is_unix=None):
# Try a third method using systemctl command # Try a third method using systemctl command
result = connection.run("sudo systemctl reboot") result = connection.run("sudo systemctl reboot")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot restart system running at %s, all methods failed.", connection.host) raise HomeAssistantError(f"Cannot restart system running at {connection.host}, all methods failed.")
else: else:
# First method using shutdown command # First method using shutdown command
result = connection.run("shutdown /r /t 0") result = connection.run("shutdown /r /t 0")
@ -126,7 +129,7 @@ def restart_system(connection: Connection, is_unix=None):
# Try a second method using wmic command # Try a second method using wmic command
result = connection.run("wmic os where Primary=TRUE call Reboot") result = connection.run("wmic os where Primary=TRUE call Reboot")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot restart system running at %s, all methods failed.", connection.host) raise HomeAssistantError(f"Cannot restart system running at {connection.host}, all methods failed.")
def sleep_system(connection: Connection, is_unix=None): def sleep_system(connection: Connection, is_unix=None):
@ -142,7 +145,8 @@ def sleep_system(connection: Connection, is_unix=None):
# Try a second method using pm-suspend command # Try a second method using pm-suspend command
result = connection.run("sudo pm-suspend") result = connection.run("sudo pm-suspend")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot put system running at %s to sleep, all methods failed.", connection.host) raise HomeAssistantError(
f"Cannot put system running at {connection.host} to sleep, all methods failed.")
else: else:
# First method using shutdown command # First method using shutdown command
result = connection.run("shutdown /h /t 0") result = connection.run("shutdown /h /t 0")
@ -150,7 +154,8 @@ def sleep_system(connection: Connection, is_unix=None):
# Try a second method using rundll32 command # Try a second method using rundll32 command
result = connection.run("rundll32.exe powrprof.dll,SetSuspendState Sleep") result = connection.run("rundll32.exe powrprof.dll,SetSuspendState Sleep")
if result.return_code != 0: if result.return_code != 0:
_LOGGER.error("Cannot put system running at %s to sleep, all methods failed.", connection.host) raise HomeAssistantError(
f"Cannot put system running at {connection.host} to sleep, all methods failed.")
def get_windows_entry_in_grub(connection: Connection): def get_windows_entry_in_grub(connection: Connection):
@ -187,22 +192,21 @@ def restart_to_windows_from_linux(connection: Connection):
if windows_entry is not None: if windows_entry is not None:
# First method using grub-reboot command # First method using grub-reboot command
result = connection.run(f"sudo grub-reboot \"{windows_entry}\"") result = connection.run(f"sudo grub-reboot \"{windows_entry}\"")
if result.return_code != 0: if result.return_code != 0:
# Try a second method using grub2-reboot command # Try a second method using grub2-reboot command
result = connection.run(f"sudo grub2-reboot \"{windows_entry}\"") result = connection.run(f"sudo grub2-reboot \"{windows_entry}\"")
# Restart system if successful grub(2)-reboot command # Restart system if successful grub(2)-reboot command
if result.return_code == 0: if result.return_code == 0:
_LOGGER.info("Rebooting to Windows") _LOGGER.debug("Rebooting to Windows")
restart_system(connection) restart_system(connection)
else: else:
_LOGGER.error("Could not restart system running on %s to Windows from Linux, all methods failed.", raise HomeAssistantError(
connection.host) f"Could not restart system running on {connection.host} to Windows from Linux, all methods failed.")
else:
raise HomeAssistantError(f"Could not find Windows entry in grub for system running at {connection.host}.")
else: else:
_LOGGER.error( raise HomeAssistantError(f"System running at {connection.host} is not a Linux system.")
"Could not restart system running on %s to Windows from Linux, system does not appear to be a Linux-based OS.",
connection.host)
def change_monitors_config(connection: Connection, monitors_config: dict): def change_monitors_config(connection: Connection, monitors_config: dict):
@ -239,11 +243,10 @@ def change_monitors_config(connection: Connection, monitors_config: dict):
if result.return_code == 0: if result.return_code == 0:
_LOGGER.info("Successfully changed monitors config on system running on %s.", connection.host) _LOGGER.info("Successfully changed monitors config on system running on %s.", connection.host)
else: else:
_LOGGER.error("Could not change monitors config on system running on %s", connection.host) raise HomeAssistantError("Could not change monitors config on system running on %s, check logs with debug",
# TODO : add useful debug info connection.host)
else: else:
_LOGGER.error("Not implemented yet.") raise HomeAssistantError("Not implemented yet for Windows OS.")
return
# Use NIRCMD to change monitors config on Windows # Use NIRCMD to change monitors config on Windows
# setdisplay {monitor:index/name} [width] [height] [color bits] {refresh rate} {-updatereg} {-allusers} # setdisplay {monitor:index/name} [width] [height] [color bits] {refresh rate} {-updatereg} {-allusers}