Announcement

Collapse
No announcement yet.

Creating Devices from Plugin - Parent/Child

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

    Creating Devices from Plugin - Parent/Child

    I am building my first Plugin, and it is working. However, the plugin creates a set of Devices with many children/features. But in Device display in HS these children show up in two places. As a feature under the parent, but also as a separate "top-level" device. It is technically the same device, all changes to e.g. value is shown on both.
    But all these extra devices clutters down the view. And if I set the "top-level" invisible, it disappears both as the parent-feature and "top-level" device.
    I have other third party plugins that create parent-feature devices, but these do not create the other "top-level" device.
    So there must be a way to avoid it, but how?

    #2
    Some screenshots would help

    [EDIT]

    Also example of your code

    Comment


      #3
      alexbk66 Yes, of course.

      Here is code for creating parent and children: (buildng on MoskusSample plugin)

      ' Create Price Device
      If (From d In devs Where d.Name(hs).Contains(TodayStats.DeviceName)).Count = 0 Then

      'There are no root device so let's create one, and keep the device reference
      Dim rootDeviceReference As Integer = CreatePContRootDevice(TodayStats.DeviceName, "PwrCntrl", "PowerPrice", "PwrCntrl")
      Dim root As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(rootDeviceReference)

      ' Price information:
      Dim childDeviceReference As Integer = CreatePContChildDevice("Price Now", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Average Daytime", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Average Night", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Average 24H", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Price Last Hour", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Price Next Hour", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Monthly Average", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      childDeviceReference = CreatePContChildDevice("Estimated Support", "PwrCntrl", "PowerPrice", "PwrCntrl")
      If childDeviceReference > 0 Then root.AssociatedDevice_Add(hs, childDeviceReference)

      End If
      'Committing to the database and return the reference TEST
      hs.SaveEventsDevices()

      And the result (some of them). As you can see e.g "Price Now" appears twice.

      Click image for larger version  Name:	Devices with children.jpg Views:	0 Size:	98.5 KB ID:	1583158 ​

      Comment


        #4
        And here are the two functions called:

        Private Function CreatePContRootDevice(ByVal name As String, ByVal Floor As String, ByVal Room As String, ByVal DevType As String) As Integer
        'Creating BASIC device and getting its device reference
        Dim device As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(CreateBasicPContDevice(name, Floor, Room, DevType, True, False))

        device.Device_Type_String(hs) = Me.Name & " " & "Root" & " " & DevType

        'Setting it as a root device
        device.Relationship(hs) = HomeSeerAPI.Enums.eRelationship.Parent_Root

        hs.SaveEventsDevices()
        Return device.Ref(hs)
        End Function

        Private Function CreatePContChildDevice(ByVal name As String, ByVal Floor As String, ByVal Room As String, ByVal DevType As String) As Integer
        'Creating BASIC device and getting its device reference
        Dim device As Scheduler.Classes.DeviceClass = hs.GetDeviceByRef(CreateBasicPContDevice(name, Floor, Room, DevType, True, False))
        device.Device_Type_String(hs) = Me.Name & " " & "Child" & " " & DevType

        'Setting it as a child device
        device.Relationship(hs) = HomeSeerAPI.Enums.eRelationship.Child

        hs.SaveEventsDevices()
        Return device.Ref(hs)
        End Function​

        Comment


          #5
          You need to add parent ref to the child "AssociatedDevice" list

          Click image for larger version

Name:	2022-12-27.png
Views:	95
Size:	55.4 KB
ID:	1583161

          Comment


            #6
            alexbk66 Thanks a lot. That made the trick. Added device.AssociatedDevice_Add(hs, ParentRef) on the childs.

            Comment


              #7
              No problem

              Comment

              Working...
              X