Skip to content

Commit ed820d2

Browse files
committed
Add rudimentary simulator for integration testing
Requires Home Assistant wmspro integration diagnostics file.
1 parent 3b923c3 commit ed820d2

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
2+
simulator/diagnostics/
23
dist/
34
*.pyc

simulator/simulator.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from http.server import BaseHTTPRequestHandler, HTTPServer
2+
import json
3+
import os
4+
import sys
5+
6+
diag = {}
7+
8+
9+
class Simulator(BaseHTTPRequestHandler):
10+
def do_GET(self):
11+
return self.do_POST()
12+
13+
def do_POST(self):
14+
if self.path != "/commonCommand":
15+
return self.send_error(404)
16+
17+
length = int(self.headers.get("content-length") or 0)
18+
body = json.loads(self.rfile.read(length).decode("utf-8"))
19+
if body.get("protocolVersion") != "1.0" or body.get("source") != 2:
20+
return self.send_error(400)
21+
22+
command = body.get("command")
23+
return getattr(self, command)(command, body)
24+
25+
def respond(self, command, response):
26+
data = {"command": command, "protocolVersion": "1.0.0"}
27+
data.update(response)
28+
self.send_response(200)
29+
self.send_header("Content-type", "application/json")
30+
self.end_headers()
31+
self.wfile.write(json.dumps(data).encode("utf-8"))
32+
33+
def ping(self, command, request):
34+
self.respond(command, {"status": 0})
35+
36+
def getConfiguration(self, command, request):
37+
self.respond(command, diag["data"]["config"])
38+
39+
def getStatus(self, command, request):
40+
dest = str(request["destinations"].pop())
41+
self.respond(command, diag["data"]["dests"][dest]["status"])
42+
43+
def action(self, command, request):
44+
actions = request["actions"]
45+
for action in actions:
46+
actionId = int(action["actionId"])
47+
destinationId = int(action["destinationId"])
48+
dest = str(action["destinationId"])
49+
details = diag["data"]["dests"][dest]["status"]["details"]
50+
for detail in details:
51+
if detail["destinationId"] == destinationId:
52+
data = detail["data"]["productData"]
53+
for item in data:
54+
if item["actionId"] == actionId:
55+
item["value"].update(action["parameters"])
56+
self.respond(command, {"status": 0})
57+
58+
59+
def main(server_class=HTTPServer, handler_class=Simulator):
60+
if len(sys.argv) != 2:
61+
print("Usage: python simulator.py <file>")
62+
sys.exit(1)
63+
64+
if not os.path.isfile(sys.argv[1]):
65+
print(f"File {sys.argv[1]} not found")
66+
sys.exit(1)
67+
68+
with open(sys.argv[1], "r", encoding="utf-8") as f:
69+
global diag
70+
diag = json.load(f)
71+
72+
server_address = ("", 80)
73+
httpd = server_class(server_address, handler_class)
74+
httpd.serve_forever()
75+
76+
77+
if __name__ == "__main__":
78+
main()

0 commit comments

Comments
 (0)