Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ All parameters are optional in the raw schema except `host` and `username` — t

#### Validation rules enforced by the service

- Either `password` or `key_file` must be provided
- Either `password` or `key_file` must be provided, but not both
- Either `command` or `input` or both must be provided
- If `key_file` is provided, the file must exist on the Home Assistant filesystem
- `known_hosts` may not be provided when `check_known_hosts` is `false`
Expand Down
7 changes: 7 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ async def _validate_service_data(hass: HomeAssistant, data: dict[str, Any]) -> N
translation_key="password_or_key_file_required",
)

if has_password and has_key_file:
raise ServiceValidationError(
"Password and key file cannot both be provided.",
translation_domain=DOMAIN,
translation_key="password_and_key_file",
)

has_command: bool = bool(data.get(CONF_COMMAND))
has_input: bool = bool(data.get(CONF_INPUT))

Expand Down
3 changes: 3 additions & 0 deletions strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"password_or_key_file_required": {
"message": "Either password or key file must be provided."
},
"password_and_key_file": {
"message": "Password and key file cannot both be provided."
},
"command_or_input": {
"message": "Either command or input must be provided."
},
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_tests/test_validate_service_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ async def test_no_password_no_key_file_raises(self):
await _validate_service_data(self.mock_hass, {"command": "echo hi"})
self.assertEqual(ctx.exception.translation_key, "password_or_key_file_required")

async def test_password_and_key_file_raises(self):
with self.assertRaises(ServiceValidationError) as ctx:
await _validate_service_data(self.mock_hass, {"password": "secret", "key_file": "/home/user/.ssh/id_rsa", "command": "echo hi"})
self.assertEqual(ctx.exception.translation_key, "password_and_key_file")

async def test_no_command_no_input_raises(self):
with self.assertRaises(ServiceValidationError) as ctx:
await _validate_service_data(self.mock_hass, {"password": "secret"})
Expand Down
3 changes: 3 additions & 0 deletions translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"password_or_key_file_required": {
"message": "Entweder Passwort oder Schlüsseldatei muss angegeben werden."
},
"password_and_key_file": {
"message": "Passwort und Schlüsseldatei können nicht gleichzeitig angegeben werden."
},
"command_or_input": {
"message": "Entweder Befehl oder Eingabe muss angegeben werden."
},
Expand Down
3 changes: 3 additions & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"password_or_key_file_required": {
"message": "Either password or key file must be provided."
},
"password_and_key_file": {
"message": "Password and key file cannot both be provided."
},
"command_or_input": {
"message": "Either command or input must be provided."
},
Expand Down
Loading