forked from rytilahti/python-miio
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiottemplate.py
More file actions
150 lines (112 loc) · 4.07 KB
/
Copy pathmiottemplate.py
File metadata and controls
150 lines (112 loc) · 4.07 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging
from pathlib import Path
import click
import requests
from containers import Device, ModelMapping
MIOTSPEC_MAPPING = Path("model_miotspec_mapping.json")
_LOGGER = logging.getLogger(__name__)
@click.group()
@click.option("-d", "--debug")
def cli(debug):
lvl = logging.INFO
if debug:
lvl = logging.DEBUG
logging.basicConfig(level=lvl)
class Generator:
def __init__(self, data):
self.data = data
def print_infos(self):
dev = Device.from_json(self.data)
click.echo(
f"Device {dev.type!r}: {dev.description} with {len(dev.services)} services"
)
for serv in dev.services:
click.echo(f"\n* Service {serv}")
if serv.properties:
click.echo("\n\t## Properties ##")
for prop in serv.properties:
click.echo(f"\t\tsiid {serv.iid}: {prop}")
if prop.value_list:
for value in prop.value_list:
click.echo(f"\t\t\t{value}")
if prop.value_range:
click.echo(f"\t\t\tRange: {prop.value_range}")
if serv.actions:
click.echo("\n\t## Actions ##")
for act in serv.actions:
click.echo(f"\t\tsiid {serv.iid}: {act}")
if serv.events:
click.echo("\n\t## Events ##")
for evt in serv.events:
click.echo(f"\t\tsiid {serv.iid}: {evt}")
def generate(self):
dev = Device.from_json(self.data)
for serv in dev.services:
_LOGGER.info("Service: %s", serv)
for prop in serv.properties:
_LOGGER.info(" * Property %s", prop)
for act in serv.actions:
_LOGGER.info(" * Action %s", act)
for ev in serv.events:
_LOGGER.info(" * Event %s", ev)
return dev.as_code()
@cli.command()
@click.argument("file", type=click.File())
def generate(file):
"""Generate pseudo-code python for given file."""
raise NotImplementedError(
"Disabled until miot support gets improved, please use print command instead"
)
data = file.read()
gen = Generator(data)
click.echo(gen.generate())
@cli.command(name="print")
@click.argument("file", type=click.File())
def _print(file):
"""Print out device information (props, actions, events)."""
data = file.read()
gen = Generator(data)
gen.print_infos()
@cli.command()
def download_mapping():
"""Download model<->urn mapping."""
click.echo(f"Downloading and saving model<->urn mapping to {MIOTSPEC_MAPPING.name}")
url = "http://miot-spec.org/miot-spec-v2/instances?status=all"
res = requests.get(url, timeout=5)
with MIOTSPEC_MAPPING.open("w") as f:
f.write(res.text)
def get_mapping() -> ModelMapping:
with MIOTSPEC_MAPPING.open("r") as f:
return ModelMapping.from_json(f.read())
@cli.command()
def list():
"""List all entries in the model<->urn mapping file."""
mapping = get_mapping()
for inst in mapping.instances:
click.echo(f"* {repr(inst)}")
@cli.command()
@click.option("--urn", default=None)
@click.argument("model", required=False)
@click.pass_context
def download(ctx, urn, model):
"""Download description file for model."""
if urn is None:
if model is None:
click.echo("You need to specify either the model or --urn")
return
if not MIOTSPEC_MAPPING.exists():
click.echo(
f"miotspec mapping doesn't exist, downloading to {MIOTSPEC_MAPPING.name}"
)
ctx.invoke(download_mapping)
mapping = get_mapping()
model = mapping.info_for_model(model)
url = f"https://miot-spec.org/miot-spec-v2/instance?type={model.type}"
click.echo(f"Going to download {url}")
content = requests.get(url, timeout=5)
save_to = model.filename
click.echo(f"Saving data to {save_to}")
with open(save_to, "w") as f:
f.write(content.text)
if __name__ == "__main__":
cli()