improved monitors service entity selection and exceptions

This commit is contained in:
Mathieu Broillet 2023-12-30 13:23:46 +01:00
parent c8831c9cd4
commit b86d710ce3
Signed by: mathieu
GPG Key ID: C0E9E0E95AF03319
2 changed files with 15 additions and 21 deletions

View File

@ -56,16 +56,11 @@ restart_computer:
change_monitors_config: change_monitors_config:
name: Change monitors config name: Change monitors config
description: Change monitors config. description: Change monitors config.
target:
entity:
integration: easy_computer_manager
domain: switch
fields: fields:
entity_id:
name: Entity ID
description: Entity ID of the device to change monitors config.
required: true
example: switch.my_computer
selector:
entity:
integration: easy_computer_manager
domain: switch
monitors_config: monitors_config:
name: Monitors config name: Monitors config
description: Monitors config. description: Monitors config.

View File

@ -22,14 +22,15 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_PORT, CONF_PORT,
CONF_USERNAME, CONF_ENTITY_ID, CONF_USERNAME, )
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import ( from homeassistant.helpers import (
config_validation as cv, config_validation as cv,
device_registry as dr, device_registry as dr,
entity_platform, entity_platform,
) )
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from paramiko.ssh_exception import AuthenticationException from paramiko.ssh_exception import AuthenticationException
@ -58,12 +59,6 @@ PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
vol.Optional(CONF_PORT, default=22): cv.string, vol.Optional(CONF_PORT, default=22): cv.string,
} }
) )
SERVICE_CHANGE_MONITORS_CONFIG_SCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required("monitors_config"): dict,
}
)
async def async_setup_entry( async def async_setup_entry(
@ -127,7 +122,9 @@ async def async_setup_entry(
) )
platform.async_register_entity_service( platform.async_register_entity_service(
SERVICE_CHANGE_MONITORS_CONFIG, SERVICE_CHANGE_MONITORS_CONFIG,
SERVICE_CHANGE_MONITORS_CONFIG_SCHEMA, make_entity_service_schema(
{vol.Required("monitors_config"): dict}
),
SERVICE_CHANGE_MONITORS_CONFIG, SERVICE_CHANGE_MONITORS_CONFIG,
) )
@ -261,10 +258,12 @@ class ComputerSwitch(SwitchEntity):
else: else:
utils.restart_system(self._connection) utils.restart_system(self._connection)
def change_monitors_config(self, **kwargs) -> None: def change_monitors_config(self, monitors_config: dict | None = None) -> None:
"""Change the monitors configuration using a YAML config file.""" """Change the monitors configuration using a YAML config file."""
if kwargs["monitors_config"] is not None: if monitors_config is not None and len(monitors_config) > 0:
utils.change_monitors_config(self._connection, kwargs["monitors_config"]) utils.change_monitors_config(self._connection, monitors_config)
else:
raise HomeAssistantError("The monitors config is empty.")
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."""