- use namespace for oil tank builders - use factory method for creating DeviceConfig - use C's strcmp for comparing strings - split parsing of incoming RCSwitch states
97 lines
4.0 KiB
C++
97 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#define MAIN_DEVICE_ID "rc-gateway"
|
|
|
|
#include "ha.h"
|
|
|
|
using namespace Ha;
|
|
|
|
DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID})->withName("RC Gateway")->withManufacturer("Adafruit")->withModel("Huzzah Esp8266");
|
|
|
|
namespace OilTank {
|
|
Sensor* buildRoomSensor(const char* id) {
|
|
auto device = DeviceConfig::create(id)
|
|
->withName("Oil tank room")
|
|
->withManufacturer("Atmel")
|
|
->withModel("AtTiny85")
|
|
->withArea("Basement")
|
|
->withParent(gatewayDevice);
|
|
return Builder<TemperatureSensor>::instance(id)
|
|
.asDevice(device)
|
|
.withValueTemplate("{{ value_json.sensor.temperature }}")
|
|
.withDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
|
.withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"})
|
|
.build();
|
|
}
|
|
|
|
Sensor* buildTankSensor(const char* id) {
|
|
auto 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")
|
|
.withUnitMseasure("cm")
|
|
.withValueTemplate("{{ value_json.sensor.value }}")
|
|
.withSecondary(
|
|
Builder<Sensor>::instance(new Sensor{ "Level", id })
|
|
.withUnitMseasure("%")
|
|
.withValueTemplate("{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}")
|
|
.build()
|
|
)
|
|
.withDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})
|
|
.withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})
|
|
.build();
|
|
}
|
|
}
|
|
|
|
struct PollinSwitch : Switch {
|
|
const char* group;
|
|
unsigned char channel;
|
|
|
|
PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr)
|
|
: Switch(name, [group, channel]{
|
|
// copy id from string into a new pointer, to avoid memory leaks
|
|
string s = Protocol_1::buildId(group, channel);
|
|
char* uId = new char[s.length() + 1];
|
|
strcpy(uId, s.c_str());
|
|
return uId;
|
|
}()), group(group), channel(channel) {
|
|
mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice);
|
|
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* name, const char* id, unsigned long on[4], unsigned long off[4], const char* area = nullptr)
|
|
: Switch(name, id) {
|
|
memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
|
|
memcpy(&this->off[4], off, 4 * sizeof(unsigned long));
|
|
mainDevice = (new DeviceConfig{id})->withName(name)->withManufacturer("Intertek")->withModel("Easy Home")->withArea(area)->withParent(gatewayDevice);
|
|
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);
|
|
}
|
|
};
|