2023-12-30 20:21:37 +01:00
|
|
|
# Some snippets of code are from the official wake_on_lan integration (inspiration for this custom component)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2023-12-31 17:30:27 +01:00
|
|
|
from homeassistant.components.switch import (SwitchEntity)
|
2023-04-06 19:13:14 +02:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-04-04 13:44:07 +02:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MAC,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
2023-12-30 13:23:46 +01:00
|
|
|
CONF_USERNAME, )
|
2023-12-30 21:20:42 +01:00
|
|
|
from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse
|
2023-04-04 13:44:07 +02:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
device_registry as dr,
|
|
|
|
entity_platform,
|
|
|
|
)
|
2023-12-30 13:23:46 +01:00
|
|
|
from homeassistant.helpers.config_validation import make_entity_service_schema
|
2023-12-31 16:52:30 +01:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2023-04-04 13:44:07 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-04-06 19:13:14 +02:00
|
|
|
|
2024-08-26 20:39:22 +02:00
|
|
|
from .computer import OSType, Computer
|
|
|
|
from .computer.utils import format_debug_information
|
2023-04-04 13:44:07 +02:00
|
|
|
from .const import SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, SERVICE_PUT_COMPUTER_TO_SLEEP, \
|
2023-12-28 22:40:29 +01:00
|
|
|
SERVICE_START_COMPUTER_TO_WINDOWS, SERVICE_RESTART_COMPUTER, SERVICE_RESTART_TO_LINUX_FROM_WINDOWS, \
|
2023-12-31 16:52:30 +01:00
|
|
|
SERVICE_CHANGE_MONITORS_CONFIG, SERVICE_STEAM_BIG_PICTURE, SERVICE_CHANGE_AUDIO_CONFIG, SERVICE_DEBUG_INFO, DOMAIN
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2024-01-01 16:27:22 +01:00
|
|
|
# Retrieve the data from the config flow
|
2023-04-04 13:44:07 +02:00
|
|
|
mac_address: str = config.data.get(CONF_MAC)
|
2024-01-01 16:27:22 +01:00
|
|
|
# broadcast_address: str | None = config.data.get(CONF_BROADCAST_ADDRESS)
|
|
|
|
# broadcast_port: int | None = config.data.get(CONF_BROADCAST_PORT)
|
2023-04-04 13:44:07 +02:00
|
|
|
host: str = config.data.get(CONF_HOST)
|
|
|
|
name: str = config.data.get(CONF_NAME)
|
|
|
|
dualboot: bool = config.data.get("dualboot")
|
|
|
|
username: str = config.data.get(CONF_USERNAME)
|
|
|
|
password: str = config.data.get(CONF_PASSWORD)
|
|
|
|
port: int | None = config.data.get(CONF_PORT)
|
|
|
|
|
2023-12-31 17:30:27 +01:00
|
|
|
# Register the computer switch
|
2023-04-04 13:44:07 +02:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
ComputerSwitch(
|
|
|
|
hass,
|
|
|
|
name,
|
|
|
|
host,
|
|
|
|
mac_address,
|
|
|
|
dualboot,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
port,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
host is not None,
|
|
|
|
)
|
|
|
|
|
|
|
|
platform = entity_platform.async_get_current_platform()
|
2023-12-30 15:32:31 +01:00
|
|
|
|
2023-12-31 17:30:27 +01:00
|
|
|
# Synthax : (service_name: str, schema: dict, supports_response: SupportsResponse)
|
|
|
|
services = [
|
|
|
|
(SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_RESTART_TO_LINUX_FROM_WINDOWS, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_PUT_COMPUTER_TO_SLEEP, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_START_COMPUTER_TO_WINDOWS, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_RESTART_COMPUTER, {}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_CHANGE_MONITORS_CONFIG, {vol.Required("monitors_config"): dict}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_STEAM_BIG_PICTURE, {vol.Required("action"): str}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_CHANGE_AUDIO_CONFIG, {
|
|
|
|
vol.Optional("volume"): int,
|
|
|
|
vol.Optional("mute"): bool,
|
|
|
|
vol.Optional("input_device"): str,
|
|
|
|
vol.Optional("output_device"): str
|
|
|
|
}, SupportsResponse.NONE),
|
|
|
|
(SERVICE_DEBUG_INFO, {}, SupportsResponse.ONLY),
|
|
|
|
]
|
|
|
|
|
2024-01-01 16:27:22 +01:00
|
|
|
# Register the services that depends on the switch
|
2023-12-31 17:30:27 +01:00
|
|
|
for service in services:
|
2023-12-30 15:32:31 +01:00
|
|
|
platform.async_register_entity_service(
|
2023-12-31 17:30:27 +01:00
|
|
|
service[0],
|
|
|
|
make_entity_service_schema(service[1]),
|
|
|
|
service[0],
|
|
|
|
supports_response=service[2]
|
2023-12-30 15:32:31 +01:00
|
|
|
)
|
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
class ComputerSwitch(SwitchEntity):
|
|
|
|
"""Representation of a computer switch."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
name: str,
|
|
|
|
host: str | None,
|
|
|
|
mac_address: str,
|
|
|
|
dualboot: bool | False,
|
|
|
|
username: str,
|
|
|
|
password: str,
|
|
|
|
port: int | None,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the WOL switch."""
|
|
|
|
|
|
|
|
self._hass = hass
|
|
|
|
self._attr_name = name
|
2024-08-25 23:20:39 +02:00
|
|
|
|
|
|
|
self.computer = Computer(host, mac_address, username, password, port, dualboot)
|
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
self._state = False
|
|
|
|
self._attr_assumed_state = host is None
|
|
|
|
self._attr_should_poll = bool(not self._attr_assumed_state)
|
|
|
|
self._attr_unique_id = dr.format_mac(mac_address)
|
|
|
|
self._attr_extra_state_attributes = {}
|
|
|
|
|
2023-12-31 16:52:30 +01:00
|
|
|
@property
|
|
|
|
def device_info(self) -> DeviceInfo | None:
|
|
|
|
"""Return the device information."""
|
2024-08-25 23:20:39 +02:00
|
|
|
# TODO: remove this?
|
|
|
|
# if self._host is None:
|
|
|
|
# return None
|
2023-12-31 16:52:30 +01:00
|
|
|
|
|
|
|
return DeviceInfo(
|
2024-08-25 23:20:39 +02:00
|
|
|
identifiers={(DOMAIN, self.computer.mac)},
|
2023-12-31 16:52:30 +01:00
|
|
|
name=self._attr_name,
|
|
|
|
manufacturer="Generic",
|
|
|
|
model="Computer",
|
2024-08-26 20:39:22 +02:00
|
|
|
sw_version=self.computer.operating_system_version,
|
2024-08-25 23:20:39 +02:00
|
|
|
connections={(dr.CONNECTION_NETWORK_MAC, self.computer.mac)},
|
2023-12-31 16:52:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
return "mdi:monitor" if self._state else "mdi:monitor-off"
|
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
return self._state
|
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def turn_on(self, **kwargs: Any) -> None:
|
|
|
|
await self.computer.start()
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
if self._attr_assumed_state:
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def turn_off(self, **kwargs: Any) -> None:
|
|
|
|
await self.computer.shutdown()
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
if self._attr_assumed_state:
|
|
|
|
self._state = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2024-08-25 23:20:39 +02:00
|
|
|
# Services
|
2024-08-26 14:29:09 +02:00
|
|
|
async def restart_to_windows_from_linux(self) -> None:
|
2023-04-04 13:44:07 +02:00
|
|
|
"""Restart the computer to Windows from a running Linux by setting grub-reboot and restarting."""
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.restart(OSType.LINUX, OSType.WINDOWS)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def restart_to_linux_from_windows(self) -> None:
|
2023-04-06 19:15:41 +02:00
|
|
|
"""Restart the computer to Linux from a running Windows by setting grub-reboot and restarting."""
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.restart()
|
2023-04-06 19:15:41 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def put_computer_to_sleep(self) -> None:
|
|
|
|
await self.computer.put_to_sleep()
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def start_computer_to_windows(self) -> None:
|
2024-08-25 23:20:39 +02:00
|
|
|
async def wait_task():
|
|
|
|
while not self.is_on:
|
2024-08-26 14:29:09 +02:00
|
|
|
await asyncio.sleep(3)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.restart(OSType.LINUX, OSType.WINDOWS)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-25 23:20:39 +02:00
|
|
|
"""Start the computer to Linux, wait for it to boot, and then set grub-reboot and restart."""
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.start()
|
2024-08-25 23:20:39 +02:00
|
|
|
self._hass.loop.create_task(wait_task())
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def restart_computer(self) -> None:
|
|
|
|
await self.computer.restart()
|
2023-04-06 19:13:14 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def change_monitors_config(self, monitors_config: dict | None = None) -> None:
|
2023-12-28 22:40:29 +01:00
|
|
|
"""Change the monitors configuration using a YAML config file."""
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.change_monitors_config(monitors_config)
|
2023-12-28 22:40:29 +01:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def steam_big_picture(self, action: str) -> None:
|
2024-08-25 23:20:39 +02:00
|
|
|
match action:
|
|
|
|
case "start":
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.start_steam_big_picture()
|
2024-08-25 23:20:39 +02:00
|
|
|
case "stop":
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.stop_steam_big_picture()
|
2024-08-25 23:20:39 +02:00
|
|
|
case "exit":
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.exit_steam_big_picture()
|
2023-12-30 15:32:31 +01:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def change_audio_config(self, volume: int | None = None, mute: bool | None = None,
|
|
|
|
input_device: str | None = None,
|
2023-12-30 20:14:24 +01:00
|
|
|
output_device: str | None = None) -> None:
|
2023-12-30 18:45:14 +01:00
|
|
|
"""Change the audio configuration using a YAML config file."""
|
2024-08-26 14:29:09 +02:00
|
|
|
await self.computer.change_audio_config(volume, mute, input_device, output_device)
|
2024-08-25 23:20:39 +02:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def debug_info(self) -> ServiceResponse:
|
2024-08-25 23:20:39 +02:00
|
|
|
"""Prints debug info."""
|
2024-08-26 20:39:22 +02:00
|
|
|
return await format_debug_information(self.computer)
|
2023-12-30 18:45:14 +01:00
|
|
|
|
2024-08-26 14:29:09 +02:00
|
|
|
async def async_update(self) -> None:
|
2023-04-04 13:44:07 +02:00
|
|
|
"""Ping the computer to see if it is online and update the state."""
|
2024-08-26 14:29:09 +02:00
|
|
|
self._state = await self.computer.is_on()
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2024-08-26 14:44:20 +02:00
|
|
|
await self.computer.update()
|
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
# Update the state attributes and the connection only if the computer is on
|
|
|
|
if self._state:
|
|
|
|
self._attr_extra_state_attributes = {
|
2024-08-26 20:39:22 +02:00
|
|
|
"operating_system": self.computer.operating_system,
|
|
|
|
"operating_system_version": self.computer.operating_system_version,
|
2024-08-25 23:20:39 +02:00
|
|
|
"mac_address": self.computer.mac,
|
|
|
|
"ip_address": self.computer.host,
|
2024-08-26 20:39:22 +02:00
|
|
|
"connected_devices": self.computer.bluetooth_devices,
|
2023-04-04 13:44:07 +02:00
|
|
|
}
|
2023-12-30 20:21:37 +01:00
|
|
|
|