Skip to content

Commit

Permalink
Added scenes with trigger/undo support, renamed Device NS to Models, …
Browse files Browse the repository at this point in the history
…renamed Refresh to LoadAll
  • Loading branch information
hagronnestad committed Sep 14, 2023
1 parent d624b5f commit 42dd54a
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 13 deletions.
22 changes: 22 additions & 0 deletions Dirigera.Lib/Dirigera.Lib/Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public async Task<bool> CheckAuthToken()
return res;
}

public async Task<List<SceneDto>?> GetScenes()
{
string url = $"{_apiBaseUrl}/scenes";
var res = await _httpClient.GetFromJsonAsync<List<SceneDto>>(url);
return res;
}

public async Task<T?> GetDevice<T>(string id)
{
string url = $"{_apiBaseUrl}/devices/{id}";
Expand Down Expand Up @@ -118,6 +125,21 @@ public async Task<HttpResponseMessage> PatchAttributesRoom(string roomId, Dictio
return res;
}

public async Task<HttpResponseMessage> TriggerScene(string sceneId)
{
string url = $"{_apiBaseUrl}/scenes/{sceneId}/trigger";
var res = await _httpClient.PostAsync(url, null);
return res;
}

public async Task<HttpResponseMessage> UndoScene(string sceneId)
{
string url = $"{_apiBaseUrl}/scenes/{sceneId}/undo";
var res = await _httpClient.PostAsync(url, null);
return res;
}


public async Task<string?> SendChallenge(string codeVerifier)
{
string authUrl = $"{_apiBaseUrl}/oauth/authorize";
Expand Down
13 changes: 13 additions & 0 deletions Dirigera.Lib/Dirigera.Lib/Api/Dto/SceneDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Dirigera.Lib.Api.Dto
{
internal class SceneDto
{
public string? Id { get; set; }
public SceneInfoDto? Info { get; set; }
public string? Type { get; set; }
public string? CreatedAt { get; set; }
public string? LastCompleted { get; set; }
public string? LastTriggered { get; set; }
public int UndoAllowedDuration { get; set; }
}
}
8 changes: 8 additions & 0 deletions Dirigera.Lib/Dirigera.Lib/Api/Dto/SceneInfoDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Dirigera.Lib.Api.Dto
{
internal class SceneInfoDto
{
public string? Name { get; set; }
public string? Icon { get; set; }
}
}
41 changes: 38 additions & 3 deletions Dirigera.Lib/Dirigera.Lib/DirigeraManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Dirigera.Lib.Api;
using Dirigera.Lib.Api.Dto.Base;
using Dirigera.Lib.Constants;
using Dirigera.Lib.Devices;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models;
using Dirigera.Lib.Models.Base;
using Dirigera.Models;
using System.Drawing;
using Zeroconf;
Expand All @@ -20,6 +20,7 @@ public class DirigeraManager

public Hub? Hub { get; set; }
public List<Room> Rooms { get; set; } = new();
public List<Scene> Scenes { get; set; } = new();
public List<Device> Devices { get; set; } = new();
public List<Light> Lights { get; set; } = new();
public List<Blind> Blinds { get; set; } = new();
Expand All @@ -40,11 +41,12 @@ public DirigeraManager(string ipAddress, string? authToken = null)
_apiClient = new ApiClient(ipAddress, authToken);
}

public async Task Refresh()
public async Task LoadAll()
{
await GetHubDetails();
await GetDevices();
await GetRooms();
await GetScenes();
}


Expand Down Expand Up @@ -92,6 +94,27 @@ private async Task GetRooms()
.ToList();
}

private async Task GetScenes()
{
var scenes = await _apiClient.GetScenes();
if (scenes is null) return;

Scenes = scenes
.Select(x => new Scene(this)
{
Id = x.Id,
Name = x.Info?.Name,
Icon = x.Info?.Icon,
Type = x.Type,
CreatedAt = x.CreatedAt,
LastCompleted = x.LastCompleted,
LastTriggered = x.LastTriggered,
UndoAllowedDuration = x.UndoAllowedDuration
})
.Where(x => x != null)
.ToList();
}

public async Task SetLightState(Light light, bool state)
{
if (light is null || light.Id is null) return;
Expand Down Expand Up @@ -203,6 +226,18 @@ public async Task SetOutlet(Outlet outlet, bool state)
});
}

public async Task TriggerScene(Scene scene)
{
if (scene is null || scene.Id is null) return;
await _apiClient.TriggerScene(scene.Id);
}

public async Task UndoScene(Scene scene)
{
if (scene is null || scene.Id is null) return;
await _apiClient.UndoScene(scene.Id);
}


/// <summary>
/// Create a <see cref="DirigeraManager"/> object by automatically discovering your IKEA DIRIGERA hub on your network.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Dirigera.Lib.Api.Dto.Base;
using System.Text.Json;

namespace Dirigera.Lib.Devices.Base
namespace Dirigera.Lib.Models.Base
{
public class Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Dirigera.Lib;
using Dirigera.Lib.Api.Dto.Base;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models.Base;

namespace Dirigera.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Dirigera.Lib;
using Dirigera.Lib.Api.Dto.Base;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models.Base;

namespace Dirigera.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Dirigera.Lib.Api.Dto;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models.Base;

namespace Dirigera.Lib.Devices
namespace Dirigera.Lib.Models
{
public class Hub : Device
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Dirigera.Lib;
using Dirigera.Lib.Api.Dto.Base;
using Dirigera.Lib.Constants;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models.Base;
using System.Drawing;

namespace Dirigera.Models
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Dirigera.Lib;
using Dirigera.Lib.Api.Dto.Base;
using Dirigera.Lib.Devices.Base;
using Dirigera.Lib.Models.Base;

namespace Dirigera.Models
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Dirigera.Lib.Api.Dto;

namespace Dirigera.Lib.Devices
namespace Dirigera.Lib.Models
{
public class Room
{
Expand Down
31 changes: 31 additions & 0 deletions Dirigera.Lib/Dirigera.Lib/Models/Scene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Dirigera.Lib.Models
{
public class Scene
{
private readonly DirigeraManager _dirigeraManager;

public string? Id { get; internal set; }
public string? Name { get; internal set; }
public string? Icon { get; internal set; }
public string? Type { get; set; }
public string? CreatedAt { get; set; }
public string? LastCompleted { get; set; }
public string? LastTriggered { get; set; }
public int UndoAllowedDuration { get; set; }

internal Scene(DirigeraManager dirigeraManager)
{
_dirigeraManager = dirigeraManager;
}

public async Task Trigger()
{
await _dirigeraManager.TriggerScene(this);
}

public async Task Undo()
{
await _dirigeraManager.UndoScene(this);
}
}
}

0 comments on commit 42dd54a

Please sign in to comment.