From d293eabbb36ea581da487c6c6ff6f5b03b6ececc Mon Sep 17 00:00:00 2001 From: Max Rydahl Andersen Date: Sat, 12 Jan 2019 02:01:11 +0100 Subject: [PATCH] autocomplete for raw api 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. --- homeassistant_cli/autocompletion.py | 20 +++++++++++++++++++- homeassistant_cli/plugins/raw.py | 9 +++++++-- setup.cfg | 1 + tests/test_raw.py | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/homeassistant_cli/autocompletion.py b/homeassistant_cli/autocompletion.py index 13d0f70..376bd34 100644 --- a/homeassistant_cli/autocompletion.py +++ b/homeassistant_cli/autocompletion.py @@ -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 @@ -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]] diff --git a/homeassistant_cli/plugins/raw.py b/homeassistant_cli/plugins/raw.py index 16da188..243c45b 100644 --- a/homeassistant_cli/plugins/raw.py +++ b/homeassistant_cli/plugins/raw.py @@ -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 @@ -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/.""" @@ -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): diff --git a/setup.cfg b/setup.cfg index 213186f..18f53e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/test_raw.py b/tests/test_raw.py index d2d2567..3cb4846 100644 --- a/tests/test_raw.py +++ b/tests/test_raw.py @@ -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 @@ -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