#pragma once #define MAIN_DEVICE_ID "rc-gateway" #include "ha.h" using namespace Ha; DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266"); DeviceConfig* atTinyDevice = (new DeviceConfig{})->withManufacturer("Atmel")->withModel("AtTiny85")->withParent(gatewayDevice); struct SensorBuilder { static Sensor* withVoltage(Sensor* sensor) { (new VoltageSensor{sensor->id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); (new BatterySensor{sensor->id, "Battery level", "{{ ((states('sensor.oil_tank_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); return sensor; } }; struct OilTankSensor : Sensor { OilTankSensor(const char* id) : Sensor("Depth", id) { deviceClass = "distance"; unitMeasure = "cm"; valueTemplate = "{{ value_json.sensor.value }}"; } }; struct OilTankLevelSensor : Sensor { OilTankLevelSensor(const char* id) : Sensor("Level", id) { unitMeasure = "%"; valueTemplate = "{{ 100 - ((value_json.sensor.value-7)|float*100/110)|round(2) }}"; } void buildUniqueId(char* uniqueId) override { sprintf(uniqueId, "level_%s", id); } void buildConfigTopic() override { sprintf(configTopic, "homeassistant/%s/%s/level_%s/config", type, MAIN_DEVICE_ID, id); } }; struct OilSensorBuilder { static Sensor* build(const char* id) { Sensor* sensor = (new OilTankSensor("7"))->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank")->withArea("Basement"); (new OilTankLevelSensor(sensor->id))->asDevice(new DeviceConfig{sensor->mainDevice->id}); (new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); (new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); return sensor; } }; struct PollinSwitch : Switch { const char* group; unsigned char channel; PollinSwitch(const char* name, const char* group, const unsigned char channel) : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) { asDevice((new DeviceConfig{id, name})->withManufacturer("Pollin")->withParent(gatewayDevice)); p1Switches.insert({ string(id), this }); } void onCommand(const char* msg) override { (String{ "ON" }.equals(msg)) ? 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]) : Switch(name, id) { memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); memcpy(&this->off[4], off, 4 * sizeof(unsigned long)); asDevice((new DeviceConfig{id, name})->withManufacturer("Intertek")->withModel("Easy Home")->withParent(gatewayDevice)); 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); String{ "ON" }.equals(msg) ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24); publisher(stateTopic, msg); } };