forked from horchi/linux-p4d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhass.c
266 lines (203 loc) · 7.56 KB
/
hass.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//***************************************************************************
// p4d / Linux - Heizungs Manager
// File hass.c
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 04.11.2010 - 25.04.2020 Jörg Wendel
//***************************************************************************
#include "lib/json.h"
#include "p4d.h"
//***************************************************************************
// Push Value to Home Assistant
//***************************************************************************
int P4d::mqttPublishSensor(const char* name, const char* title, const char* unit,
double value, const char* text, bool forceConfig)
{
// check/prepare connection
if (mqttCheckConnection() != success)
return fail;
int status {success};
MemoryStruct message;
std::string tp;
std::string sName = name;
sName = strReplace("ß", "ss", sName);
sName = strReplace("ü", "ue", sName);
sName = strReplace("ö", "oe", sName);
sName = strReplace("ä", "ae", sName);
sName = strReplace(" ", "_", sName);
// mqttDataTopic like "p4d2mqtt/sensor/<NAME>/state"
std::string sDataTopic = mqttDataTopic;
sDataTopic = strReplace("<NAME>", sName, sDataTopic);
if (mqttHaveConfigTopic)
{
// Interface description:
// https://www.home-assistant.io/docs/mqtt/discovery/
mqttReader->subscribe(sDataTopic.c_str());
status = mqttReader->read(&message, 100);
tp = mqttReader->getLastReadTopic();
if (status != success && status != Mqtt::wrnTimeout)
return fail;
if (forceConfig || status == Mqtt::wrnTimeout)
{
char* configTopic {0};
char* configJson {0};
// topic don't exists -> create sensor
if (strcmp(unit, "°") == 0)
unit = "°C";
else if (strcmp(unit, "Heizungsstatus") == 0 ||
strcmp(unit, "zst") == 0 ||
strcmp(unit, "T") == 0)
unit = "";
tell(1, "Info: Sensor '%s' not found at home assistants MQTT, sendig config message", sName.c_str());
asprintf(&configTopic, "homeassistant/sensor/%s/config", sName.c_str());
asprintf(&configJson, "{"
"\"unit_of_measurement\" : \"%s\","
"\"value_template\" : \"{{ value_json.value }}\","
"\"state_topic\" : \"p4d2mqtt/sensor/%s/state\","
"\"name\" : \"%s\","
"\"unique_id\" : \"%s_p4d2mqtt\""
"}",
unit, sName.c_str(), title, sName.c_str());
mqttWriter->writeRetained(configTopic, configJson);
free(configTopic);
free(configJson);
}
mqttReader->unsubscribe(sDataTopic.c_str());
}
// publish actual value
json_t* oValue = json_object();
if (!isEmpty(text))
json_object_set_new(oValue, "value", json_string(text));
else
json_object_set_new(oValue, "value", json_real(value));
char* j = json_dumps(oValue, JSON_PRESERVE_ORDER); // |JSON_REAL_PRECISION(5));
mqttWriter->writeRetained(sDataTopic.c_str(), j);
json_decref(oValue);
return success;
}
//***************************************************************************
// Json Add Value
//***************************************************************************
int P4d::jsonAddValue(json_t* obj, const char* name, const char* title, const char* unit,
double theValue, uint groupid, const char* text, bool forceConfig)
{
std::string sName = name;
bool newGroup {false};
json_t* oGroup {nullptr};
json_t* oSensor = json_object();
if (mqttInterfaceStyle == misSingleTopic)
{
oGroup = json_object_get(obj, groups[groupid].name.c_str());
if (!oGroup)
{
oGroup = json_object();
newGroup = true;
}
}
sName = strReplace("ß", "ss", sName);
sName = strReplace("ü", "ue", sName);
sName = strReplace("ö", "oe", sName);
sName = strReplace("ä", "ae", sName);
if (!isEmpty(text))
json_object_set_new(oSensor, "value", json_string(text));
else
json_object_set_new(oSensor, "value", json_real(theValue));
if (strcmp(unit, "°") == 0)
unit = "°C";
// create json
if (forceConfig)
{
json_object_set_new(oSensor, "unit", json_string(unit));
json_object_set_new(oSensor, "description", json_string(title));
}
if (oGroup)
{
json_object_set_new(oGroup, sName.c_str(), oSensor);
if (newGroup)
json_object_set_new(obj, groups[groupid].name.c_str(), oGroup);
}
else
{
json_object_set_new(obj, sName.c_str(), oSensor);
}
return success;
}
//***************************************************************************
// MQTT Write
//***************************************************************************
int P4d::mqttWrite(json_t* obj, uint groupid)
{
std::string sDataTopic = mqttDataTopic;
// check/prepare connection
if (mqttCheckConnection() != success)
return fail;
char* message = json_dumps(obj, JSON_REAL_PRECISION(4));
tell(3, "Debug: JSON: [%s]", message);
if (mqttInterfaceStyle == misGroupedTopic)
sDataTopic = strReplace("<GROUP>", groups[groupid].name, sDataTopic);
return mqttWriter->write(sDataTopic.c_str(), message);
}
//***************************************************************************
// Perform MQTT Requests
// - check 'mqttCommandReader' for commands of e.g. a home automation
//***************************************************************************
int P4d::performMqttRequests()
{
if (mqttCheckConnection() != success)
return fail;
MemoryStruct message;
std::string topic;
if (mqttCommandReader->read(&message, 10) == success)
{
topic = mqttCommandReader->getLastReadTopic();
tell(eloAlways, "<- (%s) [%s]", topic.c_str(), message.memory);
dispatchMqttCommandRequest(message.memory);
}
return success;
}
//***************************************************************************
// Check MQTT Connection
//***************************************************************************
int P4d::mqttCheckConnection()
{
if (!mqttWriter)
mqttWriter = new Mqtt();
if (!mqttReader)
mqttReader = new Mqtt();
if (!mqttCommandReader)
mqttCommandReader = new Mqtt();
if (mqttCommandReader->isConnected() && mqttWriter->isConnected() && mqttReader->isConnected())
return success;
if (!mqttWriter->isConnected())
{
if (mqttWriter->connect(mqttUrl, mqttUser, mqttPassword) != success)
{
tell(0, "Error: MQTT: Connecting publisher to '%s' failed", mqttUrl);
return fail;
}
tell(0, "MQTT: Connecting publisher to '%s' succeeded", mqttUrl);
}
if (!mqttCommandReader->isConnected())
{
if (mqttCommandReader->connect(mqttUrl) != success)
{
tell(0, "Error: MQTT: Connecting subscriber to '%s' failed", mqttUrl);
return fail;
}
mqttCommandReader->subscribe("mqtt2p4d/command");
tell(0, "MQTT: Connecting command subscriber to '%s' succeeded", mqttUrl);
}
if (mqttHaveConfigTopic)
{
if (!mqttReader->isConnected())
{
if (mqttReader->connect(mqttUrl, mqttUser, mqttPassword) != success)
{
tell(0, "Error: MQTT: Connecting subscriber to '%s' failed", mqttUrl);
return fail;
}
tell(0, "MQTT: Connecting subscriber to '%s' succeeded", mqttUrl);
}
}
return success;
}