forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented first version of Home Assistant Auto Discovery
- Loading branch information
Showing
13 changed files
with
360 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include "Configuration.h" | ||
#include <Arduino.h> | ||
#include <Hoymiles.h> | ||
#include <memory> | ||
|
||
// mqtt discovery device classes | ||
enum { | ||
DEVICE_CLS_NONE = 0, | ||
DEVICE_CLS_CURRENT, | ||
DEVICE_CLS_ENERGY, | ||
DEVICE_CLS_PWR, | ||
DEVICE_CLS_VOLTAGE, | ||
DEVICE_CLS_FREQ, | ||
DEVICE_CLS_TEMP, | ||
DEVICE_CLS_POWER_FACTOR | ||
}; | ||
const char* const deviceClasses[] = { 0, "current", "energy", "power", "voltage", "frequency", "temperature", "power_factor" }; | ||
enum { | ||
STATE_CLS_NONE = 0, | ||
STATE_CLS_MEASUREMENT, | ||
STATE_CLS_TOTAL_INCREASING | ||
}; | ||
const char* const stateClasses[] = { 0, "measurement", "total_increasing" }; | ||
|
||
typedef struct { | ||
uint8_t fieldId; // field id | ||
uint8_t deviceClsId; // device class | ||
uint8_t stateClsId; // state class | ||
} byteAssign_fieldDeviceClass_t; | ||
|
||
const byteAssign_fieldDeviceClass_t deviceFieldAssignment[] = { | ||
{ FLD_UDC, DEVICE_CLS_VOLTAGE, STATE_CLS_MEASUREMENT }, | ||
{ FLD_IDC, DEVICE_CLS_CURRENT, STATE_CLS_MEASUREMENT }, | ||
{ FLD_PDC, DEVICE_CLS_PWR, STATE_CLS_MEASUREMENT }, | ||
{ FLD_YD, DEVICE_CLS_ENERGY, STATE_CLS_TOTAL_INCREASING }, | ||
{ FLD_YT, DEVICE_CLS_ENERGY, STATE_CLS_TOTAL_INCREASING }, | ||
{ FLD_UAC, DEVICE_CLS_VOLTAGE, STATE_CLS_MEASUREMENT }, | ||
{ FLD_IAC, DEVICE_CLS_CURRENT, STATE_CLS_MEASUREMENT }, | ||
{ FLD_PAC, DEVICE_CLS_PWR, STATE_CLS_MEASUREMENT }, | ||
{ FLD_F, DEVICE_CLS_FREQ, STATE_CLS_MEASUREMENT }, | ||
{ FLD_T, DEVICE_CLS_TEMP, STATE_CLS_MEASUREMENT }, | ||
{ FLD_PCT, DEVICE_CLS_POWER_FACTOR, STATE_CLS_MEASUREMENT }, | ||
{ FLD_EFF, DEVICE_CLS_NONE, STATE_CLS_NONE }, | ||
{ FLD_IRR, DEVICE_CLS_NONE, STATE_CLS_NONE } | ||
}; | ||
#define DEVICE_CLS_ASSIGN_LIST_LEN (sizeof(deviceFieldAssignment) / sizeof(byteAssign_fieldDeviceClass_t)) | ||
|
||
class MqttHassPublishingClass { | ||
public: | ||
void init(); | ||
void loop(); | ||
void publishConfig(); | ||
|
||
private: | ||
void publishField(std::shared_ptr<InverterAbstract> inv, uint8_t channel, byteAssign_fieldDeviceClass_t fieldType, bool clear = false); | ||
|
||
bool _wasConnected = false; | ||
}; | ||
|
||
extern MqttHassPublishingClass MqttHassPublishing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2022 Thomas Basler and others | ||
*/ | ||
#include "MqttHassPublishing.h" | ||
#include "ArduinoJson.h" | ||
#include "MqttPublishing.h" | ||
#include "MqttSettings.h" | ||
#include "WiFiSettings.h" | ||
|
||
MqttHassPublishingClass MqttHassPublishing; | ||
|
||
void MqttHassPublishingClass::init() | ||
{ | ||
} | ||
|
||
void MqttHassPublishingClass::loop() | ||
{ | ||
if (MqttSettings.getConnected() && !_wasConnected) { | ||
// Connection established | ||
_wasConnected = true; | ||
publishConfig(); | ||
} else if (!MqttSettings.getConnected() && _wasConnected) { | ||
// Connection lost | ||
_wasConnected = false; | ||
} | ||
} | ||
|
||
void MqttHassPublishingClass::publishConfig() | ||
{ | ||
if (!Configuration.get().Mqtt_Hass_Enabled) { | ||
return; | ||
} | ||
|
||
if (!MqttSettings.getConnected() && Hoymiles.getRadio()->isIdle()) { | ||
return; | ||
} | ||
|
||
CONFIG_T& config = Configuration.get(); | ||
|
||
// Loop all inverters | ||
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { | ||
auto inv = Hoymiles.getInverterByPos(i); | ||
|
||
// Loop all channels | ||
for (uint8_t c = 0; c <= inv->Statistics()->getChannelCount(); c++) { | ||
for (uint8_t f = 0; f < DEVICE_CLS_ASSIGN_LIST_LEN; f++) { | ||
bool clear = false; | ||
if (c > 0 && !config.Mqtt_Hass_IndividualPanels) { | ||
clear = true; | ||
} | ||
publishField(inv, c, deviceFieldAssignment[f], clear); | ||
} | ||
} | ||
|
||
yield(); | ||
} | ||
} | ||
|
||
void MqttHassPublishingClass::publishField(std::shared_ptr<InverterAbstract> inv, uint8_t channel, byteAssign_fieldDeviceClass_t fieldType, bool clear) | ||
{ | ||
if (!inv->Statistics()->hasChannelFieldValue(channel, fieldType.fieldId)) { | ||
return; | ||
} | ||
|
||
char serial[sizeof(uint64_t) * 8 + 1]; | ||
sprintf(serial, "%0lx%08lx", | ||
((uint32_t)((inv->serial() >> 32) & 0xFFFFFFFF)), | ||
((uint32_t)(inv->serial() & 0xFFFFFFFF))); | ||
|
||
String fieldName; | ||
if (channel == CH0 && fieldType.fieldId == FLD_PDC) { | ||
fieldName = "PowerDC"; | ||
} else { | ||
fieldName = inv->Statistics()->getChannelFieldName(channel, fieldType.fieldId); | ||
} | ||
|
||
String configTopic = "sensor/dtu_" + String(serial) | ||
+ "/" + "ch" + String(channel) + "_" + fieldName | ||
+ "/config"; | ||
|
||
if (!clear) { | ||
String stateTopic = MqttSettings.getPrefix() + MqttPublishing.getTopic(inv, channel, fieldType.fieldId); | ||
const char* devCls = deviceClasses[fieldType.deviceClsId]; | ||
const char* stateCls = stateClasses[fieldType.stateClsId]; | ||
|
||
String name; | ||
if (channel == CH0) { | ||
name = String(inv->name()) + " " + fieldName; | ||
} else { | ||
name = String(inv->name()) + " CH" + String(channel) + " " + fieldName; | ||
} | ||
|
||
DynamicJsonDocument deviceDoc(512); | ||
deviceDoc[F("name")] = inv->name(); | ||
deviceDoc[F("ids")] = String(serial); | ||
deviceDoc[F("cu")] = String(F("http://")) + String(WiFi.localIP().toString()); | ||
deviceDoc[F("mf")] = F("OpenDTU"); | ||
deviceDoc[F("mdl")] = inv->typeName(); | ||
deviceDoc[F("sw")] = AUTO_GIT_HASH; | ||
JsonObject deviceObj = deviceDoc.as<JsonObject>(); | ||
|
||
DynamicJsonDocument root(1024); | ||
root[F("name")] = name; | ||
root[F("stat_t")] = stateTopic; | ||
root[F("unit_of_meas")] = inv->Statistics()->getChannelFieldUnit(channel, fieldType.fieldId); | ||
root[F("uniq_id")] = String(serial) + "_ch" + String(channel) + "_" + fieldName; | ||
root[F("dev")] = deviceObj; | ||
root[F("exp_aft")] = Configuration.get().Mqtt_PublishInterval * 2; | ||
if (devCls != 0) { | ||
root[F("dev_cla")] = devCls; | ||
} | ||
if (stateCls != 0) { | ||
root[F("stat_cla")] = stateCls; | ||
} | ||
|
||
char buffer[512]; | ||
serializeJson(root, buffer); | ||
MqttSettings.publishHass(configTopic, buffer); | ||
} | ||
else { | ||
MqttSettings.publishHass(configTopic, ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.