Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple test exception help & other questions #317

Open
Exergist opened this issue Jul 4, 2024 · 5 comments
Open

Simple test exception help & other questions #317

Exergist opened this issue Jul 4, 2024 · 5 comments

Comments

@Exergist
Copy link

Exergist commented Jul 4, 2024

Hey Michiel I'm trying to migrate to the newer HueApi content and I'm running into an issue.

using HueApi.Models;
using HueApi.Models.Requests;
using HueApi.ColorConverters.Original.Extensions;

...

 public async void SimpleTest()
 {
     var bridge = (await new HueApi.BridgeLocator.LocalNetworkScanBridgeLocator().LocateBridgesAsync(TimeSpan.FromSeconds(15))).ToList()[0]; // Find (first) Hue Bridge
     var registrationResult = HueApi.LocalHueApi.RegisterAsync(bridge.IpAddress, "MyApp", "MyDevice", true).Result; // Attempt to register application with first Hue Bridge found
     var ip = registrationResult.Ip;
     var key = registrationResult.StreamingClientKey;
     if (ip == null || key == null) // Check if application registration was NOT successful (key data is NOT valid)
         return;
     Console.WriteLine($"Bridge IP Address = {ip}");
     Console.WriteLine($"Application Key = {key}");
     var localHueClient = new HueApi.LocalHueApi(ip, key); // Connect application as client to Hue Bridge

    // Ideally the connection gets verified here

     // Get light information
     var lights = await localHueClient.GetLightsAsync(); // Get data for all lights from Hue Bridge
     var id = lights.Data.Last().Id; // Pick a light from all retrieved lights

     // Turn one light red
     var req = new UpdateLight()
       .TurnOn()
       .SetColor(new HueApi.ColorConverters.RGBColor("FF0000"));
     var result = await localHueClient.UpdateLightAsync(id, req);
     await Task.Delay(TimeSpan.FromSeconds(5));
 }

image

My bridge is found without issue. After pressing the 'Link' button on the bridge, a key (which is 32 alphanumeric characters) is generated. Any idea what I'm doing wrong?

Here are the error message details:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Attempted to perform an unauthorized operation.
  Source=HueApi
  StackTrace:
   at HueApi.BaseHueApi.<ProcessResponseAsync>d__142`1.MoveNext()
   at HueApi.BaseHueApi.<HueGetRequestAsync>d__138`1.MoveNext()
   at HueApiInterface.HueLightControl.<SimpleTest>d__20.MoveNext()
@michielpost
Copy link
Owner

The StreamingClientKey should only be used with the StreamingHueClient.
For normal usage, use:
var key = registrationResult.Username;

@Exergist
Copy link
Author

Exergist commented Jul 4, 2024

Thank you for your help! I have a couple more questions if you don't mind:

  1. What would be the simplest way to check that a connection has been established with the Hue Bridge (i.e. connection status, etc)?
  2. What is the difference in terms of responsiveness (if any) between HueApi and HueApi.Entertainment? I'm assuming that HueApi.Entertainment is more geared towards making frequent light changes.

Thanks again!

@michielpost
Copy link
Owner

  1. To check if you have a connection, getting some info from the bridge is the best way. Like getting the bridge info or getting the list of lights. If that returns, you know you're good.
  2. Yes, HueApi.Entertainment is for fast effects, party setups, syncing lights to music etc. For normal everyday usage you can use the normal HueApi client.

@Exergist Exergist changed the title Simple test exception help Simple test exception help & other questions Jul 7, 2024
@Exergist
Copy link
Author

Exergist commented Jul 7, 2024

Another question. After running GetLightsAsync() I output information for each of my lights/plugs.

Though I found that only some of the Light properties actually have content. Is that expected? I can see things like Software Version for each light when looking at the Hue app.

image

@michielpost
Copy link
Owner

Most values are optional and are not always returned, or only for some specific bridge resources.
You can always check the API documentation here: https://developers.meethue.com/develop/hue-api-v2/api-reference/
And use Fiddler (https://www.telerik.com/download/fiddler) to check the actual returned json from the bridge. If there is data returned from the bridge and visible in Fiddler which is not accessible through the HueApi library, please let me know and I can add new properties or you can create a PR to add them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants