DewGew/Domoticz-Google-Assistant

Sync via token

kofec opened this issue · 5 comments

kofec commented

Is your feature request related to a problem? Please describe.
When I open a gate via remote control the google assistant has incorrect state of gate.

Describe the solution you'd like

diff --git a/smarthome.py b/smarthome.py
index 584d591..e23e959 100644
--- a/smarthome.py
+++ b/smarthome.py
@@ -820,6 +820,21 @@ class SmartHomeReqHandler(OAuthReqHandler):
         else:
             logger.error('Something went wrong, check your notification settings!')
 
+    def syncDevices_post(self, s):
+        logger.debug(s.headers)
+        a = s.headers.get('Authorization', None)
+        token = None
+        if a is not None:
+            types, tokenH = a.split()
+            if types.lower() == 'bearer':
+                token = Auth['tokens'].get(tokenH, None)
+                
+        if token is None:
+            raise SmartHomeError(ERR_PROTOCOL_ERROR, 'not authorized access!!')
+
+        r = self.forceDevicesSync()
+        s.send_message(200, 'Synchronization request sent, status_code: ' + str(r))
+
     def smarthome(self, s):
         s.send_message(500, "not supported")
 
@@ -827,7 +842,8 @@ class SmartHomeReqHandler(OAuthReqHandler):
         userAgent = self.getUserAgent()
         enableReport = ReportState.enable_report_state()
         if userAgent is None:
-            return 500  # internal error
+            # return 500  # internal error
+            userAgent = "1234"
 
         data = {"agentUserId": userAgent}
         if enableReport:
@@ -1360,7 +1376,8 @@ if 'userinterface' in configuration and configuration['userinterface'] == True:
 
     smarthomePostMappings = {"/smarthome": SmartHomeReqHandler.smarthome_post,
                              "/notification": SmartHomeReqHandler.notification_post,
-                             "/settings": SmartHomeReqHandler.settings_post}
+                             "/settings": SmartHomeReqHandler.settings_post,
+                             "/sync": SmartHomeReqHandler.syncDevices_post}
 else:
     smarthomeGetMappings = {"/smarthome": SmartHomeReqHandler.smarthome,
                             "/sync": SmartHomeReqHandler.syncDevices,
@@ -1371,7 +1388,8 @@ else:
                             "/pycast": SmartHomeReqHandler.pycast}  
 
     smarthomePostMappings = {"/smarthome": SmartHomeReqHandler.smarthome_post,
-                             "/notification": SmartHomeReqHandler.notification_post}
+                             "/notification": SmartHomeReqHandler.notification_post,
+                             "/sync": SmartHomeReqHandler.syncDevices_post}
 
 smarthomeControlMappings = {'action.devices.SYNC': SmartHomeReqHandler.smarthome_sync,
                             'action.devices.QUERY': SmartHomeReqHandler.smarthome_query,

I this still an issue?

kofec commented

@DewGew I don't see any commit in this repository so there is not code to support "sync" via token. Above you can see the code which I have to add.

kofec commented

Thanks