Permalink
Browse files

Fix result reporting from service calls

Why:

 * service calls returns list of data not single elements

This change addreses the need by:

 * skip double-listing
  • Loading branch information...
maxandersen committed Jan 29, 2019
1 parent edbe4bf commit 14fc9527ec0861a7dd26e185f4937bf9aace7f07
Showing with 36 additions and 4 deletions.
  1. +2 −2 homeassistant_cli/plugins/entity.py
  2. +2 −2 homeassistant_cli/remote.py
  3. +32 −0 tests/test_entity.py
@@ -160,11 +160,11 @@ def edit(ctx: Configuration, entity, newstate, attributes, merge, json):
_LOGGING.debug("Updated to: %s", result)


def _report(ctx: Configuration, result: Dict[str, Any], action: str):
def _report(ctx: Configuration, result: List[Dict[str, Any]], action: str):
ctx.echo(
helper.format_output(
ctx,
[result],
result,
columns=ctx.columns if ctx.columns else const.COLUMNS_ENTITIES,
)
)
@@ -372,7 +372,7 @@ def call_service(
domain: str,
service: str,
service_data: Optional[Dict] = None,
) -> Dict[str, Any]:
) -> List[Dict[str, Any]]:
"""Call a service."""
try:
req = restapi(
@@ -389,7 +389,7 @@ def call_service(
"Error calling service: {} - {}".format(req.status_code, req.text)
)

return cast(Dict[str, Any], req.json())
return cast(List[Dict[str, Any]], req.json())


def get_services(ctx: Configuration,) -> List[Dict[str, Any]]:
@@ -28,6 +28,8 @@
}
"""

LIST_EDITED_ENTITY = "[{}]".format(EDITED_ENTITY)


def test_entity_list(basic_entities_text) -> None:
"""Test entities can be listed."""
@@ -215,6 +217,36 @@ def test_entity_edit(basic_entities_text, basic_entities) -> None:
assert post.request_history[0].json()['state'] == 'myspecialstate'


def test_entity_toggle(basic_entities_text, basic_entities) -> None:
"""Test basic edit of state."""
with requests_mock.Mocker() as mock:
mock.get(
"http://localhost:8123/api/states",
text=basic_entities_text,
status_code=200,
)
post = mock.post(
"http://localhost:8123/api/services/homeassistant/toggle",
text=LIST_EDITED_ENTITY,
status_code=200,
)

runner = CliRunner()
result = runner.invoke(
cli.cli,
["--output=json", "entity", "toggle", "sensor.one"],
catch_exceptions=False,
)

assert result.exit_code == 0
assert post.call_count == 1

data = json.loads(result.output)
assert isinstance(data, list)
assert len(data) == 1
assert isinstance(data[0], dict)


def test_entity_filter(default_entities) -> None:
"""Test entities can be listed with filter."""
with requests_mock.Mocker() as mock:

0 comments on commit 14fc952

Please sign in to comment.