Announcement

Collapse
No announcement yet.

Script to Fix Device Association Errors

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Script to Fix Device Association Errors

    If you have issues like I do when you start up HomeSeer where it pukes a bunch of warnings about issues with device relationships or if you try to bring up a device listing in HS Mobile and it spins forever when one of your Life 360 devices is in the filtered list, I wrote some code to fix it all up.

    As far as I can tell, this largely applies in 2 scenarios:
    1. You had originally created all the devices then removed some (either by deleting them manually or through the plugin, not sure)
    2. Your devices got stale and wouldn't update so you had to delete an entire person's set of devices, then it recreated them all wrong.
    The root cause (pun intended) is that the original author failed to set reciprocal associations between the parent and child. The parent knew about the children, so it mostly displayed ok, but the children didn't have the parent association (which seems stupid, but that's just how HS makes us do it...).

    Create a new script file then create an event that runs the "FixDeviceRef" method of that new script file, and provide it a comma separated list of your root Life 360 devices. (If you don't know how to find the device ref, you can grab it from the end of the URL when you click on the parent device to go into configuration, or you can turn on the HS option for developers that shows the ref as a column in your device list page.)

    Code:
    Sub FixDeviceRef(strArgs As String)
    
         Dim astrRefs() As String
         Dim aintAssoc() As Integer
    
         Dim devParent as Scheduler.Classes.DeviceClass
         Dim devChild as Scheduler.Classes.DeviceClass
    
         hs.WriteLog("Info", "Attempting to fix child associations for the following devices: " & strArgs)
    
         astrRefs = strArgs.Split(",")
    
         For Each ref As String In astrRefs
              devParent = hs.GetDeviceByRef(ref)
              If Not devParent Is Nothing Then
    
                   aintAssoc = devParent.AssociatedDevices(hs)
                   hs.WriteLog("Info", "Devices associated to parent " & devParent.Name(hs) & " (" & ref & "): " & String.Join(",", aintAssoc))
    
                   For Each i As Integer in aintAssoc
                        devChild = hs.GetDeviceByRef(i)
                        If Not devChild Is Nothing Then                        
                             hs.WriteLog("Info", "Adding association for " & devChild.Name(hs) & " (" & i & ") ...")
                             devChild.AssociatedDevice_ClearAll(hs)
                             devChild.AssociatedDevice_Add(hs, CType(ref, Integer))
                        Else
                             hs.WriteLog("ERROR", "FixDeviceRef couldn't find associated device " & i & " for " & devParent.Name(hs) & " - removing stale association...")
                             devParent.AssociatedDevice_Remove(hs, i)
                        End If
                   Next
    
              Else
                   hs.WriteLog("ERROR", "FixDeviceRef couldn't find parent device " & ref)
              End If
         Next
    
         hs.WriteLog("Info", "Complete.")
    
    End Sub
Working...
X