Add power service to System Bridge integration (#95719)

* Add power service to System Bridge

Add missing return types

Use in list validator and fix command

* Use attr map instead of concatination

* Update strings

* Update homeassistant/components/system_bridge/strings.json

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/99154/head
Aidan Timson 2023-08-28 16:44:23 +01:00 committed by GitHub
parent ef7a246f09
commit 42597f80a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_COMMAND,
CONF_HOST,
CONF_PATH,
CONF_PORT,
@ -47,10 +48,20 @@ CONF_KEY = "key"
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,
@ -136,7 +147,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)
@ -161,6 +172,17 @@ async def async_setup_entry(
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,
POWER_COMMAND_MAP[call.data[CONF_COMMAND]],
)()
async def handle_open_url(call: ServiceCall) -> None:
"""Handle the open url service call."""
_LOGGER.info("Open: %s", call.data)
@ -199,6 +221,18 @@ async def async_setup_entry(
),
)
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,

View File

@ -46,3 +46,22 @@ send_text:
example: "Hello world"
selector:
text:
power_command:
fields:
bridge:
required: true
selector:
device:
integration: system_bridge
command:
required: true
example: "sleep"
selector:
select:
options:
- "hibernate"
- "lock"
- "logout"
- "restart"
- "shutdown"
- "sleep"

View File

@ -133,6 +133,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."
}
}
}
}
}