aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2023-01-17 20:10:34 +0100
committerJan200101 <sentrycraft123@gmail.com>2023-01-17 20:10:34 +0100
commite4f6edc19bc45835c19ffdbaaffc8d33143f0fb4 (patch)
treeaccfeba20bed607b2288f200f4efaeb6c7695b7b
parentb833cd7f981e96e2d89ef3cb0841e45d7147b01d (diff)
downloadShellyPy-e4f6edc19bc45835c19ffdbaaffc8d33143f0fb4.tar.gz
ShellyPy-e4f6edc19bc45835c19ffdbaaffc8d33143f0fb4.zip
implement json-rpc more correctly
-rw-r--r--ShellyPy/gen2.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/ShellyPy/gen2.py b/ShellyPy/gen2.py
index 21ffe84..96747c9 100644
--- a/ShellyPy/gen2.py
+++ b/ShellyPy/gen2.py
@@ -25,6 +25,7 @@ class ShellyGen2(ShellyBase):
super().__init__(ip, port, *args, **kwargs)
self.__generation__ = 2
+ self.payload_id = 1
def update(self):
status = self.settings()
@@ -35,8 +36,14 @@ class ShellyGen2(ShellyBase):
def post(self, page, values = None):
url = "{}://{}:{}/rpc".format(self.__PROTOCOL__, self.__ip__, self.__port__)
+ # increment payload id globally
+ self.payload_id += 1
+ # but keep a local copy around so we face no race conditions
+ payload_id = self.payload_id
+
payload = {
- "id": 1,
+ "jsonrpc": "2.0",
+ "id": payload_id,
"method": page,
}
@@ -68,6 +75,9 @@ class ShellyGen2(ShellyBase):
else:
raise BadResponse("{}: {}".format(error_code, error_message))
+ if response_data["id"] != payload_id:
+ raise BadResponse("invalid payload id was returned")
+
return response_data.get("result", {})
def status(self):