-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtext.pas
151 lines (133 loc) · 3.62 KB
/
mqtext.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
149
150
151
{
https://www.home-assistant.io/integrations/text.mqtt/
Version 2024.12.1 - initial release
2024.12.15
added
+ text and maxlen variables
}
{$mode Delphi}
unit mqText;
interface
Uses
Classes, SysUtils, mqttDevice;
Type
ETextNames = tnConfig..tnValueTemplate;
{
tnConfig,
tnAvailabilityTopic,
tnAvailabilityMode,
tnAvailabilityTemplate,
tnCommandTemplate,
tnCommandTopic,
tnEnabledByDefault,
tnEncoding,
tnEntityCategory,
tnEntityPicture,
tnJsonAttributesTemplate,
tnJsonAttributesTopic,
tnMax,
tnMin,
tnMode,
tnName,
tnObjectId,
tnPattern,
tnQos,
tnRetain,
tnStateTopic,
tnUniqueId,
tnValueTemplate
}
Const
CTextNames : array [ETextNames] of string = (
'config',
'availability_topic',
'availability_mode',
'availability_template',
'command_template',
'command_topic',
'enabled_by_default',
'encoding',
'entity_category',
'entity_picture',
'json_attributes_template',
'json_attributes_topic',
'max',
'min',
'mode',
'name',
'object_id',
'pattern',
'qos',
'retain',
'state_topic',
'unique_id',
'value_template'
);
Type
{ TMQTTText }
TMQTTText = Class(TMQTTBaseObject)
private
public
Text : string;
MaxLen : integer;
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
{ TMQTTText }
constructor TMQTTText.Create;
begin
Inherited Create;
FClass := hdcText;
FConfig.Add(CTextNames, []);
FTopics := [Ord(tnConfig), Ord(tnCommandTopic), Ord(tnStateTopic)];
FConfig[CTextNames[tnCommandTopic]] := 'state'; //required
FConfig[CTextNames[tnMax]] := '255';
FConfig[CTextNames[tnMin]] := '0';
FConfig[CTextNames[tnMode]] := 'text'; //or 'password'
FConfigTopic := tnConfig;
FStateTopic := tnStateTopic;
FCommandTopic := tnCommandTopic;
FIDTopic := tnObjectId;
MaxLen := 255;
end;
function TMQTTText.FromEnumToString(AConfigItem:Integer):string;
begin
Result := '';
if (AConfigItem < Ord(Low(ETextNames))) or (AConfigItem > Ord(High(ETextNames))) then Exit;
Result := CTextNames[ETextNames(AConfigItem)];
end; //FromEnumToString
function TMQTTText.FromStringToEnum(AName: string): EAllNames;
Var
m : EAllNames;
begin
Result := eanNone;
for m := tnConfig to tnValueTemplate do begin
if AName = CTextNames[m] then begin
Result := m;
Break;
end;
end; //for
end; //FromStringToEnum
function TMQTTText.Subscribe(ATopic: EAllNames; const SubId: integer): Boolean;
Var
CompleteTopic : string;
begin
Result := False;
if not Assigned(FParent) then Exit; // **********
CompleteTopic := TopicPrefix(ATopic) + Config[CTextNames[ATopic]];
Result := FParent.Subscribe(CompleteTopic, SubId);
end; //Subscribe
Initialization
HATypeInfo[tnAvailabilityTemplate ] := hatTemplate;
HATypeInfo[tnCommandTemplate ] := hatTemplate;
HATypeInfo[tnEnabledByDefault ] := hatBoolean;
HATypeInfo[tnJsonAttributesTemplate ] := hatTemplate;
HATypeInfo[tnMax ] := hatInteger;
HATypeInfo[tnMin ] := hatInteger;
HATypeInfo[tnQos ] := hatInteger;
HATypeInfo[tnRetain ] := hatBoolean;
HATypeInfo[tnValueTemplate ] := hatTemplate;
end.