Q42.HueApi
Q42.HueApi copied to clipboard
Simple test exception help & other questions
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));
}
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()
The StreamingClientKey should only be used with the StreamingHueClient.
For normal usage, use:
var key = registrationResult.Username;
Thank you for your help! I have a couple more questions if you don't mind:
- What would be the simplest way to check that a connection has been established with the Hue Bridge (i.e. connection status, etc)?
- 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!
- 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.
- 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.
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.
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.