Skip to content

Commit

Permalink
v1.6.0, added support for CameraMotion, ContactSensor and TamperSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Dec 14, 2023
1 parent 43dd2a3 commit 1994d4d
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 15 deletions.
65 changes: 65 additions & 0 deletions src/HueApi.Tests/CameraMotionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HueApi.Models.Requests;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HueApi.Tests
{
[TestClass]
public class CameraMotionTests
{
private readonly LocalHueApi localHueClient;

public CameraMotionTests()
{
var builder = new ConfigurationBuilder().AddUserSecrets<CameraMotionTests>();
var config = builder.Build();

localHueClient = new LocalHueApi(config["ip"], key: config["key"]);
}

[TestMethod]
public async Task Get()
{
var result = await localHueClient.GetCameraMotionsAsync();

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);
}

[TestMethod]
public async Task GetById()
{
var all = await localHueClient.GetCameraMotionsAsync();
var id = all.Data.First().Id;

var result = await localHueClient.GetCameraMotionAsync(id);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);

}


[TestMethod]
public async Task PutById()
{
var all = await localHueClient.GetCameraMotionsAsync();
var id = all.Data.Last().Id;

var req = new UpdateSensitivitySensorRequest();
var result = await localHueClient.UpdateCameraMotionAsync(id, req);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);
Assert.AreEqual(id, result.Data.First().Rid);

}
}
}
65 changes: 65 additions & 0 deletions src/HueApi.Tests/ContactSensorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HueApi.Models.Requests;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HueApi.Tests
{
[TestClass]
public class ContactSensorTests
{
private readonly LocalHueApi localHueClient;

public ContactSensorTests()
{
var builder = new ConfigurationBuilder().AddUserSecrets<ContactSensorTests>();
var config = builder.Build();

localHueClient = new LocalHueApi(config["ip"], key: config["key"]);
}

[TestMethod]
public async Task Get()
{
var result = await localHueClient.GetContactSensorsAsync();

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);
}

[TestMethod]
public async Task GetById()
{
var all = await localHueClient.GetContactSensorsAsync();
var id = all.Data.First().Id;

var result = await localHueClient.GetContactSensorAsync(id);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);

}


[TestMethod]
public async Task PutById()
{
var all = await localHueClient.GetContactSensorsAsync();
var id = all.Data.Last().Id;

var req = new UpdateSensitivitySensorRequest();
var result = await localHueClient.UpdateContactSensorsAsync(id, req);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);
Assert.AreEqual(id, result.Data.First().Rid);

}
}
}
2 changes: 1 addition & 1 deletion src/HueApi.Tests/MotionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task PutById()
var all = await localHueClient.GetMotionsAsync();
var id = all.Data.Last().Id;

UpdateSensorRequest req = new UpdateSensorRequest();
var req = new UpdateSensitivitySensorRequest();
var result = await localHueClient.UpdateMotionAsync(id, req);

Assert.IsNotNull(result);
Expand Down
65 changes: 65 additions & 0 deletions src/HueApi.Tests/TamperSensorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using HueApi.Models.Requests;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HueApi.Tests
{
[TestClass]
public class TamperSensorTests
{
private readonly LocalHueApi localHueClient;

public TamperSensorTests()
{
var builder = new ConfigurationBuilder().AddUserSecrets<TamperSensorTests>();
var config = builder.Build();

localHueClient = new LocalHueApi(config["ip"], key: config["key"]);
}

[TestMethod]
public async Task Get()
{
var result = await localHueClient.GetTamperSensorsAsync();

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);
}

[TestMethod]
public async Task GetById()
{
var all = await localHueClient.GetTamperSensorsAsync();
var id = all.Data.First().Id;

var result = await localHueClient.GetTamperSensorAsync(id);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);

}


