|
| 1 | +"""Raw plugin for Home Assistant CLI (hass-cli).""" |
| 2 | +import json as json_ |
| 3 | +import logging |
| 4 | + |
| 5 | +import click |
| 6 | + |
| 7 | +from homeassistant_cli.cli import pass_context |
| 8 | +from homeassistant_cli.config import Configuration |
| 9 | +from homeassistant_cli.helper import format_output |
| 10 | +import homeassistant_cli.remote as api |
| 11 | + |
| 12 | +_LOGGING = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +def _report(ctx, cmd, method, response) -> None: |
| 16 | + |
| 17 | + response.raise_for_status() |
| 18 | + |
| 19 | + if response.ok: |
| 20 | + try: |
| 21 | + ctx.echo(format_output(ctx, response.json())) |
| 22 | + except json_.decoder.JSONDecodeError: |
| 23 | + _LOGGING.debug("Response could not be parsed as JSON") |
| 24 | + ctx.echo(response.text) |
| 25 | + else: |
| 26 | + _LOGGING.warning( |
| 27 | + "%s: <No output returned from %s %s>", |
| 28 | + response.status_code, |
| 29 | + cmd, |
| 30 | + method, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def _handle(ctx, method, httpmethod='get') -> None: |
| 35 | + |
| 36 | + method = "/api/hassio/" + method |
| 37 | + response = api.restapi(ctx, httpmethod, method) |
| 38 | + |
| 39 | + _report(ctx, httpmethod, method, response) |
| 40 | + |
| 41 | + |
| 42 | +@click.group('hassio') |
| 43 | +@pass_context |
| 44 | +def cli(ctx: Configuration): |
| 45 | + """Hass.io commands (if available).""" |
| 46 | + ctx.auto_output("data") |
| 47 | + |
| 48 | + |
| 49 | +@cli.group('supervisor') |
| 50 | +@pass_context |
| 51 | +def supervisor(ctx: Configuration): |
| 52 | + """Hass.io supervisor commands.""" |
| 53 | + ctx.auto_output("data") |
| 54 | + |
| 55 | + |
| 56 | +@supervisor.command() |
| 57 | +@pass_context |
| 58 | +def info(ctx: Configuration): |
| 59 | + """Hass.io supervisor info.""" |
| 60 | + _handle(ctx, 'supervisor/info') |
| 61 | + |
| 62 | + |
| 63 | +@supervisor.command() |
| 64 | +@pass_context |
| 65 | +def logs(ctx: Configuration): |
| 66 | + """Hass.io supervisor logs.""" |
| 67 | + _handle(ctx, 'supervisor/logs') |
| 68 | + |
| 69 | + |
| 70 | +@supervisor.command() |
| 71 | +@pass_context |
| 72 | +def reload(ctx: Configuration): |
| 73 | + """Hass.io supervisor reload.""" |
| 74 | + _handle(ctx, 'supervisor/reload', 'post') |
| 75 | + |
| 76 | + |
| 77 | +@cli.group('host') |
| 78 | +@pass_context |
| 79 | +def host(ctx: Configuration): |
| 80 | + """Hass.io Host commands.""" |
| 81 | + ctx.auto_output('data') |
| 82 | + |
| 83 | + |
| 84 | +@host.command('logs') |
| 85 | +@pass_context |
| 86 | +def hostlogs(ctx: Configuration): |
| 87 | + """Hass.io host logs.""" |
| 88 | + _handle(ctx, 'host/info') |
| 89 | + |
| 90 | + |
| 91 | +@host.command() |
| 92 | +@pass_context |
| 93 | +def reboot(ctx: Configuration): |
| 94 | + """Hass.io host reboot.""" |
| 95 | + _handle(ctx, 'host/reboot', 'post') |
| 96 | + |
| 97 | + |
| 98 | +@host.command('reload') |
| 99 | +@pass_context |
| 100 | +def hostreload(ctx: Configuration): |
| 101 | + """Hass.io host reload.""" |
| 102 | + _handle(ctx, 'host/reload', 'post') |
| 103 | + |
| 104 | + |
| 105 | +@host.command() |
| 106 | +@pass_context |
| 107 | +def shutdown(ctx: Configuration): |
| 108 | + """Hass.io host shutdown.""" |
| 109 | + _handle(ctx, 'host/shutdown', 'post') |
0 commit comments