Skip to content

Commit

Permalink
autocomplete for raw api
Browse files Browse the repository at this point in the history
Why:

 * tired of looking up comon api methods

This change addreses the need by:

 * simple 'hack' that will populate completion with the api strings
   found in hassconst.py. Its a nice start.
  • Loading branch information
maxandersen committed Jan 12, 2019
1 parent 9144557 commit d293eab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
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]]:
"""Api 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_') :]))

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
15 changes: 15 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,16 @@ 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

0 comments on commit d293eab

Please sign in to comment.