removed some leftovers from wol integration and rework of the service registring
Some checks failed
HACS Action / HACS Action (push) Failing after 6s
Validate with hassfest / validate (push) Failing after 1s

This commit is contained in:
Mathieu Broillet 2023-12-31 17:30:27 +01:00
parent a3c195e792
commit 7ced302e12
Signed by: mathieu
GPG Key ID: C0E9E0E95AF03319
3 changed files with 38 additions and 82 deletions

View File

@ -17,13 +17,11 @@ from .const import DOMAIN, SERVICE_SEND_MAGIC_PACKET, SERVICE_CHANGE_MONITORS_CO
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
WAKE_ON_LAN_SEND_MAGIC_PACKET_SCHEMA = vol.Schema( WAKE_ON_LAN_SEND_MAGIC_PACKET_SCHEMA = vol.Schema({
{
vol.Required(CONF_MAC): cv.string, vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_BROADCAST_ADDRESS): cv.string, vol.Optional(CONF_BROADCAST_ADDRESS): cv.string,
vol.Optional(CONF_BROADCAST_PORT): cv.port, vol.Optional(CONF_BROADCAST_PORT): cv.port,
} })
)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@ -52,6 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
partial(wakeonlan.send_magic_packet, mac_address, **service_kwargs) partial(wakeonlan.send_magic_packet, mac_address, **service_kwargs)
) )
# Register the wake on lan service
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
SERVICE_SEND_MAGIC_PACKET, SERVICE_SEND_MAGIC_PACKET,

View File

@ -7,7 +7,6 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, exceptions from homeassistant import config_entries, exceptions
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from paramiko.ssh_exception import AuthenticationException from paramiko.ssh_exception import AuthenticationException
from . import utils from . import utils
@ -49,7 +48,8 @@ class Hub:
async def test_connection(self) -> bool: async def test_connection(self) -> bool:
"""Test connectivity to the computer is OK.""" """Test connectivity to the computer is OK."""
try: try:
return utils.test_connection(utils.create_ssh_connection(self._host, self._username, self._password, self._port)) return utils.test_connection(
utils.create_ssh_connection(self._host, self._username, self._password, self._port))
except AuthenticationException: except AuthenticationException:
return False return False
@ -61,7 +61,7 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
if len(data["host"]) < 3: if len(data["host"]) < 3:
raise InvalidHost raise InvalidHost
hub = Hub(hass, **data) hub = Hub(hass, data["host"], data["username"], data["password"], data["port"])
if not await hub.test_connection(): if not await hub.test_connection():
raise CannotConnect raise CannotConnect

View File

