From 99ed85ee87dbac18867ee22e08764f1b9e0411c3 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 1 Jun 2022 22:57:48 +0000 Subject: [PATCH 1/4] Add power service to System Bridge Add missing return types Use in list validator and fix command --- .../components/system_bridge/__init__.py | 35 ++++++++++++++++++- .../components/system_bridge/services.yaml | 25 +++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/system_bridge/__init__.py b/homeassistant/components/system_bridge/__init__.py index 05e607d56edb02..ceec9b759f5257 100644 --- a/homeassistant/components/system_bridge/__init__.py +++ b/homeassistant/components/system_bridge/__init__.py @@ -20,6 +20,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_API_KEY, + CONF_COMMAND, CONF_HOST, CONF_PATH, CONF_PORT, @@ -48,10 +49,20 @@ CONF_TEXT = "text" SERVICE_OPEN_PATH = "open_path" +SERVICE_POWER_COMMAND = "power_command" SERVICE_OPEN_URL = "open_url" SERVICE_SEND_KEYPRESS = "send_keypress" SERVICE_SEND_TEXT = "send_text" +POWER_COMMAND_MAP = { + "hibernate": "power_hibernate", + "lock": "power_lock", + "logout": "power_logout", + "restart": "power_restart", + "shutdown": "power_shutdown", + "sleep": "power_sleep", +} + async def async_setup_entry( hass: HomeAssistant, @@ -137,7 +148,7 @@ async def async_setup_entry( if hass.services.has_service(DOMAIN, SERVICE_OPEN_URL): return True - def valid_device(device: str): + def valid_device(device: str) -> str: """Check device is valid.""" device_registry = dr.async_get(hass) device_entry = device_registry.async_get(device) @@ -162,6 +173,16 @@ async def handle_open_path(call: ServiceCall) -> None: OpenPath(path=call.data[CONF_PATH]) ) + async def handle_power_command(call: ServiceCall) -> None: + """Handle the power command service call.""" + _LOGGER.info("Power command: %s", call.data) + coordinator: SystemBridgeDataUpdateCoordinator = hass.data[DOMAIN][ + call.data[CONF_BRIDGE] + ] + await getattr( + coordinator.websocket_client, f"power_{call.data[CONF_COMMAND]}" + )() + async def handle_open_url(call: ServiceCall) -> None: """Handle the open url service call.""" _LOGGER.info("Open: %s", call.data) @@ -200,6 +221,18 @@ async def handle_send_text(call: ServiceCall) -> None: ), ) + hass.services.async_register( + DOMAIN, + SERVICE_POWER_COMMAND, + handle_power_command, + schema=vol.Schema( + { + vol.Required(CONF_BRIDGE): valid_device, + vol.Required(CONF_COMMAND): vol.In(POWER_COMMAND_MAP), + }, + ), + ) + hass.services.async_register( DOMAIN, SERVICE_OPEN_URL, diff --git a/homeassistant/components/system_bridge/services.yaml b/homeassistant/components/system_bridge/services.yaml index 78d6e87f2184a4..5773744822b80a 100644 --- a/homeassistant/components/system_bridge/services.yaml +++ b/homeassistant/components/system_bridge/services.yaml @@ -46,3 +46,28 @@ send_text: example: "Hello world" selector: text: +power_command: + name: Power Command + description: Sends a power command to the system. + fields: + bridge: + name: Bridge + description: The server to send the command to. + required: true + selector: + device: + integration: system_bridge + command: + name: Command + description: Command to call. + required: true + example: "sleep" + selector: + select: + options: + - "hibernate" + - "lock" + - "logout" + - "restart" + - "shutdown" + - "sleep" From 870a66321e4af86ffddabfd2f33f888c7c04916b Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Tue, 4 Jul 2023 11:26:30 +0000 Subject: [PATCH 2/4] Use attr map instead of concatination --- homeassistant/components/system_bridge/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/system_bridge/__init__.py b/homeassistant/components/system_bridge/__init__.py index ceec9b759f5257..d1c79dc9a43ecc 100644 --- a/homeassistant/components/system_bridge/__init__.py +++ b/homeassistant/components/system_bridge/__init__.py @@ -180,7 +180,8 @@ async def handle_power_command(call: ServiceCall) -> None: call.data[CONF_BRIDGE] ] await getattr( - coordinator.websocket_client, f"power_{call.data[CONF_COMMAND]}" + coordinator.websocket_client, + POWER_COMMAND_MAP[call.data[CONF_COMMAND]], )() async def handle_open_url(call: ServiceCall) -> None: From 235c6efad6480d2eec835b3b1df79f9eb96de746 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 26 Jul 2023 11:07:30 +0000 Subject: [PATCH 3/4] Update strings --- .../components/system_bridge/services.yaml | 6 ------ .../components/system_bridge/strings.json | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/system_bridge/services.yaml b/homeassistant/components/system_bridge/services.yaml index 5773744822b80a..49a7931789eb75 100644 --- a/homeassistant/components/system_bridge/services.yaml +++ b/homeassistant/components/system_bridge/services.yaml @@ -47,19 +47,13 @@ send_text: selector: text: power_command: - name: Power Command - description: Sends a power command to the system. fields: bridge: - name: Bridge - description: The server to send the command to. required: true selector: device: integration: system_bridge command: - name: Command - description: Command to call. required: true example: "sleep" selector: diff --git a/homeassistant/components/system_bridge/strings.json b/homeassistant/components/system_bridge/strings.json index c3e1f949152e69..26dcb9cb92b60f 100644 --- a/homeassistant/components/system_bridge/strings.json +++ b/homeassistant/components/system_bridge/strings.json @@ -84,6 +84,20 @@ "description": "Text to type." } } + }, + "power_command": { + "name": "Power Command", + "description": "Sends a power command to the system.", + "fields": { + "bridge": { + "name": "[%key:component::system_bridge::services::open_path::fields::bridge::name%]", + "description": "[%key:component::system_bridge::services::send_keypress::fields::bridge::description%]" + }, + "command": { + "name": "Command", + "description": "Command to call." + } + } } } } From b8dd18900cecbfd7c967e0be4f8a2ad26f1584f4 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 26 Jul 2023 14:50:46 +0100 Subject: [PATCH 4/4] Update homeassistant/components/system_bridge/strings.json Co-authored-by: Joost Lekkerkerker --- homeassistant/components/system_bridge/strings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/system_bridge/strings.json b/homeassistant/components/system_bridge/strings.json index 26dcb9cb92b60f..fc38bd27037024 100644 --- a/homeassistant/components/system_bridge/strings.json +++ b/homeassistant/components/system_bridge/strings.json @@ -86,7 +86,7 @@ } }, "power_command": { - "name": "Power Command", + "name": "Power command", "description": "Sends a power command to the system.", "fields": { "bridge": {