Announcement

Collapse
No announcement yet.

Resonding to a AddColorPicker feature

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

    Resonding to a AddColorPicker feature

    I am working on a plugin that will change colors for a TP-Link led bulb. I am struggling with how to work with a color picker on the device grid. I managed to get everything to work by creating my own device grid by using a feature page but I want to do use HS4 standard functionality. I can create the color picker feature by using the code:
    Code:
    FeatureFactory ff2 = FeatureFactory.CreateFeature(Util.gPlugin_id).WithName("bulb color").AddColorPicker(15, cl, EControlUse.ColorControl);
    This creates the color picker on the device grid as expected (see picture below).

    My questions
    1. What does the "targetValue" mean in the code for a colorpicker?
      Code:
      public FeatureFactory AddColorPicker(double targetValue, ControlLocation location = null, EControlUse controlUse = EControlUse.NotSpecified)
    2. Most importantly, how can I catch the event when the user clicks save? There is no value change event or any other HSEvent that I can tell but the HS log does show

      HTML Code:
      Device: HSPI_TPLinkSmartHome4 Plugin bulb color to (15) by/from: CAPI Control Handler
    3. How do I get the hex color that was selected?
    I appreciate any examples or guidance.
    James

    Running HS 3 on Win10 .

    #2
    1. if you don't have any other control attached to this feature, you can leave targetValue=0 for the color picker, if you have other controls just set it to a value that is not already taken by the other controls.

    2
    In your HSPI class, you have to override SetIOMulti

    Code:
    public override void SetIOMulti(List<ControlEvent> controlEvents) {
        foreach (var control in controlEvents) {
             //handle control event
        }
    }
    3. The color is passed as a RGB string in ControlEvent.ControlString

    Comment


      #3
      Got it. Still learning...
      I very much appreciate the help.
      James

      Running HS 3 on Win10 .

      Comment


        #4
        Originally posted by spud View Post

        3. The color is passed as an RGB string in ControlEvent.ControlString
        I am making good progress but...

        I've searched the API and the forum but can't find how I save/set the RGB string into the feature so that the feature shows pops up with the correct color.

        James

        Running HS 3 on Win10 .

        Comment


          #5
          Set DeviceValue as RRGGBB where RR is red hex scaled by 256*256 and GG is green hex scaled by 256 and BB is blue hex.

          Comment


            #6
            Originally posted by Michael McSharry View Post
            Set DeviceValue as RRGGBB where RR is red hex scaled by 256*256 and GG is green hex scaled by 256 and BB is blue hex.
            The device has both an on/off control(feature) and a separate feature with the color picker control. When I changed the feature value, it did not seem to make any difference. I used a hex to the decimal color calculator to come up with the value. Did I misread your suggestion?
            James

            Running HS 3 on Win10 .

            Comment


              #7
              what you did seems correct. when I get back home I will confirm my implementation. I know there were growing pains with color picker on HS4 but I thought they were resolved.

              Comment


                #8
                My code that takes the last 6 characters of a string as being RGB
                Code:
                sRGB = sValue.Substring(sValue.Length - 6, 6)
                Dim r As Integer = Convert.ToInt32(sRGB.Substring(0, 2), 16)
                Dim g As Integer = Convert.ToInt32(sRGB.Substring(2, 2), 16)
                Dim b As Integer = Convert.ToInt32(sRGB.Substring(4, 2), 16)
                Dim iRGB As Integer = (r << 16) + (g << 8) + b
                hs.UpdateFeatureValueByRef(iRef, CType(iRGB, Double))

                Comment


                  #9
                  It didn't work for me. Would you mind sharing how you created your color control? I am guessing, I didn't set up something correctly.
                  James

                  Running HS 3 on Win10 .

                  Comment


                    #10
                    The If False part is the HS3 code. The earlier part is the HS4 code

                    Code:
                    Sub ColorPickerType(ByVal iRef As Integer, ByVal bControlable As Boolean, ByVal oMQTT As MqttReport)
                    hs.ClearStatusGraphicsByRef(iRef)
                    hs.ClearStatusControlsByRef(iRef)
                    oMQTT.StorePayload = 0
                    Dim Pair As New Devices.Controls.StatusControl(Devices.Controls.EControlType .ColorPicker) With {
                    .IsRange = True
                    }
                    Dim Range As New HomeSeer.PluginSdk.Devices.ValueRange(0,&HFFFFFF) With {
                    .Prefix = "",
                    .Suffix = ""
                    }
                    Pair.ControlUse = HomeSeer.PluginSdk.Devices.Controls.EControlUse.ColorControl '.NotSpecified 'HomeSeerAPI.ePairControlUse.Not_Specified
                    Pair.TargetRange = Range
                    hs.AddStatusControlToFeature(iRef, Pair) 'hs.GetFeatureByRef(iRef).StatusControls.Add(Pair)
                    Pair = Nothing
                    Dim sGraphicPath As String = "/images/HomeSeer/status/"
                    hs.AddStatusGraphicToFeature(iRef, New HomeSeer.PluginSdk.Devices.StatusGraphic(sGraphicPath & "custom-color.png", CType(0, Double), CType(&HFFFFFF, Double)))
                    
                    #If False Then
                    Dim oRender As HomeSeerAPI.Enums.CAPIControlType
                    Dim vStatusControl As HomeSeerAPI.ePairStatusControl
                    If bControlable Then
                    oRender = HomeSeerAPI.Enums.CAPIControlType.Color_Picker
                    vStatusControl = HomeSeerAPI.ePairStatusControl.Both
                    Else
                    oRender = HomeSeerAPI.Enums.CAPIControlType.Not_Specified
                    vStatusControl = HomeSeerAPI.ePairStatusControl.Status
                    End If
                    Dim Pair As New HomeSeerAPI.VSPair(vStatusControl)
                    Pair.PairType = HomeSeerAPI.VSVGPairType.Range
                    Pair.RangeStart = 0
                    Pair.RangeEnd = &HFFFFFF
                    Pair.IncludeValues = True
                    Pair.ValueOffset = 0
                    Pair.RangeStatusPrefix = ""
                    Pair.RangeStatusSuffix = ""
                    Pair.ControlUse = HomeSeerAPI.ePairControlUse.Not_Specified
                    Pair.Render = oRender
                    hs.DeviceVSP_AddPair(iRef, Pair)
                    Pair = Nothing
                    oRender = Nothing
                    #End If
                    End Sub

                    Comment


                      #11
                      Originally posted by spud View Post
                      The color is passed as a RGB string in ControlEvent.ControlString
                      jasv this was the same in HS4, didn't change

                      Comment


                        #12
                        Originally posted by alexbk66 View Post

                        jasv this was the same in HS4, didn't change
                        I didn't use (didn't know how) that functionality in my HS3 plugin. I will try Michae's approach. I am trying to use the HS4 functionality based on the API documented framework on features which says that setting the value for a colorpicker will not work. See below.Which I am finding out that setting the value doesn't do anything in my testing. The doc (below) for the FeatureFactory.AddColorPicker says to use the control string but I don't see a way to set it so that the colorpicker pops up with something other than black. Any advice?

                        Click image for larger version

