Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add power service to System Bridge integration #95719

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion homeassistant/components/system_bridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
}
timmo001 marked this conversation as resolved.
Show resolved Hide resolved


async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -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)
Expand All @@ -162,6 +173,17 @@ 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,
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)
Expand Down Expand Up @@ -200,6 +222,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,
Expand Down
25 changes: 25 additions & 0 deletions homeassistant/components/system_bridge/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ send_text:
example: "Hello world"
selector:
text:
power_command:
name: Power Command
description: Sends a power command to the system.
joostlek marked this conversation as resolved.
Show resolved Hide resolved
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"