@@ -21,10 +21,10 @@ module.exports = function(RED) {
21
21
input : {
22
22
entity_id : {
23
23
messageProp : 'payload.entity_id' ,
24
- configProp : 'entity_id' , // Will be used if value not found on message,
24
+ configProp : 'entity_id' ,
25
25
validation : {
26
26
haltOnFail : true ,
27
- schema : Joi . string ( ) // Validates on message if exists, Joi will also attempt coercion
27
+ schema : Joi . string ( ) . label ( 'entity_id' )
28
28
}
29
29
}
30
30
}
@@ -38,64 +38,45 @@ module.exports = function(RED) {
38
38
/* eslint-disable camelcase */
39
39
async onInput ( { parsedMessage, message } ) {
40
40
const config = this . nodeConfig ;
41
- const entityId = config . entity_id
42
- ? config . entity_id
43
- : parsedMessage . entity_id . value ;
44
- const logAndContinueEmpty = logMsg => {
45
- this . node . warn ( logMsg ) ;
46
- return { payload : { } } ;
47
- } ;
41
+ const entityId = parsedMessage . entity_id . value ;
48
42
49
43
if ( config . server === null ) {
50
44
this . node . error ( 'No valid server selected.' ) ;
51
- return null ;
45
+ return ;
52
46
}
53
47
54
- if ( ! entityId )
55
- return logAndContinueEmpty (
56
- 'entity ID not set, cannot get current state, sending empty payload'
57
- ) ;
58
-
59
- const currentState = this . utils . merge (
48
+ const entity = this . utils . merge (
60
49
{ } ,
61
50
await config . server . homeAssistant . getStates ( entityId )
62
51
) ;
63
- if ( ! currentState . entity_id )
64
- return logAndContinueEmpty (
65
- `entity could not be found in cache for entity_id: ${ entityId } , sending empty payload`
52
+
53
+ if ( ! entity . entity_id ) {
54
+ this . node . error (
55
+ `entity could not be found in cache for entity_id: ${ entityId } `
66
56
) ;
57
+ return ;
58
+ }
67
59
68
- currentState . timeSinceChangedMs =
69
- Date . now ( ) - new Date ( currentState . last_changed ) . getTime ( ) ;
60
+ entity . timeSinceChangedMs =
61
+ Date . now ( ) - new Date ( entity . last_changed ) . getTime ( ) ;
70
62
71
63
// Convert and save original state if needed
72
64
if ( config . state_type && config . state_type !== 'str' ) {
73
- currentState . original_state = currentState . state ;
74
- currentState . state = this . getCastValue (
65
+ entity . original_state = entity . state ;
66
+ entity . state = this . getCastValue (
75
67
config . state_type ,
76
- currentState . state
68
+ entity . state
77
69
) ;
78
70
}
79
71
80
72
config . halt_if_compare = config . halt_if_compare || 'is' ;
81
73
config . halt_if_type = config . halt_if_type || 'str' ;
82
74
83
- const isHaltValid = await this . getComparatorResult (
84
- config . halt_if_compare ,
85
- config . halt_if ,
86
- currentState . state ,
87
- config . halt_if_type ,
88
- {
89
- message,
90
- entity : currentState
91
- }
92
- ) ;
93
- const shouldHaltIfState = config . halt_if && isHaltValid ;
94
-
95
- // default switch to true if undefined (backward compatibility
96
- const override_topic = config . override_topic !== false ;
97
- if ( override_topic ) message . topic = entityId ;
75
+ // default switch to true if undefined (backward compatibility)
76
+ message . topic =
77
+ config . override_topic !== false ? entityId : message . topic ;
98
78
79
+ // Set Defaults
99
80
if ( config . state_location === undefined ) {
100
81
config . state_location = 'payload' ;
101
82
config . override_payload =
@@ -107,29 +88,39 @@ module.exports = function(RED) {
107
88
config . override_data !== false ? 'msg' : 'none' ;
108
89
}
109
90
91
+ // Set 'State Location'
110
92
this . setContextValue (
111
- currentState . state ,
93
+ entity . state ,
112
94
config . override_payload ,
113
95
config . state_location ,
114
96
message
115
97
) ;
116
98
99
+ // Set 'Entity Location'
117
100
this . setContextValue (
118
- currentState ,
101
+ entity ,
119
102
config . override_data ,
120
103
config . entity_location ,
121
104
message
122
105
) ;
123
106
124
- if ( shouldHaltIfState ) {
125
- const debugMsg = `Get current state: halting processing due to current state of ${ entityId } matches "halt if state" option` ;
126
- this . debug ( debugMsg ) ;
127
- this . debugToClient ( debugMsg ) ;
128
- this . setStatusFailed ( currentState . state ) ;
129
- this . node . send ( [ null , message ] ) ;
107
+ const isHaltValid = await this . getComparatorResult (
108
+ config . halt_if_compare ,
109
+ config . halt_if ,
110
+ entity . state ,
111
+ config . halt_if_type ,
112
+ {
113
+ message,
114
+ entity
115
+ }
116
+ ) ;
117
+
118
+ if ( config . halt_if && isHaltValid ) {
119
+ this . setStatusFailed ( entity . state ) ;
120
+ this . send ( [ null , message ] ) ;
130
121
} else {
131
- this . setStatusSuccess ( currentState . state ) ;
132
- this . node . send ( [ message , null ] ) ;
122
+ this . setStatusSuccess ( entity . state ) ;
123
+ this . send ( [ message , null ] ) ;
133
124
}
134
125
}
135
126
}
0 commit comments