152 lines
6.0 KiB
C++
152 lines
6.0 KiB
C++
#pragma once
|
|
|
|
#define MAIN_DEVICE_ID "rc-gateway"
|
|
|
|
#include "esp.h"
|
|
#include "ha.h"
|
|
|
|
using namespace Ha;
|
|
|
|
typedef unordered_multimap<unsigned long, 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");
|
|
|
|
namespace OilTank {
|
|
Sensor* buildRoomSensor(const char* id) {
|
|
DeviceConfig* device = &DeviceConfig::create(id)
|
|
.withName("Servers room")
|
|
.withManufacturer("Atmel")
|
|
.withModel("AtTiny85")
|
|
.withArea("Basement")
|
|
.withParent(gatewayDevice);
|
|
return Builder<TemperatureSensor>::instance(id)
|
|
.asDevice(device)
|
|
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
|
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
|
.addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-2.5)|round(2)*100/2)|int }}"})
|
|
.build();
|
|
}
|
|
|
|
Sensor* buildTankSensor(const char* id) {
|
|
DeviceConfig* device = &DeviceConfig::create(id)
|
|
.withName("Oil tank")
|
|
.withManufacturer("Arduino")
|
|
.withModel("Pro Mini")
|
|
.withArea("Basement")
|
|
.withParent(gatewayDevice);
|
|
return Builder<Sensor>::instance(new Sensor{ "Depth", id })
|
|
.asDevice(device)
|
|
.withDeviceClass("distance")
|
|
.withUnitMeasure("cm")
|
|
.withValueTemplate("{{ value_json.sensor.value }}")
|
|
.addSecondary(
|
|
Builder<Sensor>::instance(new Sensor{ "Level", id })
|
|
.withUnitMeasure("%")
|
|
.withValueTemplate("{{ 100 - ((value_json.sensor.value-12)|float*100/120)|round(2) }}")
|
|
.build()
|
|
)
|
|
.addDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
|
.addDiagnostic(new BatterySensor{id, "Battery level", "{{ ((value_json.sensor.diagnostic.voltage|float-4.8)|round(2)*100/1.6)|int }}"})
|
|
.build();
|
|
}
|
|
}
|
|
|
|
struct PollinSwitch : Switch {
|
|
constexpr static const char* man = "Pollin";
|
|
const char* group;
|
|
unsigned char channel;
|
|
|
|
PollinSwitch(const char* group, const unsigned char 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) {
|
|
if (!name) name = (new string{string(man).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(stateTopic, msg);
|
|
}
|
|
|
|
};
|
|
|
|
struct EasyHomeSwitch : Switch {
|
|
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 };
|
|
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 };
|
|
|
|
EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr)
|
|
: Switch(nullptr, [remotePosition] {
|
|
auto uId = new string("easy_home_");
|
|
(*uId) += tolower(remotePosition);
|
|
return uId->c_str();
|
|
}()) {
|
|
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
|
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
|
|
if (!name) {
|
|
auto n = new string("Easy Home ");
|
|
(*n) += remotePosition;
|
|
name = n->c_str();
|
|
}
|
|
mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice);
|
|
withStateTopic();
|
|
deviceClass = "outlet";
|
|
for (int i = 0; i < 8; i++) {
|
|
onSwitches.insert({ this->on[i], this });
|
|
offSwitches.insert({ this->off[i], this });
|
|
}
|
|
}
|
|
|
|
void onCommand(const char* msg) override {
|
|
mySwitch.setProtocol(4);
|
|
strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24);
|
|
publisher(stateTopic, msg);
|
|
}
|
|
};
|
|
|
|
Command* commands[] = {
|
|
Builder<Button>::instance(new Button{"Restart", "restart",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) ESP.restart();
|
|
}
|
|
}).asDevice(gatewayDevice)
|
|
.addPreconfigured(HaESP::heapStats)
|
|
.addPreconfigured(HaESP::restartInfo)
|
|
.build(),
|
|
#if ENABLE_DOORBELL
|
|
Builder<Button>::instance(new Button{"Front door", "doorbell_front",
|
|
[](const char* msg) {
|
|
if (strcmp("PRESS", msg) == 0) doorbell.ring("00000000110100101000100");
|
|
}
|
|
})
|
|
.asDevice(
|
|
&DeviceConfig::create("doorbell")
|
|
.withName("Doorbell")
|
|
.withManufacturer("Thomson")
|
|
.withModel("Kinetic Halo")
|
|
.withParent(gatewayDevice)
|
|
)
|
|
.build(),
|
|
#endif
|
|
new EasyHomeSwitch{'A', (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "FritzBox", "Basement"},
|
|
new EasyHomeSwitch{'B', (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }},
|
|
new PollinSwitch{"00001", 1, "Meeting sensor", "Dining room"},
|
|
new PollinSwitch{"00001", 2, "Fire Tv", "Living room"},
|
|
new PollinSwitch{"00001", 3, "Diningroom player", "Dining room"},
|
|
new PollinSwitch{"00001", 4},
|
|
new PollinSwitch{"11111", 4, "Train", "Playroom"}
|
|
};
|
|
|
|
Sensor* sensors[] = {
|
|
OilTank::buildRoomSensor("4"),
|
|
OilTank::buildTankSensor("7")
|
|
};
|