move area completely under DeviceConfig

This commit is contained in:
Nicu Hodos 2024-05-09 13:46:22 +02:00
parent 965e158b6a
commit 333fc93a9f
3 changed files with 32 additions and 36 deletions

View File

@ -6,13 +6,16 @@
using namespace Ha; 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); DeviceConfig* atTinyDevice = (new DeviceConfig{})->withManufacturer("Atmel")->withModel("AtTiny85")->withParent(gatewayDevice);
struct SensorBuilder { struct OilTankRoomSensorBuilder {
static Sensor* withVoltage(Sensor* sensor) { static Sensor* build(const char* id) {
(new VoltageSensor{sensor->id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); auto device = (new DeviceConfig{*atTinyDevice})->withName("Oil tank room")->withArea("Basement");
(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}); 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; return sensor;
} }
}; };
@ -42,10 +45,12 @@ struct OilTankLevelSensor : Sensor {
struct OilSensorBuilder { struct OilSensorBuilder {
static Sensor* build(const char* id) { static Sensor* build(const char* id) {
Sensor* sensor = (new OilTankSensor("7"))->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank")->withArea("Basement"); auto device = (new DeviceConfig{*atTinyDevice})->withName("Oil tank")->withArea("Basement");
(new OilTankLevelSensor(sensor->id))->asDevice(new DeviceConfig{sensor->mainDevice->id}); device->id = id;
(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"})->asDevice(new DeviceConfig{sensor->mainDevice->id}); Sensor* sensor = (new OilTankSensor(id))->asDevice(device);
(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}); (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; return sensor;
} }
}; };
@ -54,9 +59,9 @@ struct PollinSwitch : Switch {
const char* group; const char* group;
unsigned char channel; 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) { : 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 }); p1Switches.insert({ string(id), this });
} }
@ -71,11 +76,11 @@ struct EasyHomeSwitch : Switch {
unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 }; unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 };
unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 }; 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) { : Switch(name, id) {
memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); memcpy(&this->on[4], on, 4 * sizeof(unsigned long));
memcpy(&this->off[4], off, 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++) { for (int i = 0; i < 8; i++) {
onSwitches.insert({ this->on[i], this }); onSwitches.insert({ this->on[i], this });
offSwitches.insert({ this->off[i], this }); offSwitches.insert({ this->off[i], this });

View File

@ -19,7 +19,6 @@ namespace Ha {
DeviceConfig() {} DeviceConfig() {}
DeviceConfig(const char* id) : id(id) {} 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) {} 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) { void buildConfig(JsonDocument& jsonDoc) {
@ -33,6 +32,11 @@ namespace Ha {
identifiers.add(id); identifiers.add(id);
} }
DeviceConfig* withName(const char* value) {
name = value;
return this;
}
DeviceConfig* withModel(const char* value) { DeviceConfig* withModel(const char* value) {
model = value; model = value;
return this; return this;
@ -96,21 +100,13 @@ namespace Ha {
EntityConfig(const char* name, const char* id, const char* type) : Component(name, id, type) { 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<T*>(this);
}
T* asDevice(DeviceConfig* deviceConfig) { T* asDevice(DeviceConfig* deviceConfig) {
mainDevice = deviceConfig; mainDevice = deviceConfig;
return static_cast<T*>(this); return static_cast<T*>(this);
} }
T* copyFromDevice(DeviceConfig* deviceConfig) { T* ofDevice(const char* id) {
mainDevice = new DeviceConfig{*deviceConfig}; mainDevice = new DeviceConfig{id};
mainDevice->id = id;
mainDevice->name = name;
mainDevice->parent = deviceConfig->parent;
return static_cast<T*>(this); return static_cast<T*>(this);
} }
}; };
@ -194,11 +190,6 @@ namespace Ha {
jsonDoc["state_topic"] = stateTopic; jsonDoc["state_topic"] = stateTopic;
jsonDoc["suggested_display_precision"] = 2; jsonDoc["suggested_display_precision"] = 2;
} }
Sensor* withDeviceName(const char* value) {
if (mainDevice) mainDevice->name = value;
return this;
}
}; };
struct TemperatureSensor : Sensor { struct TemperatureSensor : Sensor {

View File

@ -40,16 +40,16 @@ namespace Mqtt {
}; };
Ha::Switch* switches[] = { Ha::Switch* switches[] = {
(new PollinSwitch{"Meeting sensor", "00001", 1})->withArea("Dining room")->withStateTopic(), (new PollinSwitch{"Meeting sensor", "00001", 1, "Dining room"})->withStateTopic(),
(new PollinSwitch{"Fire Tv", "00001", 2})->withArea("Living room")->withStateTopic(), (new PollinSwitch{"Fire Tv", "00001", 2, "Living room"})->withStateTopic(),
(new PollinSwitch{"Diningroom player", "00001", 3})->withArea("Dining room")->withStateTopic(), (new PollinSwitch{"Diningroom player", "00001", 3, "Dining room"})->withStateTopic(),
(new PollinSwitch{"Train", "11111", 4})->withArea("Playroom")->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 }})->withArea("Basement")->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 }})->withArea("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[] = { Ha::Sensor* sensors[] = {
SensorBuilder::withVoltage((new Ha::TemperatureSensor{"4"})->copyFromDevice(atTinyDevice)->withDeviceName("Oil tank room")->withArea("Basement")), OilTankRoomSensorBuilder::build("4"),
OilSensorBuilder::build("7") OilSensorBuilder::build("7")
}; };