diff --git a/src/Q42.HueApi.Tests/LightsTests.cs b/src/Q42.HueApi.Tests/LightsTests.cs index 0f5b9b6b..199c9bb4 100644 --- a/src/Q42.HueApi.Tests/LightsTests.cs +++ b/src/Q42.HueApi.Tests/LightsTests.cs @@ -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); + + } diff --git a/src/Q42.HueApi/HueClient-Lights.cs b/src/Q42.HueApi/HueClient-Lights.cs index a1bcc49a..6ab302c5 100644 --- a/src/Q42.HueApi/HueClient-Lights.cs +++ b/src/Q42.HueApi/HueClient-Lights.cs @@ -87,6 +87,31 @@ public async Task SetLightNameAsync(string id, string name) return DeserializeDefaultHueResult(jsonResult); } + /// + /// Sets the light name + /// + /// + /// + /// + public async Task 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); + } + /// /// Asynchronously gets all lights registered with the bridge. /// diff --git a/src/Q42.HueApi/Interfaces/IHueClient.cs b/src/Q42.HueApi/Interfaces/IHueClient.cs index 8aabfa9f..d5dd4cdb 100644 --- a/src/Q42.HueApi/Interfaces/IHueClient.cs +++ b/src/Q42.HueApi/Interfaces/IHueClient.cs @@ -127,6 +127,14 @@ public interface IHueClient /// Task SetLightNameAsync(string id, string name); + /// + /// Update Light Config + /// + /// + /// + /// + Task LightConfigUpdate(string id, LightConfigUpdate config); + /// /// Send a raw string / json command /// diff --git a/src/Q42.HueApi/Models/Light.cs b/src/Q42.HueApi/Models/Light.cs index ad7bf9ff..97d86d21 100644 --- a/src/Q42.HueApi/Models/Light.cs +++ b/src/Q42.HueApi/Models/Light.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; using System.Runtime.Serialization; @@ -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; } + + /// + /// Only bri, xy, ct properties are used + /// + [DataMember(Name = "customsettings")] + public LightCommand CustomSettings { get; set; } + } + + /// + /// Defined on https://developers.meethue.com/develop/hue-api/supported-devices/ + /// + public enum StartupMode + { + [EnumMember(Value = "safety")] + Safety, + [EnumMember(Value = "powerfail")] + Powerfail, + [EnumMember(Value = "lastonstate")] + LastOnState, + [EnumMember(Value = "custom")] + Custom, + [EnumMember(Value = "unknown")] + Unknown } } diff --git a/src/Q42.HueApi/Models/LightUpdate.cs b/src/Q42.HueApi/Models/LightUpdate.cs new file mode 100644 index 00000000..019f1872 --- /dev/null +++ b/src/Q42.HueApi/Models/LightUpdate.cs @@ -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; } + + } +}