[TestMethod]
public async Task PutById()
{
var all = await localHueClient.GetTamperSensorsAsync();
var id = all.Data.Last().Id;

var req = new BaseResourceRequest();
var result = await localHueClient.UpdateTamperSensorAsync(id, req);

Assert.IsNotNull(result);
Assert.IsFalse(result.HasErrors);

Assert.IsTrue(result.Data.Count == 1);
Assert.AreEqual(id, result.Data.First().Rid);

}
}
}
25 changes: 23 additions & 2 deletions src/HueApi/BaseHueApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public abstract class BaseHueApi
protected const string ResourceUrl = "clip/v2/resource";
protected const string LightUrl = $"{ResourceUrl}/light";
protected const string SceneUrl = $"{ResourceUrl}/scene";
protected const string SmartSceneUrl = $"{ResourceUrl}/smart_scene";
protected const string RoomUrl = $"{ResourceUrl}/room";
protected const string ZoneUrl = $"{ResourceUrl}/zone";
protected const string BridgeHomeUrl = $"{ResourceUrl}/bridge_home";
Expand All @@ -30,6 +29,7 @@ public abstract class BaseHueApi
protected const string ZgpConnectivityUrl = $"{ResourceUrl}/zgp_connectivity";
protected const string ZigbeeDeviceDiscoveryUrl = $"{ResourceUrl}/zigbee_device_discovery";
protected const string MotionUrl = $"{ResourceUrl}/motion";
protected const string CameraMotionUrl = $"{ResourceUrl}/camera_motion";
protected const string TemperatureUrl = $"{ResourceUrl}/temperature";
protected const string LightLevelUrl = $"{ResourceUrl}/light_level";
protected const string ButtonUrl = $"{ResourceUrl}/button";
Expand All @@ -43,6 +43,11 @@ public abstract class BaseHueApi
protected const string HomekitUrl = $"{ResourceUrl}/homekit";
protected const string MatterUrl = $"{ResourceUrl}/matter";
protected const string MatterFabricUrl = $"{ResourceUrl}/matter_fabric";
//recource
protected const string SmartSceneUrl = $"{ResourceUrl}/smart_scene";
protected const string ContactUrl = $"{ResourceUrl}/contact";
protected const string TamperUrl = $"{ResourceUrl}/tamper";


protected string ResourceIdUrl(string resourceUrl, Guid id) => $"{resourceUrl}/{id}";

Expand Down Expand Up @@ -136,7 +141,13 @@ public abstract class BaseHueApi
#region Motion
public Task<HueResponse<MotionResource>> GetMotionsAsync() => HueGetRequestAsync<MotionResource>(MotionUrl);
public Task<HueResponse<MotionResource>> GetMotionAsync(Guid id) => HueGetRequestAsync<MotionResource>(ResourceIdUrl(MotionUrl, id));
public Task<HuePutResponse> UpdateMotionAsync(Guid id, UpdateSensorRequest data) => HuePutRequestAsync(ResourceIdUrl(MotionUrl, id), data);
public Task<HuePutResponse> UpdateMotionAsync(Guid id, UpdateSensitivitySensorRequest data) => HuePutRequestAsync(ResourceIdUrl(MotionUrl, id), data);
#endregion

#region CameraMotion
public Task<HueResponse<CameraMotionResource>> GetCameraMotionsAsync() => HueGetRequestAsync<CameraMotionResource>(CameraMotionUrl);
public Task<HueResponse<CameraMotionResource>> GetCameraMotionAsync(Guid id) => HueGetRequestAsync<CameraMotionResource>(ResourceIdUrl(CameraMotionUrl, id));
public Task<HuePutResponse> UpdateCameraMotionAsync(Guid id, UpdateSensitivitySensorRequest data) => HuePutRequestAsync(ResourceIdUrl(CameraMotionUrl, id), data);
#endregion

#region Temperature
Expand Down Expand Up @@ -227,7 +238,17 @@ public abstract class BaseHueApi
public Task<HueDeleteResponse> DeleteMatterFabricAsync(Guid id) => HueDeleteRequestAsync(ResourceIdUrl(MatterFabricUrl, id));
#endregion

