Permalink
Browse files

Merge branch 'master' into dev

  • Loading branch information...
maxandersen committed Feb 10, 2019
2 parents d3b64e8 + 499b544 commit caaf7b160f1bc7f7854d82ecdb35d0954c5e7ea8
Showing with 46 additions and 6 deletions.
  1. +10 −3 homeassistant_cli/plugins/entity.py
  2. +1 −1 homeassistant_cli/plugins/service.py
  3. +3 −2 homeassistant_cli/remote.py
  4. +32 −0 tests/test_entity.py
@@ -19,7 +19,6 @@
@pass_context
def cli(ctx):
"""Get info and operate on entities from Home Assistant."""
ctx.auto_output("table")


@cli.command()
@@ -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:
@@ -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:
@@ -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]
@@ -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
@@ -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,
)
)
@@ -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")


@@ -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")


@@ -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")


@@ -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',
@@ -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))
@@ -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(
@@ -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(
@@ -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]]:
@@ -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 caaf7b1

Please sign in to comment.