@@ -13,20 +13,20 @@ import (
13
13
)
14
14
15
15
type generalConfig struct {
16
- Name string `json:"name"`
17
- AvailabilityTopic string `json:"avty_t,omitempty"`
18
- PayloadAvailable string `json:"pl_avail,omitempty"`
19
- PayloadNotAvailable string `json:"pl_not_avail,omitempty"`
20
- UniqueId string `json:"uniq_id"`
21
- Device * device `json:"dev,omitempty"`
16
+ Name string `json:"name"`
17
+ AvailabilityTopic string `json:"avty_t,omitempty"`
18
+ PayloadAvailable string `json:"pl_avail,omitempty"`
19
+ PayloadNotAvailable string `json:"pl_not_avail,omitempty"`
20
+ UniqueId string `json:"uniq_id"`
21
+ Icon string `json:"ic,omitempty"`
22
+ Device device `json:"dev,omitempty"`
22
23
}
23
24
24
25
type sensorConfig struct {
25
26
generalConfig
26
27
27
28
StateTopic string `json:"stat_t"`
28
29
MeasurementUnit string `json:"unit_of_meas,omitempty"`
29
- Icon string `json:"ic,omitempty"`
30
30
ForceUpdate * bool `json:"frc_upd,omitempty"`
31
31
}
32
32
@@ -82,7 +82,17 @@ func (c *Client) PublishDiscoveryConfig(config config.TopicConfigurations) {
82
82
//trigger
83
83
for _ , trigger := range config .Trigger {
84
84
targetTopic := fmt .Sprintf ("%sswitch/%s/%s/config" , c .TopicPrefix , c .DeviceId , friendlyName (trigger .Name ))
85
- payload := c .generatePayloadForTriggerAction (config .Availability , trigger )
85
+ payload := c .generateSwitchPayloadForTriggerAction (config .Availability , trigger )
86
+ c .MqttClient .Publish (targetTopic , byte (0 ), false , payload )
87
+
88
+ //publish the trigger-result as sensor data
89
+ targetTopic = fmt .Sprintf ("%ssensor/%s_%s/result/config" , c .TopicPrefix , c .DeviceId , friendlyName (trigger .Name ))
90
+ payload = c .generateResultPayloadForTriggerAction (config .Availability , trigger )
91
+ c .MqttClient .Publish (targetTopic , byte (0 ), false , payload )
92
+
93
+ //publish the trigger-state as sensor data
94
+ targetTopic = fmt .Sprintf ("%ssensor/%s_%s/state/config" , c .TopicPrefix , c .DeviceId , friendlyName (trigger .Name ))
95
+ payload = c .generateStatePayloadForTriggerAction (config .Availability , trigger )
86
96
c .MqttClient .Publish (targetTopic , byte (0 ), false , payload )
87
97
}
88
98
}
@@ -91,20 +101,24 @@ func friendlyName(name string) string {
91
101
return strings .Replace (name , " " , "_" , - 1 )
92
102
}
93
103
104
+ func (c * Client ) buildDevice () device {
105
+ return device {
106
+ Name : c .DeviceName ,
107
+ Ids : []string {c .DeviceId },
108
+ Manufacturer : "rainu" ,
109
+ Model : runtime .GOOS ,
110
+ Version : "mqtt-executor" ,
111
+ }
112
+ }
113
+
94
114
func (c * Client ) generatePayloadForStatus (availability * config.Availability ) []byte {
95
115
conf := sensorConfig {
96
116
generalConfig : generalConfig {
97
117
Name : "Status" ,
98
118
PayloadAvailable : availability .Payload .Available ,
99
119
PayloadNotAvailable : availability .Payload .Unavailable ,
100
120
UniqueId : fmt .Sprintf ("%s_status" , c .DeviceId ),
101
- Device : & device {
102
- Name : c .DeviceName ,
103
- Ids : []string {c .DeviceId },
104
- Manufacturer : "rainu" ,
105
- Model : runtime .GOOS ,
106
- Version : "mqtt-executor" ,
107
- },
121
+ Device : c .buildDevice (),
108
122
},
109
123
StateTopic : availability .Topic ,
110
124
}
@@ -115,6 +129,7 @@ func (c *Client) generatePayloadForStatus(availability *config.Availability) []b
115
129
116
130
payload , err := json .Marshal (conf )
117
131
if err != nil {
132
+ //the "marshalling" is relatively safe - it should never appear at runtime
118
133
panic (err )
119
134
}
120
135
return payload
@@ -125,37 +140,31 @@ func (c *Client) generatePayloadForSensor(availability *config.Availability, sen
125
140
conf := sensorConfig {
126
141
generalConfig : generalConfig {
127
142
Name : sensor .Name ,
143
+ Icon : sensor .Icon ,
128
144
UniqueId : fmt .Sprintf ("%s_%s" , c .DeviceId , friendlyName (sensor .Name )),
129
- Device : & device {
130
- Ids : []string {c .DeviceId },
131
- },
145
+ Device : c .buildDevice (),
132
146
},
133
147
StateTopic : sensor .ResultTopic ,
134
148
MeasurementUnit : sensor .Unit ,
135
- Icon : "" ,
136
149
ForceUpdate : & bTrue ,
137
150
}
138
- if availability != nil {
139
- conf .AvailabilityTopic = availability .Topic
140
- conf .PayloadAvailable = availability .Payload .Available
141
- conf .PayloadNotAvailable = availability .Payload .Unavailable
142
- }
151
+ addAvailability (& conf .generalConfig , availability )
143
152
144
153
payload , err := json .Marshal (conf )
145
154
if err != nil {
155
+ //the "marshalling" is relatively safe - it should never appear at runtime
146
156
panic (err )
147
157
}
148
158
return payload
149
159
}
150
160
151
- func (c * Client ) generatePayloadForTriggerAction (availability * config.Availability , trigger config.Trigger ) []byte {
161
+ func (c * Client ) generateSwitchPayloadForTriggerAction (availability * config.Availability , trigger config.Trigger ) []byte {
152
162
conf := triggerConfig {
153
163
generalConfig : generalConfig {
154
164
Name : fmt .Sprintf ("%s" , trigger .Name ),
165
+ Icon : trigger .Icon ,
155
166
UniqueId : fmt .Sprintf ("%s_%s" , c .DeviceId , friendlyName (trigger .Name )),
156
- Device : & device {
157
- Ids : []string {c .DeviceId },
158
- },
167
+ Device : c .buildDevice (),
159
168
},
160
169
CommandTopic : trigger .Topic ,
161
170
PayloadStart : mqtt .PayloadStart ,
@@ -164,15 +173,60 @@ func (c *Client) generatePayloadForTriggerAction(availability *config.Availabili
164
173
StateRunning : mqtt .PayloadStatusRunning ,
165
174
StateStopped : mqtt .PayloadStatusStopped ,
166
175
}
167
- if availability != nil {
168
- conf .AvailabilityTopic = availability .Topic
169
- conf .PayloadAvailable = availability .Payload .Available
170
- conf .PayloadNotAvailable = availability .Payload .Unavailable
176
+ addAvailability (& conf .generalConfig , availability )
177
+
178
+ payload , err := json .Marshal (conf )
179
+ if err != nil {
180
+ //the "marshalling" is relatively safe - it should never appear at runtime
181
+ panic (err )
171
182
}
183
+ return payload
184
+ }
185
+
186
+ func (c * Client ) generateResultPayloadForTriggerAction (availability * config.Availability , trigger config.Trigger ) []byte {
187
+ conf := sensorConfig {
188
+ generalConfig : generalConfig {
189
+ Name : fmt .Sprintf ("%s - Result" , trigger .Name ),
190
+ Icon : trigger .Icon ,
191
+ UniqueId : fmt .Sprintf ("%s_%s_result" , c .DeviceId , friendlyName (trigger .Name )),
192
+ Device : c .buildDevice (),
193
+ },
194
+ StateTopic : fmt .Sprintf ("%s/%s" , trigger .Topic , mqtt .TopicSuffixResult ),
195
+ }
196
+ addAvailability (& conf .generalConfig , availability )
197
+
198
+ payload , err := json .Marshal (conf )
199
+ if err != nil {
200
+ //the "marshalling" is relatively safe - it should never appear at runtime
201
+ panic (err )
202
+ }
203
+ return payload
204
+ }
205
+
206
+ func (c * Client ) generateStatePayloadForTriggerAction (availability * config.Availability , trigger config.Trigger ) []byte {
207
+ conf := sensorConfig {
208
+ generalConfig : generalConfig {
209
+ Name : fmt .Sprintf ("%s - State" , trigger .Name ),
210
+ Icon : trigger .Icon ,
211
+ UniqueId : fmt .Sprintf ("%s_%s_state" , c .DeviceId , friendlyName (trigger .Name )),
212
+ Device : c .buildDevice (),
213
+ },
214
+ StateTopic : fmt .Sprintf ("%s/%s" , trigger .Topic , mqtt .TopicSuffixState ),
215
+ }
216
+ addAvailability (& conf .generalConfig , availability )
172
217
173
218
payload , err := json .Marshal (conf )
174
219
if err != nil {
220
+ //the "marshalling" is relatively safe - it should never appear at runtime
175
221
panic (err )
176
222
}
177
223
return payload
178
224
}
225
+
226
+ func addAvailability (config * generalConfig , availability * config.Availability ) {
227
+ if availability != nil {
228
+ config .AvailabilityTopic = availability .Topic
229
+ config .PayloadAvailable = availability .Payload .Available
230
+ config .PayloadNotAvailable = availability .Payload .Unavailable
231
+ }
232
+ }
0 commit comments