Skip to content

Commit

Permalink
Support for Light Startup mode #158
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Dec 13, 2018
1 parent 849cc44 commit 76bd718
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Q42.HueApi.Tests/LightsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,27 @@ public async Task GetLightsAsyncTest()
public async Task GetLightAsyncTest()
{
//Get single light
var result = await _client.GetLightAsync("19");
var result = await _client.GetLightAsync("2");

Assert.IsNotNull(result);


}

[TestMethod]
public async Task UpdateLightConfigAsync()
{
//var updateResult = await _client.LightConfigUpdate("2", new Models.LightConfigUpdate() { Startup = new LightStartup() { Mode = StartupMode.LastOnState } });
var updateResult = await _client.LightConfigUpdate("2", new Models.LightConfigUpdate() { Startup = new LightStartup() { CustomSettings = new LightCommand() { Brightness = 150 } } });
Assert.IsFalse(updateResult.Errors.Any());


//Get single light
var result = await _client.GetLightAsync("2");
Assert.IsNotNull(result);
Assert.AreEqual(result.Config.Startup.Mode, StartupMode.LastOnState);


}


Expand Down
25 changes: 25 additions & 0 deletions src/Q42.HueApi/HueClient-Lights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ public async Task<HueResults> SetLightNameAsync(string id, string name)
return DeserializeDefaultHueResult(jsonResult);
}

/// <summary>
/// Sets the light name
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
/// <returns></returns>
public async Task<HueResults> LightConfigUpdate(string id, LightConfigUpdate config)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
if (config == null)
throw new ArgumentNullException(nameof(config));

CheckInitialized();

string jsonCommand = JsonConvert.SerializeObject(config, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

HttpClient client = await GetHttpClient().ConfigureAwait(false);
var result = await client.PutAsync(new Uri(String.Format("{0}lights/{1}/config", ApiBase, id)), new JsonContent(jsonCommand)).ConfigureAwait(false);

var jsonResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

return DeserializeDefaultHueResult(jsonResult);
}

/// <summary>
/// Asynchronously gets all lights registered with the bridge.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Q42.HueApi/Interfaces/IHueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public interface IHueClient
/// <returns></returns>
Task<HueResults> SetLightNameAsync(string id, string name);

/// <summary>
/// Update Light Config
/// </summary>
/// <param name="id"></param>
/// <param name="config"></param>
/// <returns></returns>
Task<HueResults> LightConfigUpdate(string id, LightConfigUpdate config);

/// <summary>
/// Send a raw string / json command
/// </summary>
Expand Down
39 changes: 39 additions & 0 deletions src/Q42.HueApi/Models/Light.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -93,5 +95,42 @@ public class LightConfig

[DataMember(Name = "direction")]
public string Direction { get; set; }

[DataMember(Name = "startup")]
public LightStartup Startup { get; set; }
}

[DataContract]
public class LightStartup
{
[DataMember(Name = "mode")]
[JsonConverter(typeof(StringEnumConverter))]
public StartupMode? Mode { get; set; }

[DataMember(Name = "configured")]
public bool? Configured { get; set; }

/// <summary>
/// Only bri, xy, ct properties are used
/// </summary>
[DataMember(Name = "customsettings")]
public LightCommand CustomSettings { get; set; }
}

/// <summary>
/// Defined on https://developers.meethue.com/develop/hue-api/supported-devices/
/// </summary>
public enum StartupMode
{
[EnumMember(Value = "safety")]
Safety,
[EnumMember(Value = "powerfail")]
Powerfail,
[EnumMember(Value = "lastonstate")]
LastOnState,
[EnumMember(Value = "custom")]
Custom,
[EnumMember(Value = "unknown")]
Unknown
}
}
15 changes: 15 additions & 0 deletions src/Q42.HueApi/Models/LightUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Q42.HueApi.Models
{
[DataContract]
public class LightConfigUpdate
{
[DataMember(Name = "startup")]
public LightStartup Startup { get; set; }

}
}

0 comments on commit 76bd718

Please sign in to comment.