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

Возможность указать хост по умолчанию в конфигурации #7

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ If the integration is not in the list, you need to clear the browser cache.

```yaml
ssh_command:
host: 192.168.1.123 # Optional
port: 22 # Optional
username: pi # Optional
password: raspberry # Optional
```

## Usage
Expand All @@ -38,6 +42,7 @@ script:
- service: ssh_command.exec_command
data:
host: 192.168.1.123
port: 22
user: pi
pass: raspberry
command: ls -la
Expand Down
24 changes: 20 additions & 4 deletions custom_components/ssh_command/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import logging

import voluptuous as vol

from homeassistant.core import ServiceCall
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
from homeassistant.helpers import config_validation as cv
from paramiko import AutoAddPolicy, RSAKey, SSHClient

_LOGGER = logging.getLogger(__name__)

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,
}, extra=vol.PREVENT_EXTRA))

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

def setup(hass, hass_config):
hass.data[DOMAIN] = hass_config.get(DOMAIN, {})
async def exec_command(call: ServiceCall):
host = call.data.get('host', '172.17.0.1')
port = call.data.get('port', 22)
username = call.data.get('user', 'pi')
password = call.data.get('pass', 'raspberry')
host = call.data.get('host', hass.data[DOMAIN].get(CONF_HOST, '172.17.0.1'))
port = call.data.get('port', hass.data[DOMAIN].get(CONF_PORT, 22))
username = call.data.get('user', hass.data[DOMAIN].get(CONF_USERNAME, 'pi'))
password = call.data.get('pass', hass.data[DOMAIN].get(CONF_PASSWORD, 'raspberry'))
command = call.data.get('command')
ssh_private_key_path = call.data.get('ssh_private_key_path')

Expand Down
Loading