2
2
3
3
import java .util .List ;
4
4
5
+ import javax .annotation .Nonnull ;
5
6
import javax .annotation .Nullable ;
6
7
7
8
import org .apache .commons .lang3 .StringUtils ;
@@ -23,50 +24,44 @@ public class MqttHomeAssistantHelper {
23
24
@ Value ("${application.version}" ) private String version ;
24
25
25
26
public void clearAll (MqttSettings settings ) {
26
- var topic = StringUtils .joinWith ("/" , settings .homeAssistantBaseTopic (), "+" , "pcpanel" , "#" );
27
+ var topic = StringUtils .joinWith ("/" , settings .homeAssistant (). baseTopic (), "+" , "pcpanel" , "#" );
27
28
mqttService .removeAll (topic );
28
29
}
29
30
30
31
public void discover (MqttSettings settings , Device device ) {
31
- var haDevice = new HomeAssistantDevice (
32
- version ,
33
- List .of (device .getSerialNumber ()),
34
- "PCPanel Holdings, LLC" ,
35
- device .getDeviceType ().getNiceName (),
36
- device .getSerialNumber (),
37
- device .getSerialNumber ()
38
- );
32
+ var haDevice = buildDevice (device );
33
+ var availability = buildAvailability (settings );
39
34
40
- addAnalogValueConfigs (settings , device , haDevice );
41
- addBrightnessDevice (settings , device , haDevice );
42
- addLights (settings , device , haDevice );
43
- addButtons (settings , device , haDevice );
35
+ addAnalogValueConfigs (settings , device , haDevice , availability );
36
+ addBrightnessDevice (settings , device , haDevice , availability );
37
+ addLights (settings , device , haDevice , availability );
38
+ addButtons (settings , device , haDevice , availability );
44
39
}
45
40
46
- private void addLights (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
47
- addLogoLight (settings , device , haDevice );
48
- addControlLights (settings , device , haDevice );
41
+ private void addLights (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
42
+ addLogoLight (settings , device , haDevice , availability );
43
+ addControlLights (settings , device , haDevice , availability );
49
44
}
50
45
51
- private void addControlLights (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
46
+ private void addControlLights (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
52
47
for (var i = 0 ; i < device .getDeviceType ().getAnalogCount (); i ++) {
53
48
var buttonCount = device .getDeviceType ().getButtonCount ();
54
49
var type = i < buttonCount ? MqttTopicHelper .ColorType .knob : MqttTopicHelper .ColorType .slider ;
55
50
var idx = i < buttonCount ? i : i - buttonCount ;
56
51
57
- addControlLightConfig (settings , device , haDevice , i , type , idx );
52
+ addControlLightConfig (settings , device , haDevice , availability , i , type , idx );
58
53
if (type == MqttTopicHelper .ColorType .slider ) {
59
- addSliderLabelLightConfig (settings , device , haDevice , i , idx , type );
54
+ addSliderLabelLightConfig (settings , device , haDevice , availability , idx , type );
60
55
}
61
56
}
62
57
}
63
58
64
- private void addControlLightConfig (MqttSettings settings , Device device , HomeAssistantDevice haDevice , int i , MqttTopicHelper .ColorType type , int idx ) {
59
+ private void addControlLightConfig (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability , int i , MqttTopicHelper .ColorType type , int idx ) {
65
60
var controlConfigTopic = lightTopicFor (settings , device , "control_" + i );
66
61
var controlValueTopic = topicHelper .lightTopic (device .getSerialNumber (), type , idx );
67
62
68
63
var config = new HomeAssistantLightConfig (
69
- haDevice ,
64
+ haDevice , availability ,
70
65
StringUtils .capitalize (type .name ()) + " " + (idx + 1 ) + " Light" ,
71
66
device .getSerialNumber () + "_" + type .name () + "_" + idx ,
72
67
controlValueTopic ,
@@ -75,12 +70,12 @@ private void addControlLightConfig(MqttSettings settings, Device device, HomeAss
75
70
mqttService .send (controlConfigTopic , config , false );
76
71
}
77
72
78
- private void addSliderLabelLightConfig (MqttSettings settings , Device device , HomeAssistantDevice haDevice , int i , int idx , MqttTopicHelper .ColorType type ) {
73
+ private void addSliderLabelLightConfig (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability , int idx , MqttTopicHelper .ColorType type ) {
79
74
var labelConfigTopic = lightTopicFor (settings , device , "label_" + idx );
80
75
var labelValueTopic = topicHelper .lightTopic (device .getSerialNumber (), MqttTopicHelper .ColorType .label , idx );
81
76
82
77
var labelConfig = new HomeAssistantLightConfig (
83
- haDevice ,
78
+ haDevice , availability ,
84
79
StringUtils .capitalize (type .name ()) + " " + (idx + 1 ) + " Label Light" ,
85
80
device .getSerialNumber () + "_label_" + idx ,
86
81
labelValueTopic ,
@@ -89,49 +84,41 @@ private void addSliderLabelLightConfig(MqttSettings settings, Device device, Hom
89
84
mqttService .send (labelConfigTopic , labelConfig , false );
90
85
}
91
86
92
- private void addAnalogValueConfigs (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
87
+ private void addAnalogValueConfigs (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
93
88
for (var i = 0 ; i < device .getDeviceType ().getAnalogCount (); i ++) {
94
89
var configTopic = configTopicFor (settings , device , "number" , "analog" , i );
95
90
var valueTopic = topicHelper .valueTopic (device .getSerialNumber (), MqttTopicHelper .ValueType .analog , i );
96
91
97
92
var config = new HomeAssistantNumberConfig (
98
- haDevice ,
93
+ haDevice , availability ,
99
94
determineAnalogName (device , i ),
100
95
valueTopic ,
101
96
valueTopic ,
102
- null ,
103
97
determineAnalogIcon (device , i ),
104
- 0 ,
105
98
255 ,
106
- "slider" ,
107
- "analog_" + i ,
108
- device .getSerialNumber () + "_analog_" + i ,
109
- true );
99
+ device .getSerialNumber () + "_analog_" + i
100
+ );
110
101
mqttService .send (configTopic , config , false );
111
102
}
112
103
}
113
104
114
- private void addBrightnessDevice (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
105
+ private void addBrightnessDevice (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
115
106
var configTopic = configTopicFor (settings , device , "number" , "brightness" , 0 );
116
107
var valueTopic = topicHelper .valueTopic (device .getSerialNumber (), MqttTopicHelper .ValueType .brightness , 0 );
117
108
118
109
var config = new HomeAssistantNumberConfig (
119
- haDevice ,
110
+ haDevice , availability ,
120
111
"Brightness" ,
121
112
valueTopic ,
122
113
valueTopic ,
123
- null ,
124
114
"mdi:brightness-percent" ,
125
- 0 ,
126
115
100 ,
127
- "slider" ,
128
- "brightness" ,
129
- device .getSerialNumber () + "_brightness" ,
130
- true );
116
+ device .getSerialNumber () + "_brightness"
117
+ );
131
118
mqttService .send (configTopic , config , false );
132
119
}
133
120
134
- private void addLogoLight (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
121
+ private void addLogoLight (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
135
122
if (!device .getDeviceType ().isHasLogoLed ()) {
136
123
return ;
137
124
}
@@ -140,7 +127,7 @@ private void addLogoLight(MqttSettings settings, Device device, HomeAssistantDev
140
127
var valueTopic = topicHelper .lightTopic (device .getSerialNumber (), MqttTopicHelper .ColorType .logo , 0 );
141
128
142
129
var config = new HomeAssistantLightConfig (
143
- haDevice ,
130
+ haDevice , availability ,
144
131
"Logo Light" ,
145
132
device .getSerialNumber () + "_logo" ,
146
133
valueTopic ,
@@ -149,13 +136,13 @@ private void addLogoLight(MqttSettings settings, Device device, HomeAssistantDev
149
136
mqttService .send (configTopic , config , false );
150
137
}
151
138
152
- private void addButtons (MqttSettings settings , Device device , HomeAssistantDevice haDevice ) {
139
+ private void addButtons (MqttSettings settings , Device device , HomeAssistantDevice haDevice , @ Nullable HomeAssistantAvailability availability ) {
153
140
for (var i = 0 ; i < device .getDeviceType ().getButtonCount (); i ++) {
154
141
var configTopic = configTopicFor (settings , device , "binary_sensor" , "button" , i );
155
142
var valueTopic = topicHelper .actionTopic (device .getSerialNumber (), MqttTopicHelper .ActionType .button , i );
156
143
157
144
var config = new HomeAssistantButtonConfig (
158
- haDevice ,
145
+ haDevice , availability ,
159
146
valueTopic ,
160
147
"Button " + (i + 1 ),
161
148
device .getSerialNumber () + "_button_" + i
@@ -166,7 +153,7 @@ private void addButtons(MqttSettings settings, Device device, HomeAssistantDevic
166
153
167
154
private String configTopicFor (MqttSettings settings , Device device , String domain , String type , int idx ) {
168
155
return StringUtils .joinWith ("/" ,
169
- settings .homeAssistantBaseTopic (),
156
+ settings .homeAssistant (). baseTopic (),
170
157
domain ,
171
158
"pcpanel" ,
172
159
device .getSerialNumber ().toLowerCase () + "_" + type + "_" + idx ,
@@ -176,7 +163,7 @@ private String configTopicFor(MqttSettings settings, Device device, String domai
176
163
177
164
private String lightTopicFor (MqttSettings settings , Device device , String name ) {
178
165
return StringUtils .joinWith ("/" ,
179
- settings .homeAssistantBaseTopic (),
166
+ settings .homeAssistant (). baseTopic (),
180
167
"light" ,
181
168
"pcpanel" ,
182
169
device .getSerialNumber ().toLowerCase () + "_" + name ,
@@ -202,10 +189,10 @@ private String determineAnalogName(Device device, int i) {
202
189
203
190
record HomeAssistantNumberConfig (
204
191
HomeAssistantDevice device ,
192
+ @ Nullable HomeAssistantAvailability availability ,
205
193
String name ,
206
194
String command_topic ,
207
195
String state_topic ,
208
- @ Nullable String device_class ,
209
196
String icon ,
210
197
int min , // 0
211
198
int max , // 255
@@ -214,10 +201,26 @@ record HomeAssistantNumberConfig(
214
201
String unique_id ,
215
202
boolean retain // true
216
203
) {
204
+ HomeAssistantNumberConfig (HomeAssistantDevice device , @ Nullable HomeAssistantAvailability availability , String name , String command_topic , String state_topic , String icon , int max , String object_id ) {
205
+ this (
206
+ device , availability ,
207
+ name ,
208
+ command_topic ,
209
+ state_topic ,
210
+ icon ,
211
+ 0 ,
212
+ max ,
213
+ "slider" ,
214
+ object_id ,
215
+ object_id ,
216
+ true
217
+ );
218
+ }
217
219
}
218
220
219
221
record HomeAssistantLightConfig (
220
222
HomeAssistantDevice device ,
223
+ @ Nullable HomeAssistantAvailability availability ,
221
224
String name ,
222
225
String object_id ,
223
226
String unique_id ,
@@ -236,8 +239,8 @@ record HomeAssistantLightConfig(
236
239
List <String > supported_color_modes ,
237
240
boolean retain
238
241
) {
239
- HomeAssistantLightConfig (HomeAssistantDevice device , String name , String unique_id , String command_topic , String icon ) {
240
- this (device , name , unique_id , unique_id , command_topic , icon ,
242
+ HomeAssistantLightConfig (HomeAssistantDevice device , @ Nullable HomeAssistantAvailability availability , String name , String unique_id , String command_topic , String icon ) {
243
+ this (device , availability , name , unique_id , unique_id , command_topic , icon ,
241
244
"template" ,
242
245
"#000000" ,
243
246
"#{{ '%02x%02x%02x' | format(" + asInt ("red" ) + ", " + asInt ("green" ) + ", " + asInt ("blue" ) + ") }}" ,
@@ -258,6 +261,7 @@ private static String asInt(String name) {
258
261
259
262
record HomeAssistantButtonConfig ( // Is actually a binary sensor
260
263
HomeAssistantDevice device ,
264
+ @ Nullable HomeAssistantAvailability availability ,
261
265
String state_topic ,
262
266
String name ,
263
267
String object_id ,
@@ -268,15 +272,39 @@ record HomeAssistantButtonConfig( // Is actually a binary sensor
268
272
String payload_off ,
269
273
String payload_on
270
274
) {
271
- HomeAssistantButtonConfig (HomeAssistantDevice device , String command_topic , String name , String object_id ) {
272
- this (device , command_topic , name , object_id , object_id ,
275
+ HomeAssistantButtonConfig (HomeAssistantDevice device , @ Nullable HomeAssistantAvailability availability , String command_topic , String name , String object_id ) {
276
+ this (device , availability , command_topic , name , object_id , object_id ,
273
277
"click" ,
274
278
"mdi:toggle-switch" ,
275
279
"release" ,
276
280
"click" );
277
281
}
278
282
}
279
283
284
+ @ Nonnull
285
+ private HomeAssistantDevice buildDevice (Device device ) {
286
+ return new HomeAssistantDevice (
287
+ version ,
288
+ List .of (device .getSerialNumber ()),
289
+ "PCPanel Holdings, LLC" ,
290
+ device .getDeviceType ().getNiceName (),
291
+ device .getSerialNumber (),
292
+ device .getSerialNumber ()
293
+ );
294
+ }
295
+
296
+ private @ Nullable HomeAssistantAvailability buildAvailability (MqttSettings settings ) {
297
+ if (settings .homeAssistant ().availability ()) {
298
+ return new HomeAssistantAvailability (
299
+ topicHelper .availabilityTopic (),
300
+ "online" ,
301
+ "offline" ,
302
+ "{{'offline' if (value is undefined or value != 'online') else 'online'}}"
303
+ );
304
+ }
305
+ return null ;
306
+ }
307
+
280
308
record HomeAssistantDevice (
281
309
String sw_version ,
282
310
List <String > identifiers ,
@@ -286,4 +314,12 @@ record HomeAssistantDevice(
286
314
String serial_number
287
315
) {
288
316
}
317
+
318
+ record HomeAssistantAvailability (
319
+ String topic ,
320
+ String payload_available ,
321
+ String payload_not_available ,
322
+ String value_template
323
+ ) {
324
+ }
289
325
}
0 commit comments