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

autocomplete for raw api #112

Merged
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
20 changes: 19 additions & 1 deletion homeassistant_cli/autocompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from typing import Any, Dict, List, Tuple # NOQA

from homeassistant_cli import const
from homeassistant_cli import const, hassconst
from homeassistant_cli.config import Configuration, resolve_server
import homeassistant_cli.remote as api
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -152,3 +152,21 @@ def table_formats(
completions.sort()

return [c for c in completions if incomplete in c[0]]


def api_methods(
ctx: Configuration, args: List, incomplete: str
) -> List[Tuple[str, str]]:
"""Auto completion for methods."""
_init_ctx(ctx)

from inspect import getmembers

completions = []
for name, value in getmembers(hassconst):
if name.startswith('URL_API_'):
completions.append((value, name[len('URL_API_') :]))
maxandersen marked this conversation as resolved.
Show resolved Hide resolved

completions.sort()

return [c for c in completions if incomplete in c[0]]
9 changes: 7 additions & 2 deletions homeassistant_cli/plugins/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

import click
import homeassistant_cli.autocompletion as autocompletion
from homeassistant_cli.cli import pass_context
from homeassistant_cli.config import Configuration
from homeassistant_cli.helper import format_output
Expand Down Expand Up @@ -37,7 +38,9 @@ def _report(ctx, cmd, method, response) -> None:


@cli.command()
@click.argument('method')
@click.argument( # type: ignore
'method', autocompletion=autocompletion.api_methods
)
@pass_context
def get(ctx: Configuration, method):
"""Do a GET request against api/<method>."""
Expand All @@ -47,7 +50,9 @@ def get(ctx: Configuration, method):


@cli.command()
@click.argument('method')
@click.argument( # type: ignore
'method', autocompletion=autocompletion.api_methods
)
@click.option('--json')
@pass_context
def post(ctx: Configuration, method, json):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ norecursedirs = .git testing_config

[flake8]
exclude = .venv,.git,.tox,docs,venv,bin,lib,deps,build
ignore = E203

[isort]
# https://github.com/timothycrosley/isort
Expand Down
19 changes: 11 additions & 8 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def test_entity_completion(basic_entities_text) -> None:
)

cfg = cli.cli.make_context('hass-cli', ['entity', 'get'])

result = autocompletion.entities(cfg, "entity get", "")
result = autocompletion.entities( # type: ignore
cfg, ["entity", "get"], ""
)
assert len(result) == 3

resultdict = dict(result)
Expand All @@ -35,16 +36,16 @@ def test_service_completion(default_services_text) -> None:

cfg = cli.cli.make_context('hass-cli', ['service', 'get'])

result = autocompletion.services(cfg, "service get", "")
result = autocompletion.services( # type: ignore
cfg, ["service", "get"], ""
)
assert len(result) == 121

resultdict = dict(result)

assert "automation.reload" in resultdict
assert (
resultdict["automation.reload"]
== "Reload the automation configuration."
)
val = resultdict["automation.reload"]
assert val == "Reload the automation configuration."


def test_event_completion(default_events_text) -> None:
Expand All @@ -58,7 +59,9 @@ def test_event_completion(default_events_text) -> None:

cfg = cli.cli.make_context('hass-cli', ['service', 'get'])

result = autocompletion.events(cfg, "events get", "")
result = autocompletion.events( # type: ignore
cfg, ["events", "get"], ""
)
assert len(result) == 11

resultdict = dict(result)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import json

from click.testing import CliRunner
import homeassistant_cli.autocompletion as autocompletion
import homeassistant_cli.cli as cli
from homeassistant_cli.config import Configuration
import requests_mock


Expand Down Expand Up @@ -40,3 +42,15 @@ def test_raw_post() -> None:
assert result.exit_code == 0
data = json.loads(result.output)
assert data['message'] == 'success'


def test_apimethod_completion(default_services) -> None:
"""Test completion for raw api methods."""
cfg = Configuration()

result = autocompletion.api_methods(cfg, ["raw", "get"], "/api/disc")
assert len(result) == 1

resultdict = dict(result)

assert "/api/discovery_info" in resultdict