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_RESTART_TO_WINDOWS_FROM_LINUX = "restart_to_windows_from_linux"
SERVICE_PUT_COMPUTER_TO_SLEEP = "put_computer_to_sleep" SERVICE_PUT_COMPUTER_TO_SLEEP = "put_computer_to_sleep"
SERVICE_START_COMPUTER_TO_WINDOWS = "start_computer_to_windows" SERVICE_START_COMPUTER_TO_WINDOWS = "start_computer_to_windows"
RESTART_COMPUTER = "restart_computer"

View File

@ -38,6 +38,12 @@ start_computer_to_windows:
put_computer_to_sleep: put_computer_to_sleep:
name: Put computer to sleep name: Put computer to sleep
description: Put the computer to sleep. description: Put the computer to sleep.
target:
device:
integration: easy_computer_manage
restart_computer:
name: Restart
description: Restart the computer.
target: target:
device: device:
integration: easy_computer_manage integration: easy_computer_manage

View File

@ -9,12 +9,11 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
import wakeonlan import wakeonlan
from paramiko.ssh_exception import NoValidConnectionsError, SSHException, AuthenticationException
from homeassistant.components.switch import ( from homeassistant.components.switch import (
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA, PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SwitchEntity, SwitchEntity,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_BROADCAST_ADDRESS, CONF_BROADCAST_ADDRESS,
CONF_BROADCAST_PORT, CONF_BROADCAST_PORT,
@ -32,10 +31,11 @@ from homeassistant.helpers import (
entity_platform, entity_platform,
) )
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from paramiko.ssh_exception import AuthenticationException
from . import utils from . import utils
from .const import SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, SERVICE_PUT_COMPUTER_TO_SLEEP, \ from .const import SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, SERVICE_PUT_COMPUTER_TO_SLEEP, \
SERVICE_START_COMPUTER_TO_WINDOWS SERVICE_START_COMPUTER_TO_WINDOWS, RESTART_COMPUTER
from homeassistant.config_entries import ConfigEntry
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -108,6 +108,11 @@ async def async_setup_entry(
{}, {},
SERVICE_START_COMPUTER_TO_WINDOWS, SERVICE_START_COMPUTER_TO_WINDOWS,
) )
platform.async_register_entity_service(
RESTART_COMPUTER,
{},
RESTART_COMPUTER,
)
class ComputerSwitch(SwitchEntity): class ComputerSwitch(SwitchEntity):
@ -197,19 +202,30 @@ class ComputerSwitch(SwitchEntity):
if self._dualboot: if self._dualboot:
# Wait for the computer to boot using a dedicated thread to avoid blocking the main thread # Wait for the computer to boot using a dedicated thread to avoid blocking the main thread
self._hass.loop.create_task(self.service_restart_to_windows_from_linux())
self._hass.loop.create_task(self.restart_computer_to_windows_when_on())
else: else:
_LOGGER.error("This computer is not running a dualboot system.") _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.""" """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: while not self.is_on:
await asyncio.sleep(3) await asyncio.sleep(3)
await utils.restart_to_windows_from_linux(self._connection) 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: def update(self) -> None:
"""Ping the computer to see if it is online and update the state.""" """Ping the computer to see if it is online and update the state."""
ping_cmd = [ ping_cmd = [