-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqbutton.pas
148 lines (132 loc) · 3.5 KB
/
mqbutton.pas
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
https://www.home-assistant.io/integrations/button.mqtt/
Version 2024.12.15
}
{$mode Delphi}
unit mqButton;
interface
Uses
Classes, SysUtils, mqttDevice;
Type
EButtonNames = bnConfig..bnValueTemplate;
{
bnConfig,
bnAvailabilityMode,
bnAvailabilityTemplate,
bnAvailabilityTopic,
bnCommandTemplate,
bnCommandTopic,
bnDeviceClass,
bnEnabledByDefault,
bnEncoding,
bnEntityCategory,
bnIcon,
bnJsonAttributesTemplate,
bnJsonAttributesTopic,
bnName,
bnObjectId,
bnPayloadAvailable,
bnPayloadNotAvailable,
bnPayloadPress,
bnQos,
bnRetain,
bnUniqueId,
bnValueTemplate
}
EButtonDeviceClass = ( //bnDeviceClass
bdcNone,
bdcIdentify,
bdcRestart,
bdcUpdate
);
Const
CButtonNames : array[EButtonNames] of string = (
'config',
'availability_mode',
'availability_template',
'availability_topic',
'command_template',
'command_topic',
'device_class',
'enabled_by_default',
'encoding',
'entity_category',
'icon',
'json_attributes_template',
'json_attributes_topic',
'name',
'object_id',
'payload_available',
'payload_not_available',
'payload_press',
'qos',
'retain',
'unique_id',
'value_template'
);
CButtonDeviceClass : array [EButtonDeviceClass] of string = (
'None',
'identify',
'restart',
'update'
);
Type
{ TMQTTButton }
TMQTTButton = Class(TMQTTBaseObject)
private
public
Constructor Create;
function FromEnumToString(AConfigItem:Integer):string; Override;
function FromStringToEnum(AName: string): EAllNames; Override;
function Subscribe(ATopic: EAllNames; const SubId: integer): Boolean; Override;
end;
implementation
{ TMQTTButton }
constructor TMQTTButton.Create;
begin
Inherited Create;
FClass := hdcButton;
FConfig.Add(CButtonNames, []);
FTopics := [Ord(bnConfig), Ord(bnCommandTopic)];
FConfig[CButtonNames[bnCommandTopic]] := 'set'; //required
FConfig[CButtonNames[bnPayloadPress]] := 'PRESS';
FConfigTopic := bnConfig;
//FStateTopic := not used
FCommandTopic := bnCommandTopic;
FIDTopic := bnObjectId;
end;
function TMQTTButton.FromEnumToString(AConfigItem:Integer):string;
begin
Result := '';
if (AConfigItem < Ord(Low(EButtonNames))) or (AConfigItem > Ord(High(EButtonNames))) then Exit;
Result := CButtonNames[EButtonNames(AConfigItem)];
end; //FromEnumToString
function TMQTTButton.FromStringToEnum(AName: string): EAllNames;
Var
m : EAllNames;
begin
Result := eanNone;
for m := bnConfig to bnValueTemplate do begin
if AName = CButtonNames[m] then begin
Result := m;
Break;
end;
end; //for
end; //FromStringToEnum
function TMQTTButton.Subscribe(ATopic: EAllNames; const SubId: integer): Boolean;
Var
CompleteTopic : string;
begin
Result := False;
if not Assigned(FParent) then Exit;
CompleteTopic := TopicPrefix(ATopic) + Config[CButtonNames[ATopic]];
Result := FParent.Subscribe(CompleteTopic, SubId);
end; //Subscribe
Initialization
HATypeInfo[bnAvailabilityTemplate] := hatTemplate;
HATypeInfo[bnEnabledByDefault] := hatTemplate;
HATypeInfo[bnJsonAttributesTemplate] := hatTemplate;
HATypeInfo[bnQos] := hatInteger;
HATypeInfo[bnRetain] := hatBoolean;
HATypeInfo[bnValueTemplate] := hatTemplate;
end.