Refactoring HomeAssistantHelper

This commit is contained in:
Yurii
2023-11-11 04:59:39 +03:00
parent 7db49350a2
commit 5e8916b254
2 changed files with 270 additions and 257 deletions

View File

@@ -8,38 +8,48 @@ public:
{ {
} }
void setPrefix(String value) { void setDevicePrefix(String value) {
_prefix = value; devicePrefix = value;
} }
void setDeviceVersion(String value) { void setDeviceVersion(String value) {
_deviceVersion = value; deviceVersion = value;
} }
void setDeviceManufacturer(String value) { void setDeviceManufacturer(String value) {
_deviceManufacturer = value; deviceManufacturer = value;
} }
void setDeviceModel(String value) { void setDeviceModel(String value) {
_deviceModel = value; deviceModel = value;
} }
void setDeviceName(String value) { void setDeviceName(String value) {
_deviceName = value; deviceName = value;
} }
void setDeviceConfigUrl(String value) { void setDeviceConfigUrl(String value) {
_deviceConfigUrl = value; deviceConfigUrl = value;
} }
bool publish(const char* topic, JsonDocument& doc) { bool publish(const char* topic, JsonDocument& doc) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_IDENTIFIERS)][0] = _prefix; doc[FPSTR(HA_DEVICE)][FPSTR(HA_IDENTIFIERS)][0] = devicePrefix;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_SW_VERSION)] = _deviceVersion; doc[FPSTR(HA_DEVICE)][FPSTR(HA_SW_VERSION)] = deviceVersion;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MANUFACTURER)] = _deviceManufacturer;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MODEL)] = _deviceModel; if (deviceManufacturer) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_NAME)] = _deviceName; doc[FPSTR(HA_DEVICE)][FPSTR(HA_MANUFACTURER)] = deviceManufacturer;
if (_deviceConfigUrl) { }
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = _deviceConfigUrl;
if (deviceModel) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MODEL)] = deviceModel;
}
if (deviceName) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_NAME)] = deviceName;
}
if (deviceConfigUrl) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = deviceConfigUrl;
} }
client->beginPublish(topic, measureJson(doc), true); client->beginPublish(topic, measureJson(doc), true);
@@ -52,10 +62,12 @@ public:
} }
String getTopic(const char* category, const char* name, const char* nameSeparator = "/") { String getTopic(const char* category, const char* name, const char* nameSeparator = "/") {
String topic = "homeassistant/"; String topic = "";
topic.concat(prefix);
topic.concat("/");
topic.concat(category); topic.concat(category);
topic.concat("/"); topic.concat("/");
topic.concat(_prefix); topic.concat(devicePrefix);
topic.concat(nameSeparator); topic.concat(nameSeparator);
topic.concat(name); topic.concat(name);
topic.concat("/config"); topic.concat("/config");
@@ -64,10 +76,11 @@ public:
protected: protected:
PubSubClient* client; PubSubClient* client;
String _prefix = ""; String prefix = "homeassistant";
String _deviceVersion = "1.0"; String devicePrefix = "";
String _deviceManufacturer = "Community"; String deviceVersion = "1.0";
String _deviceModel = ""; String deviceManufacturer = "Community";
String _deviceName = ""; String deviceModel = "";
String _deviceConfigUrl = ""; String deviceName = "";
String deviceConfigUrl = "";
}; };

File diff suppressed because it is too large Load Diff