]> git.giorgioravera.it Git - mercedes_me_api.git/commitdiff
Added last update information in human readable.
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Mon, 23 Nov 2020 16:31:27 +0000 (17:31 +0100)
committerGiorgio Ravera <giorgio.ravera@gmail.com>
Thu, 17 Dec 2020 10:13:31 +0000 (11:13 +0100)
README.md
custom_components/mercedesmeapi/resources.py
resources.py

index dc2bd3b871c8d9d6270a6c97a834934544b570c9..d19ee6ee8f597d86579e425be8b2e8c22425b4bd 100644 (file)
--- 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.
index 6c3754b8a379be92241fd6da26396a6aec775eca..a6fbece4a6f8d88692c35c877697d6a2dd47c905 100644 (file)
@@ -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()
index 09528d59f6bc7f3ede41fede96a02d75bba99409..9ac7c16bcb2f2fb075dedc3647986679f869ca01 100644 (file)
@@ -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()