From e4f6edc19bc45835c19ffdbaaffc8d33143f0fb4 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 17 Jan 2023 20:10:34 +0100 Subject: implement json-rpc more correctly --- ShellyPy/gen2.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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): -- cgit v1.2.3