add restart computer service

This commit is contained in:
Mathieu B 2023-04-06 19:13:14 +02:00
parent 7bf802c82f
commit 3752673d92
3 changed files with 30 additions and 7 deletions

View File

@ -5,3 +5,4 @@ SERVICE_SEND_MAGIC_PACKET = "send_magic_packet"
SERVICE_RESTART_TO_WINDOWS_FROM_LINUX = "restart_to_windows_from_linux"
SERVICE_PUT_COMPUTER_TO_SLEEP = "put_computer_to_sleep"
SERVICE_START_COMPUTER_TO_WINDOWS = "start_computer_to_windows"
RESTART_COMPUTER = "restart_computer"

View File

@ -41,3 +41,9 @@ put_computer_to_sleep:
target:
device:
integration: easy_computer_manage
restart_computer:
name: Restart
description: Restart the computer.
target:
device:
integration: easy_computer_manage

View File

@ -9,12 +9,11 @@ from typing import Any
import voluptuous as vol
import wakeonlan
from paramiko.ssh_exception import NoValidConnectionsError, SSHException, AuthenticationException
from homeassistant.components.switch import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SwitchEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_BROADCAST_ADDRESS,
CONF_BROADCAST_PORT,
@ -32,10 +31,11 @@ from homeassistant.helpers import (
entity_platform,
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from paramiko.ssh_exception import AuthenticationException
from . import utils
from .const import SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, SERVICE_PUT_COMPUTER_TO_SLEEP, \
SERVICE_START_COMPUTER_TO_WINDOWS
from homeassistant.config_entries import ConfigEntry
SERVICE_START_COMPUTER_TO_WINDOWS, RESTART_COMPUTER
_LOGGER = logging.getLogger(__name__)
@ -108,6 +108,11 @@ async def async_setup_entry(
{},
SERVICE_START_COMPUTER_TO_WINDOWS,
)
platform.async_register_entity_service(
RESTART_COMPUTER,
{},
RESTART_COMPUTER,
)
class ComputerSwitch(SwitchEntity):
@ -197,19 +202,30 @@ class ComputerSwitch(SwitchEntity):
if self._dualboot:
# Wait for the computer to boot using a dedicated thread to avoid blocking the main thread
self._hass.loop.create_task(self.restart_computer_to_windows_when_on())
self._hass.loop.create_task(self.service_restart_to_windows_from_linux())
else:
_LOGGER.error("This computer is not running a dualboot system.")
async def restart_computer_to_windows_when_on(self) -> None:
async def service_restart_to_windows_from_linux(self) -> None:
"""Method to be run in a separate thread to wait for the computer to boot and then reboot to Windows."""
while not self.is_on:
await asyncio.sleep(3)
await utils.restart_to_windows_from_linux(self._connection)
def restart_computer(self) -> None:
"""Restart the computer using appropriate restart command based on running OS and/or distro."""
# TODO: check for default grub entry and adapt accordingly
if self._dualboot and not utils.is_unix_system(connection=self._connection):
utils.restart_system(self._connection)
# Wait for the computer to boot using a dedicated thread to avoid blocking the main thread
self.restart_to_windows_from_linux()
else:
utils.restart_system(self._connection)
def update(self) -> None:
"""Ping the computer to see if it is online and update the state."""
ping_cmd = [