Skip to content

Commit 18b21d2

Browse files
authored
GUI: Description on app from code comment (#180)
1 parent fd258a1 commit 18b21d2

File tree

8 files changed

+220
-121
lines changed

8 files changed

+220
-121
lines changed

exampleapps/apps/test1.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
using System.Reactive.Linq;
99
using System.Runtime.Serialization;
1010
// using Netdaemon.Generated.Extensions;
11+
12+
/// <summary>
13+
/// Does some awesome stuff
14+
/// </summary>
1115
public class GlobalApp : NetDaemonApp
1216
{
1317
// private ISchedulerResult _schedulerResult;

exampleapps/apps/test2.cs

Lines changed: 119 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -6,125 +6,131 @@
66
using Netdaemon.Generated.Reactive;
77
using NetDaemon.Common;
88

9-
public class BatteryManager : GeneratedAppBase //NetDaemonRxApp
10-
// public class BatteryManager : NetDaemonRxApp
9+
namespace NStest
1110
{
12-
// private ISchedulerResult _schedulerResult;
13-
private int numberOfRuns = 0;
1411

15-
public string? HelloWorldSecret { get; set; }
16-
public override async Task InitializeAsync()
12+
/// <summary> cool multiple lines </summary>
13+
public class BatteryManager : GeneratedAppBase //NetDaemonRxApp
14+
// public class BatteryManager : NetDaemonRxApp
1715
{
18-
// Remote.Tvrummet.TurnOn(new {activity="TV"});
19-
// Log(Remote.Tvrummet.State);
20-
// Log(Remote.Tvrummet.Area);
21-
22-
// SetState("sensor.testing", "on", new { attributeobject = new { aobject = "hello" } });
23-
// RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
24-
// RunDaily("13:00:00", () => Log("Hello world!"));
25-
// RunIn(TimeSpan.FromSeconds(5), () => Entity("light.tomas_rum").TurnOn());
26-
// Entity("light.tomas_rum")
27-
// .StateChanges
28-
// .Subscribe(s => Log("Chanche {entity}", s.New.State));
29-
30-
// StateChanges
31-
// .Where(e => e.New.EntityId.StartsWith("light."))
32-
// .Subscribe(e =>
33-
// {
34-
// Log("Hello!");
35-
// });
36-
// EventChanges
37-
// // .Where(e => e.Domain == "scene" && e.Event == "turn_on")
38-
// .Subscribe(e =>
39-
// {
40-
// Log("Hello!");
41-
// },
42-
// err => LogError(err, "Ohh nooo!"),
43-
// () => Log("Ending event"));
44-
45-
46-
47-
// Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();
48-
49-
// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
50-
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
51-
// var app = (GlobalApp)GetApp("global_app");
52-
53-
// Log($"The global app shows {app.SharedThing}");
54-
// int? test = null;
55-
// bool testa = test.HasValue;
56-
57-
// await this.LightEx().JulbelysningVardagsrumH.TurnOn().ExecuteAsync();
58-
// await this.MediaPlayerEx().PlexChromecast.Play().ExecuteAsync();
59-
// Scheduler.RunIn(TimeSpan.FromSeconds(10), async () => await DoTheMagic("s").ConfigureAwait(false));
60-
16+
// private ISchedulerResult _schedulerResult;
17+
private int numberOfRuns = 0;
18+
19+
public string? HelloWorldSecret { get; set; }
20+
public override async Task InitializeAsync()
21+
{
22+
// Remote.Tvrummet.TurnOn(new {activity="TV"});
23+
// Log(Remote.Tvrummet.State);
24+
// Log(Remote.Tvrummet.Area);
25+
26+
// SetState("sensor.testing", "on", new { attributeobject = new { aobject = "hello" } });
27+
// RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world!"));
28+
// RunDaily("13:00:00", () => Log("Hello world!"));
29+
// RunIn(TimeSpan.FromSeconds(5), () => Entity("light.tomas_rum").TurnOn());
30+
// Entity("light.tomas_rum")
31+
// .StateChanges
32+
// .Subscribe(s => Log("Chanche {entity}", s.New.State));
33+
34+
// StateChanges
35+
// .Where(e => e.New.EntityId.StartsWith("light."))
36+
// .Subscribe(e =>
37+
// {
38+
// Log("Hello!");
39+
// });
40+
// EventChanges
41+
// // .Where(e => e.Domain == "scene" && e.Event == "turn_on")
42+
// .Subscribe(e =>
43+
// {
44+
// Log("Hello!");
45+
// },
46+
// err => LogError(err, "Ohh nooo!"),
47+
// () => Log("Ending event"));
48+
49+
50+
51+
// Event("TEST_EVENT").Call(async (ev, data) => { Log("EVENT2!"); }).Execute();
52+
53+
// Scheduler.RunEvery(5000, () => { var x = 0; var z = 4 / x; return Task.CompletedTask; });
54+
// Entity("sun.sun").WhenStateChange(allChanges: true).Call((entityid, to, from) => throw new Exception("Test")).Execute();
55+
// var app = (GlobalApp)GetApp("global_app");
56+
57+
// Log($"The global app shows {app.SharedThing}");
58+
// int? test = null;
59+
// bool testa = test.HasValue;
60+
61+
// await this.LightEx().JulbelysningVardagsrumH.TurnOn().ExecuteAsync();
62+
// await this.MediaPlayerEx().PlexChromecast.Play().ExecuteAsync();
63+
// Scheduler.RunIn(TimeSpan.FromSeconds(10), async () => await DoTheMagic("s").ConfigureAwait(false));
64+
65+
}
66+
67+
// async Task DoTheMagic()
68+
// {
69+
// Log("WAITING FOR STATE");
70+
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;
71+
72+
// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");
73+
74+
// await task.Task;
75+
// Log("STATE IS COOL DAMN IT!!!");
76+
77+
// }
78+
// async Task DoTheMagic(string test)
79+
// {
80+
// Log("WAITING FOR STATE");
81+
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;
82+
83+
// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");
84+
85+
// await task.Task;
86+
// Log("STATE IS COOL DAMN IT!!!");
87+
88+
// }
89+
// bool doingWork = false;
90+
// private async Task DoWork()
91+
// {
92+
// if (doingWork)
93+
// return;
94+
95+
// // Time to do work
96+
// Entity(Wh)
97+
// }
98+
99+
[HomeAssistantServiceCall]
100+
public void CallMeFromHass(dynamic data)
101+
{
102+
Log("A call from hass! {data}", data);
103+
}
61104
}
62105

63-
// async Task DoTheMagic()
106+
// public static class NotifyExtensions
64107
// {
65-
// Log("WAITING FOR STATE");
66-
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;
67-
68-
// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");
69-
70-
// await task.Task;
71-
// Log("STATE IS COOL DAMN IT!!!");
72-
108+
// public static async Task NotifyMobile(this NetDaemonApp app, string mobileId, string title, string message)
109+
// {
110+
// var data = new Obj
111+
// {
112+
// ["title"] = title,
113+
// ["message"] = message
114+
// };
115+
// await app.CallService("notify", mobileId, data, false);
116+
// }
117+
118+
// public static async Task NotifyMobileThread(this NetDaemonApp app, string mobileId, string title, string message, string threadId)
119+
// {
120+
// var data = new Obj
121+
// {
122+
// ["title"] = title,
123+
// ["message"] = message,
124+
// ["data"] = new Obj
125+
// {
126+
// ["push"] = new Obj
127+
// {
128+
// ["thread-id"] = threadId
129+
// }
130+
// }
131+
// };
132+
// await app.CallService("notify", mobileId, data, false);
133+
// }
73134
// }
74-
// async Task DoTheMagic(string test)
75-
// {
76-
// Log("WAITING FOR STATE");
77-
// // await Entity("binary_sensor.vardagsrum_pir").DelayUntilStateChange(to: "on").Task;
78-
79-
// var task = DelayUntilStateChange(new string[] { "binary_sensor.vardagsrum_pir" }, to: "on", from: "off");
80135

81-
// await task.Task;
82-
// Log("STATE IS COOL DAMN IT!!!");
83-
84-
// }
85-
// bool doingWork = false;
86-
// private async Task DoWork()
87-
// {
88-
// if (doingWork)
89-
// return;
90-
91-
// // Time to do work
92-
// Entity(Wh)
93-
// }
94-
95-
[HomeAssistantServiceCall]
96-
public void CallMeFromHass(dynamic data)
97-
{
98-
Log("A call from hass! {data}", data);
99-
}
100136
}
101-
102-
// public static class NotifyExtensions
103-
// {
104-
// public static async Task NotifyMobile(this NetDaemonApp app, string mobileId, string title, string message)
105-
// {
106-
// var data = new Obj
107-
// {
108-
// ["title"] = title,
109-
// ["message"] = message
110-
// };
111-
// await app.CallService("notify", mobileId, data, false);
112-
// }
113-
114-
// public static async Task NotifyMobileThread(this NetDaemonApp app, string mobileId, string title, string message, string threadId)
115-
// {
116-
// var data = new Obj
117-
// {
118-
// ["title"] = title,
119-
// ["message"] = message,
120-
// ["data"] = new Obj
121-
// {
122-
// ["push"] = new Obj
123-
// {
124-
// ["thread-id"] = threadId
125-
// }
126-
// }
127-
// };
128-
// await app.CallService("notify", mobileId, data, false);
129-
// }
130-
// }

exampleapps/apps/test2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
my_app:
2-
class: BatteryManager
2+
class: NStest.BatteryManager
33
# HelloWorldSecret: !secret test_secret
44
dependencies:
55
- global_app

src/App/NetDaemon.App/Common/INetDaemon.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public interface INetDaemonAppBase :
349349
/// </summary>
350350
public string? Id { get; set; }
351351

352+
/// <summary>
353+
/// Returns the description, is the decorating comment of app class
354+
/// </summary>
355+
public string Description { get; }
356+
352357
/// <summary>
353358
/// Gets or sets a flag indicating whether this app is enabled.
354359
/// This property property can be controlled from Home Assistant.
@@ -410,7 +415,7 @@ public interface IDoLogging
410415
/// <param name="message">The message to log</param>
411416
/// <param name="param">Params</param>
412417
void Log(Exception exception, string message, params object[] param);
413-
418+
414419
/// <summary>
415420
/// Logs an informational message
416421
/// </summary>
@@ -438,7 +443,7 @@ public interface IDoLogging
438443
/// <param name="message">The message to log</param>
439444
/// <param name="param">Params</param>
440445
void LogInformation(Exception exception, string message, params object[] param);
441-
446+
442447
/// <summary>
443448
/// Logs a debug message
444449
/// </summary>
@@ -479,7 +484,7 @@ public interface IDoLogging
479484
/// <param name="exception">A exception</param>
480485
/// <param name="message">The message to log</param>
481486
void LogError(Exception exception, string message);
482-
487+
483488
/// <summary>
484489
/// Logs an error message
485490
/// </summary>

src/App/NetDaemon.App/Common/NetDaemonAppBase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace NetDaemon.Common
1616
/// </summary>
1717
public abstract class NetDaemonAppBase : INetDaemonAppBase
1818
{
19+
/// <summary>
20+
/// A set of properties found in static analysis of code for each app
21+
/// </summary>
22+
public static Dictionary<string, Dictionary<string, string>> CompileTimeProperties { get; set; } = new Dictionary<string, Dictionary<string, string>>();
23+
1924
/// <summary>
2025
/// The NetDaemonHost instance
2126
/// </summary>
@@ -82,6 +87,28 @@ public IHttpHandler Http
8287
/// <inheritdoc/>
8388
public bool IsEnabled { get; set; } = true;
8489

90+
/// <inheritdoc/>
91+
public string Description
92+
{
93+
get
94+
{
95+
var app_key = this.GetType().FullName;
96+
97+
if (app_key is null)
98+
return "";
99+
100+
if (CompileTimeProperties.ContainsKey(app_key))
101+
{
102+
if (CompileTimeProperties[app_key].ContainsKey("description"))
103+
{
104+
return CompileTimeProperties[app_key]["description"];
105+
}
106+
}
107+
return "";
108+
}
109+
}
110+
111+
85112
/// <inheritdoc/>
86113
public ILogger? Logger { get; set; }
87114

src/DaemonRunner/DaemonRunner/Service/API/ApiController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public ApiController(
5555
[Route("apps")]
5656
public IEnumerable<ApiApplication>? Apps()
5757
{
58+
5859
return _host?.AllAppInstances.Select(n => new ApiApplication()
5960
{
6061
Id = n.Id,
6162
Dependencies = n.Dependencies,
62-
IsEnabled = n.IsEnabled
63+
IsEnabled = n.IsEnabled,
64+
Description = n.Description
6365
});
6466
}
65-
66-
6767
}
6868

6969
}

src/DaemonRunner/DaemonRunner/Service/API/ApiData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class ApiApplication
1111

1212
public bool IsEnabled { get; set; }
1313

14+
public string? Description { get; set; }
15+
1416
}
1517

1618
public class ApiConfig

0 commit comments

Comments
 (0)