From: Giorgio Ravera Date: Mon, 23 Nov 2020 16:31:27 +0000 (+0100) Subject: Added last update information in human readable. X-Git-Tag: v0.5~6 X-Git-Url: http://git.giorgioravera.it/?a=commitdiff_plain;h=dda6963a2243e8a15afdb8c7e18a656415d9ac13;p=mercedes_me_api.git Added last update information in human readable. --- diff --git a/README.md b/README.md index cdc3a54..d751b91 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # mercedes_me_api -[![License Status][license-img]][license-url] [![Releases][releases-img]][releases-url] [![Last Commit][last-commit-img]][last-commit-url] [![hacs][hacs-img]][hacs-url] +[![License Status][license-img]][license-url] [![BuyMeCoffee][buymecoffee-img]][buymecoffee-url] Script to use Mercedes Me APIs. diff --git a/custom_components/mercedesmeapi/resources.py b/custom_components/mercedesmeapi/resources.py index 6c3754b..a6fbece 100644 --- a/custom_components/mercedesmeapi/resources.py +++ b/custom_components/mercedesmeapi/resources.py @@ -6,7 +6,7 @@ Author: G. Ravera For more details about this component, please refer to the documentation at https://github.com/xraver/mercedes_me_api/ """ -from datetime import timedelta +from datetime import datetime import logging import json import os @@ -29,6 +29,10 @@ class MercedesMeResource (Entity): self._state = state self._timestamp = timestamp self._valid = valid + if(timestamp != None): + self._lastupdate = datetime.fromtimestamp(self._timestamp/1000) + else: + self._lastupdate = 0 def __str__(self): return json.dumps({ @@ -52,6 +56,13 @@ class MercedesMeResource (Entity): "valid" : self._valid, }) + def UpdateState(self, state, timestamp): + """Update status of the resource.""" + self._state = state + self._timestamp = timestamp + self._lastupdate = datetime.fromtimestamp(self._timestamp/1000) + self._valid = True + @property def name(self): """Return the name of the sensor.""" @@ -68,6 +79,7 @@ class MercedesMeResource (Entity): return ({ "valid": self._valid, "timestamp": self._timestamp, + "last_update": self._lastupdate, }) # @property @@ -197,6 +209,7 @@ class MercedesMeResources: print ("\tvalid: " + str(res._valid)) print ("\tstate: " + res._state) print ("\ttimestamp: " + str(res._timestamp)) + print ("\tlast_update: " + str(res._lastupdate)) ######################## # Update Resources State @@ -205,8 +218,6 @@ class MercedesMeResources: for res in self.database: result = GetResource(URL_RES_PREFIX + res._href, self.mercedesConfig) if not "reason" in result: - res._valid = True - res._timestamp = result[res._name]["timestamp"] - res._state = result[res._name]["value"] + res.UpdateState(result[res._name]["value"], result[res._name]["timestamp"]) # Write Resource File self.WriteResourcesFile() diff --git a/resources.py b/resources.py index 09528d5..9ac7c16 100644 --- a/resources.py +++ b/resources.py @@ -6,6 +6,7 @@ Author: G. Ravera For more details about this component, please refer to the documentation at https://github.com/xraver/mercedes_me_api/ """ +from datetime import datetime import logging import json import os @@ -26,6 +27,10 @@ class MercedesMeResource: self._state = state self._timestamp = timestamp self._valid = valid + if(timestamp != None): + self._lastupdate = datetime.fromtimestamp(self._timestamp/1000) + else: + self._lastupdate = 0 def __str__(self): return json.dumps({ @@ -49,6 +54,13 @@ class MercedesMeResource: "valid" : self._valid, }) + def UpdateState(self, state, timestamp): + """Update status of the resource.""" + self._state = state + self._timestamp = timestamp + self._lastupdate = datetime.fromtimestamp(self._timestamp/1000) + self._valid = True + def name(self): """Return the name of the sensor.""" return self._vin + "_" + self._name @@ -62,15 +74,17 @@ class MercedesMeResource: return ({ "valid": self._valid, "timestamp": self._timestamp, + "last_update": self._lastupdate, }) def update(self): """Fetch new state data for the sensor.""" result = GetResource(URL_RES_PREFIX + self._href, self._config) if not "reason" in result: - self._valid = True - self._timestamp = result[self._name]["timestamp"] self._state = result[self._name]["value"] + self._timestamp = result[self._name]["timestamp"] + self._lastupdate = datetime.fromtimestamp(self._timestamp/1000) + self._valid = True class MercedesMeResources: @@ -194,6 +208,7 @@ class MercedesMeResources: print ("\tvalid: " + str(res._valid)) print ("\tstate: " + res._state) print ("\ttimestamp: " + str(res._timestamp)) + print ("\tlast update: " + str(res._lastupdate)) ######################## # Update Resources State @@ -202,8 +217,6 @@ class MercedesMeResources: for res in self.database: result = GetResource(URL_RES_PREFIX + res._href, self.mercedesConfig) if not "reason" in result: - res._valid = True - res._timestamp = result[res._name]["timestamp"] - res._state = result[res._name]["value"] + res.update(result[res._name]["value"], result[res._name]["timestamp"]) # Write Resource File self.WriteResourcesFile()