#region Contact
public Task<HueResponse<ContactSensor>> GetContactSensorsAsync() => HueGetRequestAsync<ContactSensor>(ContactUrl);
public Task<HueResponse<ContactSensor>> GetContactSensorAsync(Guid id) => HueGetRequestAsync<ContactSensor>(ResourceIdUrl(ContactUrl, id));
public Task<HuePutResponse> UpdateContactSensorsAsync(Guid id, UpdateSensorRequest data) => HuePutRequestAsync(ResourceIdUrl(ContactUrl, id), data);
#endregion

#region Tamper
public Task<HueResponse<TamperSensor>> GetTamperSensorsAsync() => HueGetRequestAsync<TamperSensor>(TamperUrl);
public Task<HueResponse<TamperSensor>> GetTamperSensorAsync(Guid id) => HueGetRequestAsync<TamperSensor>(ResourceIdUrl(TamperUrl, id));
public Task<HuePutResponse> UpdateTamperSensorAsync(Guid id, BaseResourceRequest data) => HuePutRequestAsync(ResourceIdUrl(TamperUrl, id), data);
#endregion

protected async Task<HueResponse<T>> HueGetRequestAsync<T>(string url)
{
Expand Down
2 changes: 1 addition & 1 deletion src/HueApi/HueApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<Version>1.5.3</Version>
<Version>1.6.0</Version>
<Authors>Michiel Post</Authors>
<Description>For Clip v2 API. Open source library for interaction with the Philips Hue Bridge. Allows you to control your lights from C#.</Description>
<PackageProjectUrl>https://github.com/michielpost/Q42.HueApi</PackageProjectUrl>
Expand Down
21 changes: 18 additions & 3 deletions src/HueApi/Models/ButtonResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ public class ButtonResource : HueResource

public class Button
{
[JsonPropertyName("last_event")]
public ButtonLastEvent? LastEvent { get; set; }
[JsonPropertyName("button_report")]
public ButtonReport? ButtonReport { get; set; }

[JsonPropertyName("repeat_interval")]
public int? RepeatInterval { get; set; }

[JsonPropertyName("event_values")]
public List<ButtonEvent>? EventValues { get; set; }
}

public class ButtonReport
{
[JsonPropertyName("updated")]
public DateTimeOffset? Updated { get; set; }

[JsonPropertyName("event")]
public ButtonEvent? Event { get; set; }
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ButtonLastEvent
public enum ButtonEvent
{
initial_press, repeat, short_release, long_release, double_short_release, long_press
}
Expand Down
17 changes: 17 additions & 0 deletions src/HueApi/Models/Requests/UpdateSensitivitySensorRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using HueApi.Models.Sensors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace HueApi.Models.Requests
{
public class UpdateSensitivitySensorRequest : UpdateSensorRequest
{
[JsonPropertyName("sensitivity")]
public Sensitivity? Sensitivity { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/HueApi/Models/Requests/UpdateSensorRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using HueApi.Models.Sensors;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,6 +13,5 @@ public class UpdateSensorRequest : BaseResourceRequest
[JsonPropertyName("enabled")]
public bool Enabled { get; set; }


}
}
14 changes: 14 additions & 0 deletions src/HueApi/Models/Sensors/CameraMotionResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace HueApi.Models.Sensors
{
public class CameraMotionResource : MotionResource
{

}
}
29 changes: 29 additions & 0 deletions src/HueApi/Models/Sensors/ContactSensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json.Serialization;

namespace HueApi.Models.Sensors
{
public class ContactSensor : HueResource
{
[JsonPropertyName("enabled")]
public bool Enabled { get; set; } = default!;

[JsonPropertyName("contact_report")]
public ContactReport? ContactReport { get; set; }
}

public class ContactReport
{
[JsonPropertyName("changed")]
public DateTimeOffset Changed { get; set; }

[JsonPropertyName("state")]
public ContactState State { get; set; }

}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ContactState
{
contact, no_contact
}
}
Loading

0 comments on commit 1994d4d

Please sign in to comment.