Announcement

Collapse
No announcement yet.

Sample Creating a Device

Collapse
This is a sticky topic.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sample Creating a Device

    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!)


    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)
    Attached Files
    Regards,

    Rick Tinker (a.k.a. "Tink")

  • #2
    Very, very nice! I like it!
    I'll use the weekend reading it line by line, making sure I understand it.

    First question
    "VSPair" and "VGPair" is read as "Value-String Pair" and "Value-Graphics pair?

    (If so, then why not call it "ValueStringPair" and "ValueGraphicPair"? It would make it easier to read... It's stuff like that I'm talking about...)


    And thank you to Wade!
    HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
    Running on Windows 10 (64) virtualized
    on ESXi (Fujitsu Primergy TX150 S8).
    WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

    Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

    Comment


    • #3
      Originally posted by Moskus View Post
      Very, very nice! I like it!
      I'll use the weekend reading it line by line, making sure I understand it.

      First question
      "VSPair" and "VGPair" is read as "Value-String Pair" and "Value-Graphics pair?

      (If so, then why not call it "ValueStringPair" and "ValueGraphicPair"? It would make it easier to read... It's stuff like that I'm talking about...)


      And thank you to Wade!
      For one thing, dashes are not allowed in variable or procedure names.

      Did you ever wonder where the programming language "C" got its name? Programmers do not like to type long variable names - I am better than most because I actually know how to type using more than 2 fingers - but still, variable names are variable depending upon the programmer, and we have thousands that are easily over 9 years old, so it takes a while to clean up that stuff.
      Regards,

      Rick Tinker (a.k.a. "Tink")

      Comment


      • #4
        Nice Tink:

        Thanks for the example.
        Don

        Comment


        • #5
          i always wondered why hs wasnt shortend to h , know i understand.

          1 year ago i was criticized for to long names in programming but the standard is now complete readable understandable meaningful naming in programming.
          Last edited by wetlip; January 12th, 2013, 03:59 AM.

          Comment


          • #6
            Originally posted by Rick Tinker View Post
            Did you ever wonder where the programming language "C" got its name? Programmers do not like to type long variable names - I am better than most because I actually know how to type using more than 2 fingers - but still, variable names are variable depending upon the programmer, and we have thousands that are easily over 9 years old, so it takes a while to clean up that stuff.
            It's true that programmer didn't like to use long names (or still don't do it in other languages), but there is a new thing in town, and I bet that you have heard of it:
            It's called IntelliSense, and it's built into Visual Studio so you don't even think of it.

            I know HomeSeer is "old", it's one of its strengths. And I get that you can't change too much, because it would completely destory backwards compability. But new functionality and variables could easily have been made to comply the .NET guidelines. It is after all a .NET program.

            What these APIs idealy should look like is something very similar to the .NET framework itself. Logical, structured, unabreviated names for functions and routines with plenty of comments. And the documentation is pretty good.


            But I'll try to shut up about it, even if it will be hard.
            I WILL of course use the SDK for all that it's worth, and I WILL convert my exitsting plugins and I do have some ideas for a couple of new ones.
            Last edited by Moskus; January 14th, 2013, 02:48 AM.
            HSPro 3.0.0.458, Z-NET with Z-wave plugin 3.0.1.190, RFXCOM + 2x RFXtrx433E, HSTouch, Squeezebox plugin, iTach IP/WF2IR & GC-100-6 with UltraGCIR, BLDenon, NetcamStudio, Jon00s Webpage builder, Harmony Hub plugin, SCSIP (with FreePBX), Arduino plugin, IFTTT, Pushalot plugin, Device History plugin.
            Running on Windows 10 (64) virtualized
            on ESXi (Fujitsu Primergy TX150 S8).
            WinSeer (for Win10) - TextSeer - FitbitSeer - HSPI_MoskusSample

            Are you Norwegian (or Scandinavian) and getting started with HomeSeer? Read the "HomeSeer School"!

            Comment


            • #7
              adding javascript links to head of homeseer page

              seeing all the thermostats png files in this example, i wonder if it is possible for plugin writers to put a reference to an extra javascript file to the head of the page. And add functions to the document ready part in the head of the page. this to implement jQuery to build the thermostat picture par example.

              here under is asimple javascript function added to the body of the page,
              it would be much easier as this function could be referenced without cutting and pasting the code with append.



              PHP Code:
               stb.Append("<script type='text/javascript'>")
                          
              stb.Append(" function radioclick(value1) {")
                          
              stb.Append(" {")
                          
              stb.Append("var id1 = 'Radio' + value1;")
                          
              stb.Append("var rb = document.getElementById(id1);")
                          
              stb.Append("  if ( rb.checked = true ) {")
                          
              'stb.Append(" var td = 'check' + value;")
                          stb.Append(" var dtu =document.getElementById('
              ovstbr1c2');")
                          stb.Append(" dtu.html = value1;")
                          stb.Append("$.post('
              /Sample Plugin', id1 +'=checked');")
                          stb.Append("}}}")
                          stb.Append("</script>")

                          stb.Append("<script type='
              text/javascript'>")




                          stb.Append("$(document).ready(function () {")
                          stb.Append("$(function () {")
                          stb.Append(" $('
              button[id^=buttonov]').click(function ()")
                          stb.Append(" {")
                          stb.Append("  $('
              button[id^=ov1]').click();")
                          stb.Append(" });")
                          stb.Append(" });")
                          stb.Append(" });")

                          stb.Append("</script>") 

              Comment


              • #8
                Sure there is!

                You can put something such as this:
                HTML Code:
                <script type="text/javascript" src="js/jquery-1.7.js"></script>
                (Replace the src path with the path to your JavaScript file)

                As the extra_meta parameter to the GetPageHeader call, or you can add it to \HTML\meta.htm which HomeSeer appends the contents of if it exists.
                Regards,

                Rick Tinker (a.k.a. "Tink")

                Comment


                • #9
                  I'm interested in learning a little more about parent/child devices.
                  Code:
                  dvParent.AssociatedDevice_Add(hs, dvRef)
                  This makes sense, and I've added it to my code. Is there anything that will change the way parent and child devices are displayed on the device management page? It would be really nice if the child devices were grouped visually underneath the parent. Maybe I'm missing something.
                  HS Pro 3.0 | Linux Ubuntu 16.04 x64 virtualized under Proxmox (KVM)
                  Hardware: Z-NET - W800 Serial - Digi PortServer TS/8 and TS/16 serial to Ethernet - Insteon PLM - RFXCOM - X10 Wireless
                  Plugins: HSTouch iOS and Android, RFXCOM, BlueIris, BLLock, BLDSC, BLRF, Insteon PLM (MNSandler), Device History, Ecobee, BLRing, Kodi, UltraWeatherWU3
                  Second home: Zee S2 with Z-Wave, CT101 Z-Wave Thermostat, Aeotec Z-Wave microswitches, HSM200 occupancy sensor, Ecolink Z-Wave door sensors, STI Driveway Monitor interfaced to Zee S2 GPIO pins.

                  Comment


                  • #10
                    Device Information tab will show you related devices, but no, they are not grouped together because that is one of the features of this - the child devices can change their name, location, location2 and they are still linked.

                    For example, in HS2 I put all of my Z-Wave root devices in one location (Root Devices) so that if upon rare occasion I need to make a change to a node, I know where to find its root. For devices that create Battery child devices, I put them all in one location so I can just view that one location to see if any batteries need replacing. For temperature sensors, they also go in a Temperatures location so at-a-glance, I can see temps around the house - they could also just stay in the room they belong in of course, but if they were slaved together, then the root and battery would be there too.

                    Linking them is critical though to the new API. You could find each device, get its interface and interfaceInstance properties and device_type, and then use it if you need it, or... You get one of the devices, look up that information, then using the associated device info you either get the parent which then leads you to all the child devices, or if the one you found is the parent then you instantly have access to all of the child devices so your enumeration can end.

                    So a parent device will have multiple associated devices, but child devices will have 1. For an added measure, there is a "Root" enum for the device types (music, thermostat, security, etc.) that can be used as an added measure to indicate the parent (root) device.
                    Regards,

                    Rick Tinker (a.k.a. "Tink")

                    Comment


                    • #11
                      Originally posted by reidfo View Post
                      I'm interested in learning a little more about parent/child devices.
                      Code:
                      dvParent.AssociatedDevice_Add(hs, dvRef)
                      This makes sense, and I've added it to my code. Is there anything that will change the way parent and child devices are displayed on the device management page? It would be really nice if the child devices were grouped visually underneath the parent. Maybe I'm missing something.
                      parent devices are associated with child devices and vice versa, how does homeseer knows which is the parent ? :

                      PHP Code:
                      dv.AssociatedDevice_Add(hsdvParent.Ref(hs)) 

                      Comment


                      • #12
                        Originally posted by wetlip View Post
                        parent devices are associated with child devices and vice versa, how does homeseer knows which is the parent ? :

                        PHP Code:
                        dv.AssociatedDevice_Add(hsdvParent.Ref(hs)) 
                        Your plugin will have to make that determination, most likely by the device type and/or subtype. In my case with themostats there is a device type for the thermostat root device. If you're doing something other than thermostats you could use the subtype property to define different types of child devices.
                        HS Pro 3.0 | Linux Ubuntu 16.04 x64 virtualized under Proxmox (KVM)
                        Hardware: Z-NET - W800 Serial - Digi PortServer TS/8 and TS/16 serial to Ethernet - Insteon PLM - RFXCOM - X10 Wireless
                        Plugins: HSTouch iOS and Android, RFXCOM, BlueIris, BLLock, BLDSC, BLRF, Insteon PLM (MNSandler), Device History, Ecobee, BLRing, Kodi, UltraWeatherWU3
                        Second home: Zee S2 with Z-Wave, CT101 Z-Wave Thermostat, Aeotec Z-Wave microswitches, HSM200 occupancy sensor, Ecolink Z-Wave door sensors, STI Driveway Monitor interfaced to Zee S2 GPIO pins.

                        Comment


                        • #13
                          From my earlier post...

                          So a parent device will have multiple associated devices, but child devices will have 1. For an added measure, there is a "Root" enum for the device types (music, thermostat, security, etc.) that can be used as an added measure to indicate the parent (root) device.
                          Regards,

                          Rick Tinker (a.k.a. "Tink")

                          Comment


                          • #14
                            Not to hijack this thread; I'd love to see you break down triggers and conditions like this excellent example.
                            Don

                            Comment


                            • #15
                              Originally posted by donstephens View Post
                              Not to hijack this thread; I'd love to see you break down triggers and conditions like this excellent example.
                              I think that was the intent when Wade created the sample plug-in "simple" - check that out and see if it helps.
                              Regards,

                              Rick Tinker (a.k.a. "Tink")

                              Comment

                              Working...
                              X