Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen committed Feb 10, 2019
2 parents d3b64e8 + 499b544 commit caaf7b1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
13 changes: 10 additions & 3 deletions homeassistant_cli/plugins/entity.py
Expand Up @@ -19,7 +19,6 @@
@pass_context
def cli(ctx):
"""Get info and operate on entities from Home Assistant."""
ctx.auto_output("table")


@cli.command()
Expand All @@ -29,6 +28,7 @@ def cli(ctx):
@pass_context
def get(ctx: Configuration, entity):
"""Get/read entity state from Home Assistant."""
ctx.auto_output("table")
state = api.get_state(ctx, entity)

if state:
Expand All @@ -50,6 +50,7 @@ def get(ctx: Configuration, entity):
@pass_context
def delete(ctx: Configuration, entity):
"""Delete entity from Home Assistant."""
ctx.auto_output("table")
deleted = api.remove_state(ctx, entity)

if deleted:
Expand All @@ -63,6 +64,7 @@ def delete(ctx: Configuration, entity):
@pass_context
def listcmd(ctx, entityfilter):
"""List all state from Home Assistant."""
ctx.auto_output("table")
states = api.get_states(ctx)

result = [] # type: List[Dict]
Expand Down Expand Up @@ -107,6 +109,7 @@ def listcmd(ctx, entityfilter):
@pass_context
def edit(ctx: Configuration, entity, newstate, attributes, merge, json):
"""Edit entity state from Home Assistant."""
ctx.auto_output('data')
if json:
_LOGGING.debug(
"json found overriding/creating new state for entity %s", entity
Expand Down Expand Up @@ -169,11 +172,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 All @@ -197,6 +200,7 @@ def _homeassistant_cmd(ctx: Configuration, entities, cmd, action):
@pass_context
def toggle(ctx: Configuration, entities):
"""Toggle state for one or more entities in Home Assistant."""
ctx.auto_output("table")
_homeassistant_cmd(ctx, entities, 'toggle', "toggled")


Expand All @@ -207,6 +211,7 @@ def toggle(ctx: Configuration, entities):
@pass_context
def off_cmd(ctx: Configuration, entities):
"""Turn entity off."""
ctx.auto_output("table")
_homeassistant_cmd(ctx, entities, 'turn_off', "turned off")


Expand All @@ -217,6 +222,7 @@ def off_cmd(ctx: Configuration, entities):
@pass_context
def on_cmd(ctx: Configuration, entities):
"""Turn entity on."""
ctx.auto_output("table")
_homeassistant_cmd(ctx, entities, 'turn_on', "turned on")


Expand Down Expand Up @@ -251,6 +257,7 @@ def history(ctx: Configuration, entities: List, since: str, end: str):
"""
import dateparser

ctx.auto_output("table")
settings = {
'DATE_ORDER': 'DMY',
'TIMEZONE': 'UTC',
Expand Down
2 changes: 1 addition & 1 deletion homeassistant_cli/plugins/service.py
Expand Up @@ -99,4 +99,4 @@ def call(ctx: Configuration, service, arguments):
result = api.call_service(ctx, parts[0], parts[1], data)

_LOGGING.debug("Formatting ouput")
ctx.echo(format_output(ctx, result)) # type: ignore
ctx.echo(format_output(ctx, result))
5 changes: 3 additions & 2 deletions homeassistant_cli/remote.py
Expand Up @@ -91,6 +91,7 @@ def wsapi(
ctx: Configuration, frame: Dict, wait: bool = False
) -> Optional[Dict]:
"""Make a call to Home Assistant using WS API."""

async def fetcher() -> Optional[Dict]:
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
Expand Down Expand Up @@ -485,7 +486,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 @@ -502,7 +503,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 caaf7b1

Please sign in to comment.