Skip to content

Commit

Permalink
Tests, and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 17, 2020
1 parent ef47851 commit b7bf500
Show file tree
Hide file tree
Showing 23 changed files with 1,072 additions and 874 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ENV \
DOTNET_CLI_TELEMETRY_OPTOUT=true \
HASS_RUN_PROJECT_FOLDER=/usr/src/Service \
HASS_HOST=localhost \
HASSCLIENT_MSGLOGLEVEL=Default \
HASS_PORT=8123 \
HASS_TOKEN=NOT_SET \
HASS_DAEMONAPPFOLDER=/data
Expand Down
44 changes: 44 additions & 0 deletions exampleapps/apps/Extensions/AppExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using JoySoftware.HomeAssistant.NetDaemon.Common.Reactive;
using Helto4real.Powertools;
public static class DaemonAppExtensions
{

/// <summary>
/// Takes a snapshot of given entity id of camera and sends to private discord server
/// </summary>
/// <param name="app">NetDaemonApp to extend</param>
/// <param name="camera">Unique id of the camera</param>
public static void CameraTakeSnapshotAndNotify(this NetDaemonRxApp app, string camera)
{
var imagePath = app.CameraSnapshot(camera);

app.NotifyImage(camera, imagePath);
}

public static void Notify(this NetDaemonRxApp app, string message)
{
app.CallService("notify", "hass_discord", new
{
message = message,
target = "511278310584746008"
});
}

public static void NotifyImage(this NetDaemonRxApp app, string message, string imagePath)
{
var dict = new Dictionary<string, IEnumerable<string>>
{
["images"] = new List<string> { imagePath }
};

app.CallService("notify", "hass_discord", new
{
data = dict,
message = message,
target = "511278310584746008"
});
}
}
58 changes: 58 additions & 0 deletions exampleapps/apps/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

using System;
using System.Threading.Tasks;
using JoySoftware.HomeAssistant.NetDaemon.Common;

// public static class ServiceCallExtensions
// {
// public static Task ToggleAsync(this NetDaemonApp app, string entityId, params (string name, object val)[] attributeNameValuePair)
// {
// // Get the domain if supported, else domain is homeassistant
// string domain = GetDomainFromEntity(entityId);
// // Use it if it is supported else use default "homeassistant" domain

// // Use expando object as all other methods
// dynamic attributes = attributeNameValuePair.ToDynamic();
// // and add the entity id dynamically
// attributes.entity_id = entityId;

// return app.CallService(domain, "toggle", attributes, false);
// }

// public static Task TurnOffAsync(this NetDaemonApp app, string entityId, params (string name, object val)[] attributeNameValuePair)
// {
// // Get the domain if supported, else domain is homeassistant
// string domain = GetDomainFromEntity(entityId);
// // Use it if it is supported else use default "homeassistant" domain

// // Use expando object as all other methods
// dynamic attributes = attributeNameValuePair.ToDynamic();
// // and add the entity id dynamically
// attributes.entity_id = entityId;

// return app.CallService(domain, "turn_off", attributes, false);
// }

// public static Task TurnOnAsync(this NetDaemonApp app, string entityId, params (string name, object val)[] attributeNameValuePair)
// {
// // Use default domain "homeassistant" if supported is missing
// string domain = GetDomainFromEntity(entityId);
// // Use it if it is supported else use default "homeassistant" domain

// // Convert the value pairs to dynamic type
// dynamic attributes = attributeNameValuePair.ToDynamic();
// // and add the entity id dynamically
// attributes.entity_id = entityId;

// return app.CallService(domain, "turn_on", attributes, false);
// }

// private static string GetDomainFromEntity(string entity)
// {
// var entityParts = entity.Split('.');
// if (entityParts.Length != 2)
// throw new ApplicationException($"entity_id is mal formatted {entity}");

// return entityParts[0];
// }
// }
65 changes: 65 additions & 0 deletions exampleapps/apps/Powertools/Powertools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using JoySoftware.HomeAssistant.NetDaemon.Common.Reactive;

// Use unique namespaces for your apps if you going to share with others to avoid
// conflicting names
namespace Helto4real.Powertools
{
public static class Powertools
{
/// <summary>
/// Takes a snapshot of given entity id of camera and sends to private discord server
/// </summary>
/// <param name="app">NetDaemonApp to extend</param>
/// <param name="camera">Unique id of the camera</param>
/// <returns>The path to the snapshot</returns>
public static string CameraSnapshot(this NetDaemonRxApp app, string camera)
{
var resultingFilename = $"/config/www/motion/{camera}_latest.jpg";
app.CallService("camera", "snapshot", new
{
entity_id = camera,
filename = resultingFilename
});

return resultingFilename;
}

/// <summary>
/// Takes a snapshot of given entity id of camera and sends to private discord server
/// </summary>
/// <param name="app">NetDaemonApp to extend</param>
/// <param name="camera">Unique id of the camera</param>
public static void CameraSnapshot(this NetDaemonRxApp app, string camera, string snapshotPath)
{
app.CallService("camera", "snapshot", new
{
entity_id = camera,
filename = snapshotPath
});
}

/// <summary>
/// Prints the contents from a IDictionary to a string
/// </summary>
/// <param name="app">NetDaemonApp to extend</param>
/// <param name="dict">The dict to print from, typically from dynamic result</param>
/// <returns></returns>
public static string PrettyPrintDictData(this NetDaemonRxApp app, IDictionary<string, object>? dict)
{

if (dict == null)
return string.Empty;

var builder = new StringBuilder(100);
foreach (var key in dict.Keys)
{
builder.AppendLine($"{key}:{dict[key]}");
}
return builder.ToString();
}
}
}

31 changes: 28 additions & 3 deletions exampleapps/apps/test2.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Threading.Tasks;
using JoySoftware.HomeAssistant.NetDaemon.Common;
using JoySoftware.HomeAssistant.NetDaemon.Common.Reactive;
using System.Linq;
using System;
using System.Reactive.Linq;
using System.Collections.Generic;
// using Netdaemon.Generated.Extensions;
public class BatteryManager : NetDaemonApp
public class BatteryManager : NetDaemonRxApp
{
// private ISchedulerResult _schedulerResult;
private int numberOfRuns = 0;
Expand All @@ -14,7 +15,31 @@ public override async Task InitializeAsync()
{
Log("Hello");
Log("Hello {name}", "Tomas");
Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();
// RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
// RunDaily("13:00:00", () => Log("Hello world!"));
// RunIn(TimeSpan.FromSeconds(5), () => Entity("light.tomas_rum").TurnOn());
// Entity("light.tomas_rum")
// .StateChanges
// .Subscribe(s => Log("Chanche {entity}", s.New.State));

// StateChanges
// .Where(e => e.New.EntityId.StartsWith("light."))
// .Subscribe(e =>
// {
// Log("Hello!");
// });
// EventChanges
// // .Where(e => e.Domain == "scene" && e.Event == "turn_on")
// .Subscribe(e =>
// {
// Log("Hello!");
// },
// err => LogError(err, "Ohh nooo!"),
// () => Log("Ending event"));



// Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();

// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
Expand Down
Loading

0 comments on commit b7bf500

Please sign in to comment.