Wade I think is working on a simplification of the Sample plug-in (thanks Wade) but for those of you having trouble with device creation, let me see if this sample code snippet helps...
It is also as a VB module in the enclosed zip so you can add it to a plug-in and view it in all its full color glory (and copy/paste as desired!)
It is also as a VB module in the enclosed zip so you can add it to a plug-in and view it in all its full color glory (and copy/paste as desired!)
Code:
Dim dvRef As Integer Try dvRef = hs.NewDeviceRef("My Device Name") Catch ex As Exception dvRef = -1 End Try If dvRef < 1 Then ' Log an error Exit Sub End If ' Force HomeSeer to save changes to devices and events so we can find our new device hs.SaveEventsDevices() ' Get the new device. Dim dv As Scheduler.Classes.DeviceClass = Nothing Try dv = hs.GetDeviceByRef(dvRef) Catch ex As Exception dv = Nothing End Try If dv Is Nothing Then ' Log an error Exit Sub End If ' Make the value/status pairs show drop-down control options. dv.MISC_Set(hs, Enums.dvMISC.SHOW_VALUES) dv.Location(hs) = "My Plugin Location" dv.Location2(hs) = "First Floor" dv.Interface(hs) = IFACE_NAME dv.InterfaceInstance(hs) = "Instance, if applicable" dv.Address(hs) = "XYZ-04-PDQ" dv.Code(hs) = hs.GetNextVirtualCode Dim DT As New DeviceTypeInfo DT.Device_API = DeviceTypeInfo.eDeviceAPI.Security DT.Device_Type = DeviceTypeInfo.eDeviceType_Security.Keypad DT.Device_SubType = 4 DT.Device_SubType_Description = "Keypad Address 4" dv.DeviceType_Set(hs) = DT dv.Device_Type_String(hs) = IFACE_NAME & " Keypad" ' Make sure we can have control options on this device by verifying that the status only bit is cleared. dv.MISC_Clear(hs, Enums.dvMISC.STATUS_ONLY) ' Also make sure that the option to prevent the value/status pairs from displaying as trigger options is cleared. dv.MISC_Clear(hs, Enums.dvMISC.NO_STATUS_TRIGGER) ' Mark the device that we can be polled for status. dv.Status_Support(hs) = True ' Make sure our device's string value is not set, as that will override the value/status pairs: hs.SetDeviceString(dvRef, "", False) ' This device is a child device, the parent being the root device for the entire security system. ' As such, this device needs to be associated with the root (Parent) device. If dvParent.AssociatedDevices_Count(hs) < 1 Then ' There are none added, so it is OK to add this one. dvParent.AssociatedDevice_Add(hs, dvRef) Else Dim Found As Boolean = False For Each ref As Integer In dvParent.AssociatedDevices(hs) If ref = dvRef Then Found = True Exit For End If Next If Not Found Then dvParent.AssociatedDevice_Add(hs, dvRef) Else ' This is an error condition likely as this device's reference ID should not already be associated. End If End If ' Now, we want to make sure our child device also reflects the relationship by adding the parent to ' the child's associations. dv.AssociatedDevice_ClearAll(hs) ' There can be only one parent, so make sure by wiping these out. dv.AssociatedDevice_Add(hs, dvParent.Ref(hs)) ' Now, let's store some information our plug-in uses in the device... Dim PData As clsPlugExtraData = Nothing PData = dv.PlugExtraData_Get(hs) If PData Is Nothing Then PData = New clsPlugExtraData ' I am going to store the security keypad codes in the device - nobody would think to look there! PData.AddNamed("KeyPadData", SecurityData) ' Now we need to put the data back... dv.PlugExtraData_Set(hs) = PData ' OK, now we need to add our status and control pairs.... ' In this part of the demo, we are going to set it up as if it were something that shows us ' the temperature, but has a control option for requesting a temperature update, just to make it ' interesting because I could not think of a good example for a 'range' value/status pair continuing ' with the security system example! Dim Pair As VSPair = Nothing Dim GPair As VGPair = Nothing ' First, clear away anything that was put there by default by HomeSeer hs.DeviceVSP_ClearAll(dvRef, True) ' This pair will display the temperature. Pair = New VSPair(HomeSeerAPI.ePairStatusControl.Status) Pair.PairType = VSVGPairType.Range Pair.RangeStart = -500 'This is a bit chilly Pair.RangeEnd = 1000 'This is pretty warm! Pair.RangeStatusPrefix = "" Pair.RangeStatusSuffix = " " & VSPair.ScaleReplace Pair.IncludeValues = True Pair.ValueOffset = 0 Pair.HasScale = True If Not hs.DeviceVSP_AddPair(dvRef, Pair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding pair failed.", COLOR_RED) ' These two pairs will result in a control button (Update) being available, and when that is selected ' the status will change to "Getting Temperature..." until a new temperature is set. Pair = New VSPair(ePairStatusControl.Control) ' This is what appears in the device actions... Pair.PairType = VSVGPairType.SingleValue Pair.Render = VSVGPairRender.Button Pair.Status = "Update" Pair.Value = -1000 ' This is outside the range used by temperature. If Not hs.DeviceVSP_AddPair(dvRef, Pair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding pair failed.", COLOR_RED) Pair = New VSPair(ePairStatusControl.Status) ' This is what appears for the status... Pair.PairType = VSVGPairType.SingleValue Pair.Status = "Getting Temperature..." Pair.Value = -1000 ' This is outside the range used by temperature. If Not hs.DeviceVSP_AddPair(dvRef, Pair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding pair failed.", COLOR_RED) ' Now, let's set up the graphics, but first we need to wipe out the HomeSeer defaults... hs.DeviceVGP_ClearAll(dvRef, True) ' We won't bother setting up a graphic for the -1000 value (update) as it should not be displayed for very long. ' Set up the ranges for Fahrenheit for the graphics. GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = -50 GPair.RangeEnd = 5 GPair.Graphic = "/images/HomeSeer/status/Thermometer-00.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 5.00000001 GPair.RangeEnd = 15.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-10.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 16 GPair.RangeEnd = 25.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-20.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 26 GPair.RangeEnd = 35.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-30.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 36 GPair.RangeEnd = 45.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-40.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 46 GPair.RangeEnd = 55.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-50.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 56 GPair.RangeEnd = 65.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-60.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 66 GPair.RangeEnd = 75.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-70.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 76 GPair.RangeEnd = 85.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-80.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 86 GPair.RangeEnd = 95.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-90.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 96 GPair.RangeEnd = 104.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-100.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED) GPair = New VGPair() GPair.PairType = VSVGPairType.Range GPair.RangeStart = 105 GPair.RangeEnd = 150.99999999 GPair.Graphic = "/images/HomeSeer/status/Thermometer-110.png" If Not hs.DeviceVGP_AddPair(dvRef, GPair) Then hs.WriteLogEx(IFACE_NAME & " Error", "Adding graphic pair failed.", COLOR_RED)
Comment