96 lines
3.8 KiB
C++
96 lines
3.8 KiB
C++
#pragma once
|
|
|
|
#define MAIN_DEVICE_ID "rc-gateway"
|
|
#define BATTERY_PERCENTAGE_TEMPLATE(min, diff) "{% if value_json.sensor.diagnostic.voltage|is_number %}\
|
|
{{ ((value_json.sensor.diagnostic.voltage|float-" #min ")|round(2)*100/" #diff ")|int }}\
|
|
{% endif %}"
|
|
|
|
#include "esp.h"
|
|
#include "ha.h"
|
|
|
|
using namespace Ha;
|
|
|
|
typedef unordered_multimap<uint32_t, Ha::Switch*> mapswitches;
|
|
|
|
mapswitches onSwitches;
|
|
mapswitches offSwitches;
|
|
unordered_map<string, Ha::Switch*> p1Switches;
|
|
|
|
auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266");
|
|
|
|
struct PollinSwitch : Switch {
|
|
constexpr static const char* man = "Pollin";
|
|
const char* group;
|
|
uint8_t channel;
|
|
|
|
PollinSwitch(const char* group, const uint8_t channel, const char* name = nullptr, const char* area = nullptr)
|
|
: Switch(nullptr, [group, channel]{
|
|
// copy id from string into a new pointer, to avoid memory leaks
|
|
return (new string{Protocol_1::buildId(group, channel)})->c_str();
|
|
}()), group(group), channel(channel) { // cppcheck-suppress [selfInitialization]
|
|
if (!name) name = (new string{string(man).append(" ").append(id)})->c_str();
|
|
entityId = [this]() {
|
|
auto eId = string{man};
|
|
transform(eId.begin(), eId.end(), eId.begin(), ::tolower);
|
|
return (new string{eId.append("_").append(id)})->c_str();
|
|
}();
|
|
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer(man).withArea(area).withParent(gatewayDevice);
|
|
withStateTopic();
|
|
deviceClass = "outlet";
|
|
p1Switches.insert({ string(id), this });
|
|
}
|
|
|
|
void onCommand(const char* msg) override {
|
|
strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel);
|
|
publisher(State::topic, msg);
|
|
}
|
|
|
|
};
|
|
|
|
struct EasyHomeSwitch : Switch {
|
|
constexpr static const array<uint32_t, 4> onAll = { 4326554, 4537114, 4767530, 4972714 };
|
|
constexpr static const array<uint32_t, 4> offAll = { 4483146, 4626810, 4661562, 4819642 };
|
|
array<uint32_t, 4> onButton;
|
|
array<uint32_t, 4> offButton;
|
|
|
|
EasyHomeSwitch(const char remotePosition, array<uint32_t, 4> on, array<uint32_t, 4> off, const char* name = nullptr, const char* area = nullptr)
|
|
: Switch(nullptr, [remotePosition] {
|
|
auto uId = new string("easy_home_");
|
|
(*uId) += tolower(remotePosition);
|
|
return uId->c_str();
|
|
}()), onButton(on), offButton(off) {
|
|
if (!name) {
|
|
auto n = new string("Easy Home ");
|
|
(*n) += remotePosition;
|
|
name = n->c_str();
|
|
}
|
|
entityId = id;
|
|
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
|
|
withStateTopic();
|
|
deviceClass = "outlet";
|
|
for (int i = 0; i < 4; i++) {
|
|
onSwitches.insert({ onAll[i], this });
|
|
onSwitches.insert({ onButton[i], this });
|
|
offSwitches.insert({ offAll[i], this });
|
|
offSwitches.insert({ offButton[i], this });
|
|
}
|
|
}
|
|
|
|
void onCommand(const char* msg) override {
|
|
mySwitch.setProtocol(4);
|
|
strcmp("ON", msg) == 0 ? mySwitch.send(onButton[0], 24) : mySwitch.send(offButton[0], 24);
|
|
publisher(State::topic, msg);
|
|
}
|
|
};
|
|
|
|
template <class T>
|
|
auto batterySensors(const char* id, const char* batterySensorTemplate) {
|
|
return [id, batterySensorTemplate](Builder<T>& builder) -> Builder<T>& {
|
|
builder
|
|
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
|
.addDiagnostic(new BatterySensor{id, "Battery level", batterySensorTemplate})
|
|
.build();
|
|
return builder;
|
|
};
|
|
}
|