Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for configurable larger websocket responses #411

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion homeassistant_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
)


def validate_file_size(ctx, param, value):
if value is not None:
if str(value).endswith('KB'):
size = int(value[:-2]) * 1024
elif str(value).endswith('MB'):
size = int(value[:-2]) * 1024 * 1024
else:
# bytes
size = int(value)
return size


def run() -> None:
"""Run entry point.

Expand Down Expand Up @@ -136,7 +148,7 @@ def _default_token() -> Optional[str]:
@click.option(
'--timeout',
help='Timeout for network operations.',
default=const.DEFAULT_TIMEOUT,
default=str(const.DEFAULT_TIMEOUT),
show_default=True,
)
@click.option(
Expand Down Expand Up @@ -205,6 +217,13 @@ def _default_token() -> Optional[str]:
default=None,
help='Sort table by the jsonpath expression. Example: last_changed',
)
@click.option(
'--max-message-size',
callback=validate_file_size,
default=const.WS_MAX_MESSAGE_SIZE,
help='Max size of websocket payload. Default: 4MB',
envvar='HASS_WS_MAX_MESSAGE_SIZE',
)
@pass_context
def cli(
ctx: Configuration,
Expand All @@ -222,6 +241,7 @@ def cli(
no_headers: bool,
table_format: str,
sort_by: Optional[str],
max_message_size: str,
) -> None:
"""Command line interface for Home Assistant."""
ctx.verbose = verbose
Expand All @@ -238,6 +258,7 @@ def cli(
ctx.no_headers = no_headers
ctx.table_format = table_format
ctx.sort_by = sort_by # type: ignore
ctx.max_message_size = max_message_size

_LOGGER.debug("Using settings: %s", ctx)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def resolve_server(ctx: Any) -> str: # noqa: F821
ctx.resolved_server = None

if not ctx.resolved_server:

if ctx.server == "auto":

if "HASSIO_TOKEN" in os.environ and "HASS_TOKEN" not in os.environ:
ctx.resolved_server = const.DEFAULT_SERVER_MDNS
else:
Expand Down Expand Up @@ -126,6 +124,7 @@ def __init__(self) -> None:
self.no_headers = False
self.table_format = 'plain'
self.sort_by = None
self.max_message_size = const.WS_MAX_MESSAGE_SIZE # type: str

def echo(self, msg: str, *args: Optional[Any]) -> None:
"""Put content message to stdout."""
Expand All @@ -151,6 +150,7 @@ def __repr__(self) -> str:
"access-token": 'yes' if self.token is not None else 'no',
"api-password": 'yes' if self.password is not None else 'no',
"insecure": self.insecure,
"max_message_size": self.max_message_size,
"output": self.output,
"verbose": self.verbose,
}
Expand Down
2 changes: 2 additions & 0 deletions homeassistant_cli/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
('CHANGED', 'last_changed'),
]
COLUMNS_SERVICES = [('DOMAIN', 'domain'), ("SERVICE", "domain.services[*]")]

WS_MAX_MESSAGE_SIZE = '4194304'
4 changes: 2 additions & 2 deletions homeassistant_cli/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def wsapi(
async def fetcher() -> Optional[Dict]:
async with aiohttp.ClientSession() as session:
async with session.ws_connect(
resolve_server(ctx) + "/api/websocket"
resolve_server(ctx) + "/api/websocket",
max_msg_size=ctx.max_message_size,
) as wsconn:

await wsconn.send_str(
json.dumps({'type': 'auth', 'access_token': ctx.token})
)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_area_list(default_areas) -> None:
with mock.patch(
'homeassistant_cli.remote.get_areas', return_value=default_areas
):

runner = CliRunner()
result = runner.invoke(
cli.cli, ["--output=json", "area", "list"], catch_exceptions=False
Expand All @@ -29,7 +28,6 @@ def test_area_list_filter(default_areas) -> None:
with mock.patch(
'homeassistant_cli.remote.get_areas', return_value=default_areas
):

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand Down
3 changes: 0 additions & 3 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def test_device_list(default_devices, default_areas) -> None:
with mock.patch(
'homeassistant_cli.remote.get_areas', return_value=default_areas
):

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand All @@ -37,7 +36,6 @@ def test_device_list_filter(default_devices, default_areas) -> None:
with mock.patch(
'homeassistant_cli.remote.get_areas', return_value=default_areas
):

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand All @@ -64,7 +62,6 @@ def test_device_assign(default_areas, default_devices) -> None:
'homeassistant_cli.remote.assign_area',
return_value={'success': True},
):

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def test_raw_ws() -> None:
with mocker.patch(
'homeassistant_cli.remote.wsapi', return_value={"result": "worked"}
) as mockmethod:

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand All @@ -103,7 +102,6 @@ def test_raw_ws_data() -> None:
with mocker.patch(
'homeassistant_cli.remote.wsapi', return_value={"result": "worked"}
) as mockmethod:

runner = CliRunner()
result = runner.invoke(
cli.cli,
Expand Down
1 change: 0 additions & 1 deletion tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def test_service_completion(default_services) -> None:
def test_service_call(default_services) -> None:
"""Test basic call of a service."""
with requests_mock.Mocker() as mock:

post = mock.post(
"http://localhost:8123/api/services/homeassistant/restart",
json={"result": "bogus"},
Expand Down