-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqvalve.pas
204 lines (182 loc) · 5.58 KB
/
mqvalve.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
https://www.home-assistant.io/integrations/valve.mqtt/
Version 2024.12.15
}
{$mode Delphi}
unit mqValve;
interface
Uses
Classes, SysUtils, mqttDevice;
Type
EValveNames = vnConfig..vnValueTemplate;
{
vnConfig,
vnAvailabilityMode,
vnAvailabilityTemplate,
vnAvailabilityTopic,
vnCommandTemplate,
vnCommandTopic,
vnDeviceClass,
vnEnabledByDefault,
vnEncoding,
vnEntityCategory,
vnIcon,
vnJsonAttributesTemplate,
vnJsonAttributesTopic,
vnName,
vnObjectId,
vnOptimistic,
vnPayloadAvailable,
vnPayloadClose,
vnPayloadNotAvailable,
vnPayloadOpen,
vnPayloadStop,
vnPositionClosed,
vnPositionOpen,
vnQos,
vnReportsPosition,
vnRetain,
vnStateClosed,
vnStateClosing,
vnStateOpen,
vnStateOpening,
vnStateStopped, //@@@ not existing yet
vnStateTopic,
vnUniqueId,
vnValueTemplate
}
Const
CValveNames : array [EValveNames] 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',
'optimistic',
'payload_available',
'payload_close',
'payload_not_available',
'payload_open',
'payload_stop',
'position_closed',
'position_open',
'qos',
'reports_position',
'retain',
'state_closed',
'state_closing',
'state_open',
'state_opening',
'state_stopped',
'state_topic',
'unique_id',
'value_template'
);
Type //@@@this is yet missing in HA
EValveStates = (evsClosed, evsClosing, evsOpen, evsOpening, evsStopped);
Const //@@@compiler bug: need to reinitialize in Initialization
EValveStatesNames : array [EValveStates] of EValveNames = (
vnStateClosed, vnStateClosing, vnStateOpen, vnStateOpening, vnStateStopped
);
Type
{ TMQTTValve }
TMQTTValve = Class(TMQTTBaseObject)
private
public
State : EValveStates;
Position : integer;
ReportsPosition : Boolean;
Constructor Create;
function FromEnumToString(AConfigItem:Integer):string; Override;
function FromStringToEnum(AName: string): EAllNames; Override;
function Subscribe(ATopic: EAllNames; const SubId: integer): Boolean; Override;
Function JSONState:string;
end;
implementation
{ TMQTTValve }
constructor TMQTTValve.Create;
begin
Inherited Create;
FClass := hdcValve;
FConfig.Add(CValveNames, []);
FTopics := [Ord(vnConfig), Ord(vnCommandTopic), Ord(vnStateTopic)];
FConfig[CValveNames[vnPositionClosed]] := '0';
FConfig[CValveNames[vnPositionOpen]] := '100';
ReportsPosition := False;
FConfig[CValveNames[vnReportsPosition]] := 'false'; //if true the following has to be empty
FConfig[CValveNames[vnPayloadClose]] := 'CLOSE';
FConfig[CValveNames[vnPayloadOpen]] := 'OPEN';
//FConfig[CValveNames[vnPayloadStop]] := 'STOP'; //no default
FConfig[CValveNames[vnStateClosed]] := 'closed';
FConfig[CValveNames[vnStateClosing]] := 'closing';
FConfig[CValveNames[vnStateOpen]] := 'open';
FConfig[CValveNames[vnStateOpening]] := 'opening';
FConfig[CValveNames[vnStateStopped]] := 'stopped';
FConfigTopic := vnConfig;
FStateTopic := vnStateTopic;
FCommandTopic := vnCommandTopic;
FIDTopic := vnObjectId;
end;
function TMQTTValve.FromEnumToString(AConfigItem:Integer):string;
begin
Result := '';
if (AConfigItem < Ord(Low(EValveNames))) or (AConfigItem > Ord(High(EValveNames))) then Exit;
Result := CValveNames[EValveNames(AConfigItem)];
end; //FromEnumToString
function TMQTTValve.FromStringToEnum(AName: string): EAllNames;
Var
m : EAllNames;
begin
Result := eanNone;
for m := vnConfig to vnValueTemplate do begin
if AName = CValveNames[m] then begin
Result := m;
Break;
end;
end; //for
end; //FromStringToEnum
function TMQTTValve.Subscribe(ATopic: EAllNames; const SubId: integer): Boolean;
Var
CompleteTopic : string;
begin
Result := False;
if not Assigned(FParent) then Exit; // **********
CompleteTopic := TopicPrefix(ATopic) + Config[CValveNames[ATopic]];
Result := FParent.Subscribe(CompleteTopic, SubId);
end; //Subscribe
function TMQTTValve.JSONState: string;
begin //{"state": "opening", "position": 10}
Result := Format('{"state":"%s", "position": %d}',
[
FConfig[CValveNames[EValveStatesNames[State]]],
Position
]);
end;
Initialization
EValveStatesNames[evsClosed] := vnStateClosed;
EValveStatesNames[evsClosing] := vnStateClosing;
EValveStatesNames[evsOpen] := vnStateOpen;
EValveStatesNames[evsOpening] := vnStateOpening;
EValveStatesNames[evsStopped] := vnStateStopped;
HATypeInfo[vnAvailabilityTemplate ] := hatTemplate;
HATypeInfo[vnCommandTemplate ] := hatTemplate;
HATypeInfo[vnEnabledByDefault ] := hatBoolean;
HATypeInfo[vnJsonAttributesTemplate ] := hatTemplate;
HATypeInfo[vnOptimistic ] := hatBoolean;
HATypeInfo[vnPositionClosed ] := hatInteger;
HATypeInfo[vnPositionOpen ] := hatInteger;
HATypeInfo[vnQos ] := hatInteger;
HATypeInfo[vnReportsPosition ] := hatBoolean;
HATypeInfo[vnRetain ] := hatBoolean;
HATypeInfo[vnValueTemplate ] := hatTemplate;
end.