-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathservice.py
More file actions
122 lines (105 loc) · 3.72 KB
/
service.py
File metadata and controls
122 lines (105 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Service plugin for Home Assistant CLI (hass-cli)."""
import logging
import re as reg
import sys
from typing import Any, Dict, List, Pattern # noqa: F401
import click
import homeassistant_cli.autocompletion as autocompletion
from homeassistant_cli.cli import pass_context
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__)
@click.group('service')
@pass_context
def cli(ctx):
"""Call and work with services."""
@cli.command('list')
@click.argument('servicefilter', default=".*", required=False)
@pass_context
def list_cmd(ctx: Configuration, servicefilter):
"""Get list of services."""
ctx.auto_output('table')
services = api.get_services(ctx)
service_filter = servicefilter
result = [] # type: List[Dict[Any,Any]]
if service_filter == ".*":
result = services
else:
result = services
service_filter_re = reg.compile(service_filter) # type: Pattern
domains = []
for domain in services:
domain_name = domain['domain']
domain_data = {}
services_dict = domain['services']
service_data = {}
for service in services_dict:
if service_filter_re.search(
"{}.{}".format(domain_name, service)
):
service_data[service] = services_dict[service]
if service_data:
domain_data["services"] = service_data
domain_data["domain"] = domain_name
domains.append(domain_data)
result = domains
flatten_result = [] # type: List[Dict[str,Any]]
for domain in result:
for service in domain['services']:
item = {}
item['domain'] = domain['domain']
item['service'] = service
item = {**item, **domain['services'][service]}
flatten_result.append(item)
cols = [
('DOMAIN', 'domain'),
('SERVICE', 'service'),
('DESCRIPTION', 'description'),
]
ctx.echo(
format_output(
ctx, flatten_result, columns=ctx.columns if ctx.columns else cols
)
)
@cli.command('call')
@click.argument(
'service',
required=True,
shell_complete=autocompletion.services, # type: ignore
)
@click.option(
'--arguments', help="""Comma separated key/value pairs 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(
'--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 call(ctx: Configuration, service, data=None):
"""Call a service."""
ctx.auto_output('data')
_LOGGING.debug("service call <start>")
parts = service.split(".")
if len(parts) != 2:
_LOGGING.error("Service name not following <domain>.<service> format")
sys.exit(1)
_LOGGING.debug("calling %s.%s(%s)", parts[0], parts[1], data)
result = api.call_service(ctx, parts[0], parts[1], data)
_LOGGING.debug("Formatting output")
ctx.echo(format_output(ctx, result))