Skip to content

Commit

Permalink
Increase argument capability in raw ws and raw post
Browse files Browse the repository at this point in the history
This re-uses the --yaml and --json argument parsers from services to
allow passing from stdin and files.

One awesome use case is:
hass-cli raw ws lovelace/config > ui-lovelace.yaml
$EDITOR ui-lovelace.yaml , replacing the status fields, up until result:
with config:, and then,
hass-cli raw ws lovelace/config/save --yaml @ui-lovelace.yaml
  • Loading branch information
glance- committed Dec 6, 2021
1 parent 8cbfc47 commit 9e21b96
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions homeassistant_cli/plugins/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant_cli.config import Configuration
from homeassistant_cli.helper import format_output
import homeassistant_cli.remote as api
from homeassistant_cli.helper import argument_callback

_LOGGING = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,14 +57,23 @@ def get(ctx: Configuration, method):
@click.argument(
'method', autocompletion=autocompletion.api_methods # type: ignore
)
@click.option('--json')
@click.option(
'--json', help="""Json string to use as arguments.
if string is -, the data is read from stdin, and if it starts with the letter @
the rest should be a filename from which the data is read""",
callback=argument_callback,
expose_value=False
)
@click.option(
'--yaml', help="""Yaml string to use as arguments.
if string is -, the data is read from stdin, and if it starts with the letter @
the rest should be a filename from which the data is read""",
callback=argument_callback,
expose_value=False
)
@pass_context
def post(ctx: Configuration, method, json):
def post(ctx: Configuration, method, data={}): # noqa: D301
"""Do a POST request against api/<method>."""
if json:
data = json_.loads(json)
else:
data = {}

response = api.restapi(ctx, 'post', method, data)

Expand All @@ -74,22 +84,26 @@ def post(ctx: Configuration, method, json):
@click.argument(
'wstype', autocompletion=autocompletion.wsapi_methods # type: ignore
)
@click.option('--json')
@click.option(
'--json', help="""Json string to use as arguments.
if string is -, the data is read from stdin, and if it starts with the letter @
the rest should be a filename from which the data is read""",
callback=argument_callback,
expose_value=False
)
@click.option(
'--yaml', help="""Yaml string to use as arguments.
if string is -, the data is read from stdin, and if it starts with the letter @
the rest should be a filename from which the data is read""",
callback=argument_callback,
expose_value=False
)
@pass_context
def websocket(ctx: Configuration, wstype, json): # noqa: D301
def websocket(ctx: Configuration, wstype, data={}): # noqa: D301
"""Send a websocket request against /api/websocket.
WSTYPE is name of websocket methods.
\b
--json is dictionary to pass in addition to the type.
Example: --json='{ "area_id":"2c8bf93c8082492f99c989896962f207" }'
"""
if json:
data = json_.loads(json)
else:
data = {}

frame = {'type': wstype}
frame = {**frame, **data} # merging data into frame

Expand Down

0 comments on commit 9e21b96

Please sign in to comment.