начинает создавать библиотеку HA
This commit is contained in:
23
lib/HomeAssistantDevices/HAControllableDevice.cpp
Normal file
23
lib/HomeAssistantDevices/HAControllableDevice.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <HAControllableDevice.hpp>
|
||||
|
||||
HAControllableDevice::HAControllableDevice(PubSubClient &client,
|
||||
const char *device_type,
|
||||
const char *name,
|
||||
const char *unique_id)
|
||||
: HADevice(client, device_type, name, unique_id) {
|
||||
setupCommandTopic();
|
||||
}
|
||||
|
||||
void HAControllableDevice::setupCommandTopic() {
|
||||
command_topic_size = strlen(device_topic) + 4 + 1;
|
||||
command_topic = (char *)malloc(command_topic_size);
|
||||
|
||||
strcpy(command_topic, device_topic);
|
||||
strcat(command_topic, "/cmd");
|
||||
}
|
||||
|
||||
void HAControllableDevice::subscribeToCommandTopic() {
|
||||
Serial.print("Subscribe to ");
|
||||
Serial.println(command_topic);
|
||||
client.subscribe(command_topic);
|
||||
};
|
18
lib/HomeAssistantDevices/HAControllableDevice.hpp
Normal file
18
lib/HomeAssistantDevices/HAControllableDevice.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <HADevice.hpp>
|
||||
|
||||
class HAControllableDevice : public HADevice {
|
||||
protected:
|
||||
char *command_topic;
|
||||
unsigned int command_topic_size;
|
||||
|
||||
void setupCommandTopic();
|
||||
|
||||
public:
|
||||
HAControllableDevice(PubSubClient &client, const char *device_type,
|
||||
const char *name, const char *unique_id);
|
||||
|
||||
void subscribeToCommandTopic();
|
||||
virtual void handle(char *topic, byte *payload, unsigned int length) = 0;
|
||||
};
|
40
lib/HomeAssistantDevices/HADevice.cpp
Normal file
40
lib/HomeAssistantDevices/HADevice.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <HADevice.hpp>
|
||||
|
||||
HADevice::HADevice(PubSubClient &client, const char *device_type,
|
||||
const char *name, const char *unique_id)
|
||||
: client(client), device_type(device_type), name(name),
|
||||
unique_id(unique_id) {
|
||||
buffer = (char *)malloc(MQTT_MAX_PACKET_SIZE);
|
||||
|
||||
setupDeviceTopic();
|
||||
setupConfigTopic();
|
||||
setupStateTopic();
|
||||
}
|
||||
|
||||
void HADevice::setupDeviceTopic() {
|
||||
device_topic_size =
|
||||
strlen(base_topic) + 1 + strlen(device_type) + 1 + strlen(unique_id) + 1;
|
||||
device_topic = (char *)malloc(device_topic_size);
|
||||
|
||||
strcpy(device_topic, base_topic);
|
||||
strcat(device_topic, "/");
|
||||
strcat(device_topic, device_type);
|
||||
strcat(device_topic, "/");
|
||||
strcat(device_topic, unique_id);
|
||||
}
|
||||
|
||||
void HADevice::setupConfigTopic() {
|
||||
config_topic_size = strlen(device_topic) + 7 + 1;
|
||||
config_topic = (char *)malloc(config_topic_size);
|
||||
|
||||
strcpy(config_topic, device_topic);
|
||||
strcat(config_topic, "/config");
|
||||
}
|
||||
|
||||
void HADevice::setupStateTopic() {
|
||||
state_topic_size = strlen(device_topic) + 6 + 1;
|
||||
state_topic = (char *)malloc(state_topic_size);
|
||||
|
||||
strcpy(state_topic, device_topic);
|
||||
strcat(state_topic, "/state");
|
||||
}
|
36
lib/HomeAssistantDevices/HADevice.hpp
Normal file
36
lib/HomeAssistantDevices/HADevice.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
class HADevice {
|
||||
protected:
|
||||
PubSubClient &client;
|
||||
const char *name;
|
||||
const char *unique_id;
|
||||
const char *device_type;
|
||||
|
||||
static constexpr const char *base_topic = "homeassistant";
|
||||
|
||||
char *device_topic = nullptr;
|
||||
unsigned int device_topic_size = 0;
|
||||
|
||||
char *config_topic = nullptr;
|
||||
unsigned int config_topic_size = 0;
|
||||
|
||||
char *state_topic = nullptr;
|
||||
unsigned int state_topic_size = 0;
|
||||
|
||||
char *buffer;
|
||||
unsigned int buffer_size = MQTT_MAX_PACKET_SIZE;
|
||||
|
||||
void setupDeviceTopic();
|
||||
void setupConfigTopic();
|
||||
void setupStateTopic();
|
||||
|
||||
public:
|
||||
HADevice(PubSubClient &client, const char *device_type, const char *name,
|
||||
const char *unique_id);
|
||||
virtual void sendConfig() = 0;
|
||||
virtual void sendState() = 0;
|
||||
};
|
55
lib/HomeAssistantDevices/HALight.cpp
Normal file
55
lib/HomeAssistantDevices/HALight.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <HALight.hpp>
|
||||
|
||||
HALight::HALight(PubSubClient &client, const char *name, const char *unique_id,
|
||||
HALightController &baseLight)
|
||||
: HAControllableDevice(client, "light", name, unique_id),
|
||||
light(&baseLight) {}
|
||||
|
||||
void HALight::sendState() {
|
||||
StaticJsonDocument<256> doc;
|
||||
|
||||
buffer[0] = '\0';
|
||||
|
||||
if (light->getState()) {
|
||||
doc["state"] = "ON";
|
||||
} else {
|
||||
doc["state"] = "OFF";
|
||||
}
|
||||
|
||||
serializeJson(doc, buffer, buffer_size);
|
||||
|
||||
Serial.println(state_topic);
|
||||
client.publish(state_topic, buffer, true);
|
||||
}
|
||||
|
||||
void HALight::sendConfig() {
|
||||
StaticJsonDocument<256> doc;
|
||||
|
||||
buffer[0] = '\0';
|
||||
|
||||
doc["~"] = device_topic;
|
||||
doc["name"] = name;
|
||||
doc["unique_id"] = unique_id;
|
||||
doc["cmd_t"] = "~/cmd";
|
||||
doc["stat_t"] = "~/state";
|
||||
doc["schema"] = "json";
|
||||
|
||||
serializeJson(doc, buffer, buffer_size);
|
||||
client.publish(config_topic, buffer, true);
|
||||
}
|
||||
|
||||
void HALight::handle(char *topic, byte *payload, unsigned int length) {
|
||||
if (strcmp(topic, command_topic) != 0)
|
||||
return;
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
deserializeJson(doc, (const byte *)payload, length);
|
||||
|
||||
if (strcmp(doc["state"], "ON") == 0) {
|
||||
light->setState(true);
|
||||
} else {
|
||||
light->setState(false);
|
||||
}
|
||||
|
||||
sendState();
|
||||
}
|
21
lib/HomeAssistantDevices/HALight.hpp
Normal file
21
lib/HomeAssistantDevices/HALight.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <HAControllableDevice.hpp>
|
||||
|
||||
class HALightController {
|
||||
public:
|
||||
virtual void setState(bool state) = 0;
|
||||
virtual bool getState() = 0;
|
||||
};
|
||||
|
||||
class HALight : public HAControllableDevice {
|
||||
private:
|
||||
HALightController *light;
|
||||
|
||||
public:
|
||||
HALight(PubSubClient &client, const char *name, const char *unique_id,
|
||||
HALightController &baseLight);
|
||||
void handle(char *topic, byte *payload, unsigned int length);
|
||||
void sendConfig();
|
||||
void sendState();
|
||||
};
|
3
lib/HomeAssistantDevices/HomeAssistantDevices.hpp
Normal file
3
lib/HomeAssistantDevices/HomeAssistantDevices.hpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include <HALight.hpp>
|
Reference in New Issue
Block a user