-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomations.go
58 lines (43 loc) · 1.2 KB
/
automations.go
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
package hal
type Automation interface {
// Name is a friendly name for the automation, used in logs and stats.
Name() string
// Entities should return a list of entities that this automation should
// listen on. For every state change to one of these entities, the
// automation will be trigged.
Entities() Entities
// Action is called when the automation is triggered.
Action(trigger EntityInterface)
}
type AutomationConfig struct {
action func(trigger EntityInterface)
entities Entities
name string
}
func NewAutomation() *AutomationConfig {
return &AutomationConfig{}
}
func (c *AutomationConfig) Entities() Entities {
return c.entities
}
func (c *AutomationConfig) Action(trigger EntityInterface) {
c.action(trigger)
}
func (c *AutomationConfig) Name() string {
return c.name
}
func (c *AutomationConfig) WithAction(action func(trigger EntityInterface)) *AutomationConfig {
c.action = action
if c.name == "" {
c.name = getShortFunctionName(action)
}
return c
}
func (c *AutomationConfig) WithEntities(entities ...EntityInterface) *AutomationConfig {
c.entities = entities
return c
}
func (c *AutomationConfig) WithName(name string) *AutomationConfig {
c.name = name
return c
}