Skip to content

Commit

Permalink
Fix result reporting from service calls
Browse files Browse the repository at this point in the history
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 14fc952
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions homeassistant_cli/plugins/entity.py
Expand Up @@ -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,
)
)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant_cli/remote.py
Expand Up @@ -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(
Expand All @@ -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]]:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_entity.py
Expand Up @@ -28,6 +28,8 @@
}
"""

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


def test_entity_list(basic_entities_text) -> None:
"""Test entities can be listed."""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 14fc952

Please sign in to comment.