forked from rytilahti/python-miio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiottemplate.py
65 lines (45 loc) · 1.37 KB
/
miottemplate.py
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
import logging
import click
from containers import Device
_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 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."""
data = file.read()
gen = Generator(data)
print(gen.generate())
@cli.command()
@click.argument("type")
def download(type):
"""Download description file for model."""
import requests
url = f"https://miot-spec.org/miot-spec-v2/instance?type={type}"
content = requests.get(url)
save_to = f"{type}.json"
click.echo(f"Saving data to {save_to}")
with open(save_to, "w") as f:
f.write(content.text)
if __name__ == "__main__":
cli()