Name:	colorpicker.png
Views:	259
Size:	267.5 KB
ID:	1437654
                        James

                        Running HS 3 on Win10 .

                        Comment


                          #13
                          The value from HS Colorpicker to the plugin is passed via ControlString (as Spud explained) as hex string i.e. #FFFF00 for yellow. But in some cases the double value may be also used (i.e. if using EasyTrigger to set color). So you need to support both. Example from my IKEA plugin:
                          Code:
                          /// <summary>
                          /// This function is called from plugin SetIOMulti (for my devices) or HSEvent (other devices)
                          /// </summary>
                          /// <param name="value"></param>
                          /// <param name="cause"></param>
                          public override void NotifyValueChange(double value, string cause, string ControlString)
                          {
                              try
                              {
                                Colors.XY xy;
                          
                                if (!String.IsNullOrEmpty(ControlString))
                                {
                                   xy = Colors.ToXY(ControlString);
                                }
                                else
                                {
                                   Colors.RGB rgb = Colors.RGB.hexToRGB((uint)value);
                                   xy = Colors.ToXY(rgb);
                                }
                          
                                Log($"Setting {aType}: {value}/'{ControlString}' {xy.iX}-{xy.iY}");
                          
                                // DeviceIkeaLight converts RGB value to XY to send to devices
                                (parent as DeviceIkeaDevice).SetColorXY(tr_device, xy.iX, xy.iY, TransitionTime);
                              }
                              catch(Exception ex)
                              {
                                 LogErr($"NotifyValueChange({value}, '{ControlString}'): {ex}");
                              }
                          }

                          When setting the color back to the HS device I set both value and string:

                          Code:
                          public override void SetValue(double value)
                          {
                              base.SetValue(value);
                              if(value>0 && value<16777215)
                                  DeviceString = ((int)value).ToString("X8");
                          }
                          Note: this is from HS3, but I'm sure it is not much different in HS4

                          Comment


                            #14
                            And to convert the ControlString to RGB I use

                            Code:
                            public Color toRGB(string rgb) => System.Drawing.ColorTranslator.FromHtml($"#{rgb}");

                            Comment


                              #15
                              Michael McSharry, I did not understand this phrase,
                              Set DeviceValue as RRGGBB where RR is red hex scaled by 256*256 and GG is green hex scaled by 256 and BB is blue
                              What is meaning of scaled by 256 * 256 and scaled by 256?

                              Comment

                              Working...
                              X