Skip to content

Commit

Permalink
Fix SSH_COMMAND_SCHEMA and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 19, 2024
1 parent b1476d5 commit e931b22
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ script:
command: ls -la
```
If you want use secrets, add them to `configuration.yaml`:
If you want use secrets or change default values, add them to `configuration.yaml`:

```yaml
ssh_command:
host: 192.168.1.123 # Optional
port: 22 # Optional
username: pi # Optional
password: !secret ssh_parssword # Optional
host: 192.168.1.123
port: 22
user: pi
pass: !secret ssh_parssword
```
38 changes: 16 additions & 22 deletions custom_components/ssh_command/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

import voluptuous as vol
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import config_validation as cv
from paramiko import AutoAddPolicy, RSAKey, SSHClient
Expand All @@ -10,31 +9,28 @@

DOMAIN = "ssh_command"

SSH_COMMAND_SCHEMA = vol.All(
vol.Schema(
{
vol.Optional(CONF_HOST): cv.string,
vol.Optional(CONF_PORT): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional("private_key"): cv.string,
},
extra=vol.PREVENT_EXTRA,
)
DEFAULT_SCHEMA = vol.Schema(
{
vol.Optional("host", default="172.17.0.1"): cv.string,
vol.Optional("port", default=22): cv.port,
vol.Optional("user", default="pi"): cv.string,
vol.Optional("pass", default="raspberry"): cv.string,
},
extra=vol.PREVENT_EXTRA,
)

CONFIG_SCHEMA = vol.Schema({DOMAIN: SSH_COMMAND_SCHEMA}, extra=vol.ALLOW_EXTRA)
CONFIG_SCHEMA = vol.Schema({DOMAIN: DEFAULT_SCHEMA}, extra=vol.ALLOW_EXTRA)


async def async_setup(hass: HomeAssistant, config: dict) -> bool:
config: dict = config.get(DOMAIN) or {}
default = config[DOMAIN] if DOMAIN in config else DEFAULT_SCHEMA({})

def exec_command(call: ServiceCall):
host = call.data.get("host", config.get(CONF_HOST, "172.17.0.1"))
port = call.data.get("port", config.get(CONF_PORT, 22))
username = call.data.get("user", config.get(CONF_USERNAME, "pi"))
password = call.data.get("pass", config.get(CONF_PASSWORD, "raspberry"))
command = call.data.get("command")
host = call.data.get("host", default["host"])
port = call.data.get("port", default["port"])
username = call.data.get("user", default["user"])
password = call.data.get("pass", default["pass"])
command = call.data["command"]

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
Expand Down Expand Up @@ -62,9 +58,7 @@ def exec_command(call: ServiceCall):

# ServiceResponse from Hass 2023.7
# https://github.com/home-assistant/core/blob/2023.7.0/homeassistant/core.py
hass.services.async_register(
DOMAIN, "exec_command", exec_command, SSH_COMMAND_SCHEMA
)
hass.services.async_register(DOMAIN, "exec_command", exec_command)

return True

Expand Down

0 comments on commit e931b22

Please sign in to comment.