diff --git a/gateway/include/devices.h b/gateway/include/devices.h index d6cb24a..6ae8b46 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -6,13 +6,16 @@ using namespace Ha; -DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID, "RC Gateway"})->withManufacturer("Adafruit")->withModel("Huzzah Esp8266"); +DeviceConfig* gatewayDevice = (new DeviceConfig{MAIN_DEVICE_ID})->withName("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}); +struct OilTankRoomSensorBuilder { + static Sensor* build(const char* id) { + auto device = (new DeviceConfig{*atTinyDevice})->withName("Oil tank room")->withArea("Basement"); + device->id = id; + Sensor* sensor = (new Ha::TemperatureSensor(id))->asDevice(device); + (new VoltageSensor{sensor->id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->ofDevice((id)); + (new BatterySensor{sensor->id, "Battery level", "{{ ((states('sensor.oil_tank_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"})->ofDevice(id); return sensor; } }; @@ -42,10 +45,12 @@ struct OilTankLevelSensor : Sensor { 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}); + auto device = (new DeviceConfig{*atTinyDevice})->withName("Oil tank")->withArea("Basement"); + device->id = id; + Sensor* sensor = (new OilTankSensor(id))->asDevice(device); + (new OilTankLevelSensor(sensor->id))->ofDevice(id); + (new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->ofDevice(id); + (new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_battery_voltage')|float-3.6)|round(2)*100/1.6)|int }}"})->ofDevice(id); return sensor; } }; @@ -54,9 +59,9 @@ struct PollinSwitch : Switch { const char* group; unsigned char channel; - PollinSwitch(const char* name, const char* group, const unsigned char channel) + PollinSwitch(const char* name, const char* group, const unsigned char channel, const char* area = nullptr) : Switch(name, Protocol_1::buildId(group, channel)), group(group), channel(channel) { - asDevice((new DeviceConfig{id, name})->withManufacturer("Pollin")->withParent(gatewayDevice)); + asDevice((new DeviceConfig{id})->withName(name)->withManufacturer("Pollin")->withArea(area)->withParent(gatewayDevice)); p1Switches.insert({ string(id), this }); } @@ -71,11 +76,11 @@ 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]) + 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)); - asDevice((new DeviceConfig{id, name})->withManufacturer("Intertek")->withModel("Easy Home")->withParent(gatewayDevice)); + asDevice((new DeviceConfig{id})->withName(name)->withManufacturer("Intertek")->withModel("Easy Home")->withArea(area)->withParent(gatewayDevice)); for (int i = 0; i < 8; i++) { onSwitches.insert({ this->on[i], this }); offSwitches.insert({ this->off[i], this }); diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 7dca89a..570621c 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -19,7 +19,6 @@ namespace Ha { DeviceConfig() {} DeviceConfig(const char* id) : id(id) {} - DeviceConfig(const char* id, const char* name) : id(id), name(name) {} DeviceConfig(DeviceConfig& d) : id(d.id), name(d.name), model(d.model), manufacturer(d.manufacturer), area(d.area), parent(d.parent) {} void buildConfig(JsonDocument& jsonDoc) { @@ -33,6 +32,11 @@ namespace Ha { identifiers.add(id); } + DeviceConfig* withName(const char* value) { + name = value; + return this; + } + DeviceConfig* withModel(const char* value) { model = value; return this; @@ -96,21 +100,13 @@ namespace Ha { EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) { } - T* withArea(const char* value) { - if (mainDevice) mainDevice->withArea(value); - return static_cast(this); - } - T* asDevice(DeviceConfig* deviceConfig) { mainDevice = deviceConfig; return static_cast(this); } - T* copyFromDevice(DeviceConfig* deviceConfig) { - mainDevice = new DeviceConfig{*deviceConfig}; - mainDevice->id = id; - mainDevice->name = name; - mainDevice->parent = deviceConfig->parent; + T* ofDevice(const char* id) { + mainDevice = new DeviceConfig{id}; return static_cast(this); } }; @@ -194,11 +190,6 @@ namespace Ha { jsonDoc["state_topic"] = stateTopic; jsonDoc["suggested_display_precision"] = 2; } - - Sensor* withDeviceName(const char* value) { - if (mainDevice) mainDevice->name = value; - return this; - } }; struct TemperatureSensor : Sensor { diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 66c2187..96957cb 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -40,16 +40,16 @@ namespace Mqtt { }; Ha::Switch* switches[] = { - (new PollinSwitch{"Meeting sensor", "00001", 1})->withArea("Dining room")->withStateTopic(), - (new PollinSwitch{"Fire Tv", "00001", 2})->withArea("Living room")->withStateTopic(), - (new PollinSwitch{"Diningroom player", "00001", 3})->withArea("Dining room")->withStateTopic(), - (new PollinSwitch{"Train", "11111", 4})->withArea("Playroom")->withStateTopic(), - (new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }})->withArea("Basement")->withStateTopic(), - (new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }})->withArea("Basement")->withStateTopic() + (new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"})->withStateTopic(), + (new PollinSwitch{"Fire Tv", "00001", 2, "Living room"})->withStateTopic(), + (new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"})->withStateTopic(), + (new PollinSwitch{"Train", "11111", 4, "Playroom"})->withStateTopic(), + (new EasyHomeSwitch{"FritzBox", "easy_home_a", (unsigned long[4]) { 4483136, 4626800, 4661552, 4819632 }, (unsigned long[4]) { 4326544, 4537104, 4767520, 4972704 }, "Basement"})->withStateTopic(), + (new EasyHomeSwitch{"Outside", "easy_home_b", (unsigned long[4]) { 4483140, 4626804, 4661556, 4819636 }, (unsigned long[4]) { 4326548, 4537108, 4767524, 4972708 }, "Basement"})->withStateTopic() }; Ha::Sensor* sensors[] = { - SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")), + OilTankRoomSensorBuilder::build("4"), OilSensorBuilder::build("7") };