@ -9,10 +9,7 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
import wakeonlan import wakeonlan
from homeassistant.components.switch import ( from homeassistant.components.switch import (SwitchEntity)
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
SwitchEntity,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_BROADCAST_ADDRESS, CONF_BROADCAST_ADDRESS,
@ -26,7 +23,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse from homeassistant.core import HomeAssistant, ServiceResponse, SupportsResponse
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import ( from homeassistant.helpers import (
config_validation as cv,
device_registry as dr, device_registry as dr,
entity_platform, entity_platform,
) )
@ -42,31 +38,13 @@ from .const import SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, SERVICE_PUT_COMPUTER_T
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_OFF_ACTION = "turn_off"
DEFAULT_NAME = "Computer Management (WoL, SoL)"
DEFAULT_PING_TIMEOUT = 1
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_BROADCAST_ADDRESS): cv.string,
vol.Optional(CONF_BROADCAST_PORT): cv.port,
vol.Required(CONF_HOST): cv.string,
vol.Required("dualboot", default=False): cv.boolean,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_USERNAME, default="root"): cv.string,
vol.Required(CONF_PASSWORD, default="root"): cv.string,
vol.Optional(CONF_PORT, default=22): cv.string,
}
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigEntry, config: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
# Setup the computer switch
mac_address: str = config.data.get(CONF_MAC) mac_address: str = config.data.get(CONF_MAC)
broadcast_address: str | None = config.data.get(CONF_BROADCAST_ADDRESS) broadcast_address: str | None = config.data.get(CONF_BROADCAST_ADDRESS)
broadcast_port: int | None = config.data.get(CONF_BROADCAST_PORT) broadcast_port: int | None = config.data.get(CONF_BROADCAST_PORT)
@ -77,6 +55,7 @@ async def async_setup_entry(
password: str = config.data.get(CONF_PASSWORD) password: str = config.data.get(CONF_PASSWORD)
port: int | None = config.data.get(CONF_PORT) port: int | None = config.data.get(CONF_PORT)
# Register the computer switch
async_add_entities( async_add_entities(
[ [
ComputerSwitch( ComputerSwitch(
@ -97,55 +76,32 @@ async def async_setup_entry(
platform = entity_platform.async_get_current_platform() platform = entity_platform.async_get_current_platform()
basic_services = [SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, # Synthax : (service_name: str, schema: dict, supports_response: SupportsResponse)
SERVICE_RESTART_TO_LINUX_FROM_WINDOWS, services = [
SERVICE_PUT_COMPUTER_TO_SLEEP, (SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, {}, SupportsResponse.NONE),
SERVICE_START_COMPUTER_TO_WINDOWS, (SERVICE_RESTART_TO_WINDOWS_FROM_LINUX, {}, SupportsResponse.NONE),
SERVICE_RESTART_COMPUTER] (SERVICE_RESTART_TO_LINUX_FROM_WINDOWS, {}, SupportsResponse.NONE),
(SERVICE_PUT_COMPUTER_TO_SLEEP, {}, SupportsResponse.NONE),
for service in basic_services: (SERVICE_START_COMPUTER_TO_WINDOWS, {}, SupportsResponse.NONE),
platform.async_register_entity_service( (SERVICE_RESTART_COMPUTER, {}, SupportsResponse.NONE),
service, (SERVICE_CHANGE_MONITORS_CONFIG, {vol.Required("monitors_config"): dict}, SupportsResponse.NONE),
{}, (SERVICE_STEAM_BIG_PICTURE, {vol.Required("action"): str}, SupportsResponse.NONE),
service, (SERVICE_CHANGE_AUDIO_CONFIG, {
) vol.Optional("volume"): int,
# Register the service to change the monitors configuration
platform.async_register_entity_service(
SERVICE_CHANGE_MONITORS_CONFIG,
make_entity_service_schema(
{vol.Required("monitors_config"): dict}
),
SERVICE_CHANGE_MONITORS_CONFIG,
)
# Register the service to control Steam Big Picture mode
platform.async_register_entity_service(
SERVICE_STEAM_BIG_PICTURE,
make_entity_service_schema(
{vol.Required("action"): str}
),
SERVICE_STEAM_BIG_PICTURE,
)
# Register the service to change the audio configuration
platform.async_register_entity_service(
SERVICE_CHANGE_AUDIO_CONFIG,
make_entity_service_schema(
{vol.Optional("volume"): int,
vol.Optional("mute"): bool, vol.Optional("mute"): bool,
vol.Optional("input_device"): str, vol.Optional("input_device"): str,
vol.Optional("output_device"): str} vol.Optional("output_device"): str
), }, SupportsResponse.NONE),
SERVICE_CHANGE_AUDIO_CONFIG, (SERVICE_DEBUG_INFO, {}, SupportsResponse.ONLY),
) ]
# Register the service to print debug info # Register the services
for service in services:
platform.async_register_entity_service( platform.async_register_entity_service(
SERVICE_DEBUG_INFO, service[0],
{}, make_entity_service_schema(service[1]),
SERVICE_DEBUG_INFO, service[0],
supports_response=SupportsResponse.ONLY supports_response=service[2]
) )
@ -320,7 +276,8 @@ class ComputerSwitch(SwitchEntity):
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", "-c", "1", "-W", str(DEFAULT_PING_TIMEOUT), str(self._host)] timeout = 1
ping_cmd = ["ping", "-c", "1", "-W", str(timeout), str(self._host)]
status = sp.call(ping_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL) status = sp.call(ping_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
self._state = not bool(status) self._state = not bool(status)