forked from home-assistant-libs/aioshelly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.py
153 lines (118 loc) · 3.5 KB
/
verify.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
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
151
152
153
"""Verify script that downloads all Coiot examples from the Shelly website and checks to make sure that we can parse them."""
import json
import logging
import re
import urllib.parse
from dataclasses import dataclass, field
from unittest.mock import Mock
import requests
import urllib3
from aiohttp.helpers import reify
import aioshelly
urllib3.disable_warnings()
BASE_URL = "https://shelly-api-docs.shelly.cloud/docs/coiot/v2/examples/"
_LOGGER = logging.getLogger(__name__)
@dataclass
class CoiotExample:
"""CoiotExample class."""
filename: str
_cache: dict = field(default_factory=dict)
@reify
def name(self):
"""Get filename."""
return urllib.parse.unquote(self.filename)
@reify
def url(self):
"""Get file URL."""
return BASE_URL + self.filename
@reify
def content(self):
"""Get file content."""
return requests.get(self.url, verify=False).text
@reify
def content_parsed(self):
"""Parse file."""
lines = self.content.split("\n")
parsed = []
start = None
for i, line in enumerate(lines):
if line.rstrip() == "{":
start = i
elif line.rstrip() == "}":
parsed.append(lines[start : i + 1])
if len(parsed) != 2:
raise ValueError("Uuh, not length 2")
processed = []
for value in parsed:
text = "\n".join(value).strip()
try:
processed.append(json.loads(text))
except ValueError:
_LOGGER.error("Error parsing %s", self.url)
_LOGGER.exception(text)
raise
return processed
@reify
def cit_s(self):
"""Return parsed cit/s."""
return self.content_parsed[0]
@reify
def cit_d(self):
"""Return parsed cit/d."""
return self.content_parsed[1]
@reify
def device(self):
"""Create mocked device."""
device = aioshelly.Device(Mock(), None, aioshelly.ConnectionOptions("mock-ip"))
device._update_d(self.cit_d)
device._update_s(self.cit_s)
return device
def coiot_examples():
"""Get coiot examples."""
index = requests.get(
BASE_URL,
# Not sure, local machine barfs on their cert
verify=False,
).text
return [
CoiotExample(match)
for match in re.findall(r'href="(.+?)"', index)
if match.startswith("Shelly")
]
def print_example(example):
"""Print example."""
print(example.name)
print()
for block in example.device.blocks:
print(block)
for attr, value in block.current_values().items():
info = block.info(attr)
if value is None:
value = "None"
if aioshelly.BLOCK_VALUE_UNIT in info:
unit = " " + info[aioshelly.BLOCK_VALUE_UNIT]
else:
unit = ""
print(f"{attr.ljust(16)}{value}{unit}")
print()
print("-" * 32)
print()
def run():
"""Run coiot_examples and print errors."""
errors = []
for example in coiot_examples():
try:
print_example(example)
except Exception as err:
errors.append((example, err))
break
for example, err in errors:
print("Error fetching", example.name)
print(example.url)
print()
_LOGGER.error("", exc_info=err)
print()
print("-" * 32)
print()
if __name__ == "__main__":
run()