sincze / Domoticz-Growatt-Webserver-Plugin

16 stars 10 forks source link

onMessage: check first the "Status" before getting the "Data" element. #27

Closed JoostDkr closed 1 year ago

JoostDkr commented 1 year ago

Last days I received the Python-error "'KeyError': ('Data',)"

I found out that the HTTP response did not contain a lot of data when this error occurred. For example the "Data" element is missing. In that case the status was 588.

The solution to prevent the phyton-error, I moved then line "strData = Data["Data"].decode("utf-8", "ignore")" to under the line "if (Status == 200)".

So if now status 588 is received ( there is no 'data" element) there will be no phyton-error but the plugin report the error.

By the way: at error 400 an 500, the plugin report en Google error. Should be a Growatt error.

sincze commented 1 year ago

HI Thank you for your remark. Do you mean:

    def onMessage(self, Connection, Data):
        DumpHTTPResponseToLog(Data)
        Status = int(Data["Status"])

        if (Status == 200):
            strData = Data["Data"].decode("utf-8", "ignore")
            LogMessage(strData)
            apiResponse = json.loads(strData)
            Domoticz.Debug("Retrieved following json: "+json.dumps(apiResponse))
JoostDkr commented 1 year ago

Yep!

sincze commented 1 year ago

CLosed and merged