Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vicfergar committed Jul 31, 2023
1 parent 0f48c5f commit 36261e1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/HassClient.Core/Models/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public class Service
/// </summary>
[JsonProperty]
public Dictionary<string, ServiceField> Fields { get; private set; }

/// <summary>
/// Gets the fields/parameters that the service supports.
/// </summary>
[JsonProperty]
public ServiceResponse Response { get; private set; }
}
}
2 changes: 1 addition & 1 deletion src/HassClient.Core/Models/ServiceField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace HassClient.Models
{
/// <summary>
/// Represents a signle field in a service call.
/// Represents a single field in a service call.
/// </summary>
public class ServiceField
{
Expand Down
16 changes: 16 additions & 0 deletions src/HassClient.Core/Models/ServiceResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace HassClient.Models
{
/// <summary>
/// Represents a response in a service call.
/// </summary>
public class ServiceResponse
{
/// <summary>
/// Gets a value indicating whether the response is optional.
/// </summary>
[JsonProperty("optional")]
public string IsOptional { get; private set; }
}
}
11 changes: 6 additions & 5 deletions src/HassClient.WS.Tests/ServiceApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@ public class ServiceApiTests : BaseHassWSApiTest
[Test]
public async Task GetServices()
{
var services = await this.hassWSApi.GetServicesAsync();
System.Collections.Generic.IEnumerable<ServiceDomain> services = await this.hassWSApi.GetServicesAsync();

Assert.NotNull(services);
Assert.IsNotEmpty(services);
CollectionAssert.AllItemsAreNotNull(services);
}

[Test]
public async Task CallService()
{
var result = await this.hassWSApi.CallServiceAsync("homeassistant", "check_config");
Context result = await this.hassWSApi.CallServiceAsync("homeassistant", "check_config");

Assert.NotNull(result);
}

[Test]
public async Task CallServiceForEntities()
{
var result = await this.hassWSApi.CallServiceForEntitiesAsync("homeassistant", "update_entity", "sun.sun");
bool result = await this.hassWSApi.CallServiceForEntitiesAsync("homeassistant", "update_entity", "sun.sun");

Assert.NotNull(result);
}

[Test]
public async Task CallServiceWithKnwonDomain()
{
var result = await this.hassWSApi.CallServiceAsync(KnownDomains.Homeassistant, KnownServices.CheckConfig);
bool result = await this.hassWSApi.CallServiceAsync(KnownDomains.Homeassistant, KnownServices.CheckConfig);

Assert.IsTrue(result);
}

[Test]
public async Task CallServiceForEntitiesWithKnwonDomain()
{
var result = await this.hassWSApi.CallServiceForEntitiesAsync(KnownDomains.Homeassistant, KnownServices.UpdateEntity, "sun.sun");
bool result = await this.hassWSApi.CallServiceForEntitiesAsync(KnownDomains.Homeassistant, KnownServices.UpdateEntity, "sun.sun");

Assert.IsTrue(result);
}
Expand Down
51 changes: 33 additions & 18 deletions src/HassClient.WS.Tests/SubscriptionApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class SubscriptionApiTests : BaseHassWSApiTest

private async Task<StateChangedEvent> ForceStateChangedAndGetEventData(MockEventSubscriber subscriber)
{
var domain = testEntitytId.GetDomain();
var update = await this.hassWSApi.CallServiceForEntitiesAsync(domain, "toggle", testEntitytId);
string domain = testEntitytId.GetDomain();
bool update = await this.hassWSApi.CallServiceForEntitiesAsync(domain, "toggle", testEntitytId);
Assert.NotNull(update, "SetUp failed");

var eventResultInfo = await subscriber.WaitFirstEventArgWithTimeoutAsync<EventResultInfo>(
(x) => HassSerializer.TryGetEnumFromSnakeCase<KnownEventTypes>(x.EventType, out var knownEventType) &&
EventResultInfo eventResultInfo = await subscriber.WaitFirstEventArgWithTimeoutAsync<EventResultInfo>(
(x) => HassSerializer.TryGetEnumFromSnakeCase<KnownEventTypes>(x.EventType, out KnownEventTypes knownEventType) &&
knownEventType == KnownEventTypes.StateChanged,
500);

Expand All @@ -31,19 +31,19 @@ private async Task<StateChangedEvent> ForceStateChangedAndGetEventData(MockEvent
[Test]
public async Task AddMultipleEventHandlerSubscriptionForAnyEvent()
{
var testEventHandler1 = new MockEventHandler<EventResultInfo>();
var testEventHandler2 = new MockEventHandler<EventResultInfo>();
var subscriber1 = new MockEventSubscriber();
var subscriber2 = new MockEventSubscriber();
MockEventHandler<EventResultInfo> testEventHandler1 = new();
MockEventHandler<EventResultInfo> testEventHandler2 = new();
MockEventSubscriber subscriber1 = new();
MockEventSubscriber subscriber2 = new();
testEventHandler1.Event += subscriber1.Handle;
testEventHandler2.Event += subscriber2.Handle;
var result1 = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler1.EventHandler);
var result2 = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler2.EventHandler);
bool result1 = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler1.EventHandler);
bool result2 = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler2.EventHandler);

Assert.IsTrue(result1);
Assert.IsTrue(result2);

var eventData = await this.ForceStateChangedAndGetEventData(subscriber1);
StateChangedEvent eventData = await this.ForceStateChangedAndGetEventData(subscriber1);

Assert.NotZero(subscriber1.HitCount);
Assert.AreEqual(subscriber1.HitCount, subscriber2.HitCount);
Expand All @@ -53,10 +53,10 @@ public async Task AddMultipleEventHandlerSubscriptionForAnyEvent()
[Test]
public async Task AddEventHandlerSubscriptionForAnyEvent()
{
var testEventHandler = new MockEventHandler<EventResultInfo>();
var subscriber = new MockEventSubscriber();
MockEventHandler<EventResultInfo> testEventHandler = new();
MockEventSubscriber subscriber = new();
testEventHandler.Event += subscriber.Handle;
var result = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler.EventHandler);
bool result = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler.EventHandler);

Assert.IsTrue(result);

Expand All @@ -68,14 +68,29 @@ public async Task AddEventHandlerSubscriptionForAnyEvent()
[Test]
public async Task AddEventHandlerSubscriptionForStateChangedEvents()
{
var testEventHandler = new MockEventHandler<EventResultInfo>();
var subscriber = new MockEventSubscriber();
MockEventHandler<EventResultInfo> testEventHandler = new();
MockEventSubscriber subscriber = new();
testEventHandler.Event += subscriber.Handle;
var result = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler.EventHandler, KnownEventTypes.StateChanged);
bool result = await this.hassWSApi.AddEventHandlerSubscriptionAsync(testEventHandler.EventHandler, KnownEventTypes.StateChanged);

Assert.IsTrue(result);

var eventData = await this.ForceStateChangedAndGetEventData(subscriber);
StateChangedEvent eventData = await this.ForceStateChangedAndGetEventData(subscriber);

Assert.NotZero(subscriber.HitCount);
Assert.IsTrue(eventData.EntityId == testEntitytId);
Assert.NotNull(eventData.NewState.State);
}

[Test]
public async Task StateChagedEventListenerShouldSubscribeEntityStatusChanged()
{
MockEventHandler<StateChangedEvent> testEventHandler = new();
MockEventSubscriber subscriber = new();
testEventHandler.Event += subscriber.Handle;
this.hassWSApi.StateChagedEventListener.SubscribeEntityStatusChanged(testEntitytId, testEventHandler.EventHandler);

StateChangedEvent eventData = await this.ForceStateChangedAndGetEventData(subscriber);

Assert.NotZero(subscriber.HitCount);
Assert.IsTrue(eventData.EntityId == testEntitytId);
Expand Down

0 comments on commit 36261e1

Please sign in to comment.