-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightOnRegistry.cs
107 lines (97 loc) · 4.5 KB
/
LightOnRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
namespace HaKafkaNet.ExampleApp;
/// <summary>
/// This class has 6 different implementations of the same automation
/// created expressly for the purpose of demonstrating the options
/// you have for structuring your code
/// </summary>
public class LightOnRegistry : IAutomationRegistry
{
private readonly IHaServices _services;
private readonly IAutomationBuilder _builder;
private readonly IAutomationFactory _factory;
public const string OFFICE_MOTION = "binary_sensor.office_motion";
public const string OFFICE_LIGHT = "light.office_light";
public LightOnRegistry(IHaServices services, IAutomationBuilder builder, IAutomationFactory factory)
{
_services = services;
_builder = builder;
_factory = factory;
}
public void Register(IRegistrar reg)
{
reg.Register(Register().ToArray());
reg.RegisterDelayed(RegisterContitionals().ToArray());
}
public IEnumerable<IAutomation> Register()
{
//prebuilt for your convenience
//you could write your own extension methods if you like
yield return _factory.LightOnMotion(OFFICE_MOTION, OFFICE_LIGHT)
//meta data is completely optional
.WithMeta(new AutomationMetaData(){
Name = "Office Light On Motion", //defaults to GetType().Name
Description = "from factory prebuilt", //defaults to GetType().FullName
Enabled = false //defaults to true
});
//use the factory to create any automation
//you could put logic like this in an extension method
yield return _factory.SimpleAutomation(
[OFFICE_MOTION],
async (stateChange, ct)=> {
if (stateChange.New.State == "on")
{
await _services.Api.TurnOn(OFFICE_LIGHT, ct);
}
}).WithMeta(new AutomationMetaData(){
Name = "Office Light On Motion",
Description = "from factory manual",
Enabled = false
});
//create your own automations for reuse
//you could also put a manual construction like this in a facotry extension method
yield return new LightOnCustomAutomation(_services.Api, OFFICE_MOTION, OFFICE_LIGHT, 200, "Office Light On Motion", "custom built");
//the builder offers a more descriptive way create automations
yield return _builder.CreateSimple(false) // false is for enabled at startup which defaults to true if not specified
.WithName("Office Light On Motion")
.WithDescription("from builder without services")
.WithTriggers(OFFICE_MOTION)
.WithExecution(async (stateChange, ct) => {
if (stateChange.New.State == "on")
{
//services reference comes from this class
await _services.Api.TurnOn(OFFICE_LIGHT, ct);
}
})
.Build();
// you can rely on the builder to pass in home assistant services
yield return _builder.CreateSimple(false)
.WithName("Office Light On Motion")
.WithDescription("from builder with services")
.WithTriggers(OFFICE_MOTION)
.WithExecution(async (stateChange, ct) =>{
if (stateChange.New.State == "on")
{
//services reference injected into callback
await _services.Api.TurnOn(OFFICE_LIGHT, ct);
}
})
.Build();
}
public IEnumerable<IConditionalAutomation> RegisterContitionals()
{
//the conditional automation us typically used to run an automation on a delay
//by not specifying the 'For" it defaults to TimeSpan.Zero and runs immediately
//this allows you to clean up some of the if statements in the examples above
yield return _builder.CreateConditional(false)
.WithName("Office Light On Motion")
.WithDescription("from builder using conditional")
.WithTriggers(OFFICE_MOTION)
.When(sc => sc.New.State == "on") // there is an asynchronous overload for this method should you need to call services
.Then(async ct =>
{
await _services.Api.TurnOn(OFFICE_LIGHT, ct);
})
.Build();
}
}