ADD: new deviceclass and toJSON Method

This commit is contained in:
2024-06-27 21:08:16 +01:00
parent a1e58a5d63
commit 30ba44f114

View File

@@ -1,43 +1,55 @@
export class MQTTEntity {
name: string = "";
stat_t: string = "state/topic";
uniq_id: string = "unique_id";
dev: MQTTDevice | null = null;
// entity_type: ENTITY_TYPE = 0;
name: string = '';
stat_t: string = '';
uniq_id: string = '';
// dev: MQTTDevice | null = null;
// entity_type: ENTITY_TYPE = 0;
setProperty(name: unknown, value: any): void {
if(!this.hasOwnProperty(String(name))) return
this[name as keyof this] = value;
}
setProperty(name: unknown, value: any): void {
if (!this.hasOwnProperty(String(name))) return;
this[name as keyof this] = value;
}
createString() : string{
let string: string = "";
for(let property of Object.getOwnPropertyNames(this)){
if(this[property as keyof this] == null) continue;
string += `"${property}": "${this[property as keyof this]}",`
}
console.log(string);
return string;
getProperty(name: string): any {
if (!this.hasOwnProperty(name)) return;
return this[name as keyof this];
}
toJSON(): any {
let jsonObject: { [key: string]: string } = {};
for (let prop_name of Object.getOwnPropertyNames(this)) {
let property = this[prop_name as keyof this];
if (property == '') continue;
jsonObject[prop_name] = String(property);
}
return jsonObject;
}
// toJSON(): any {
// return {
// name: this.name,
// stat_t: this.stat_t,
// uniq_id: this.uniq_id,
// };
// }
}
export class MQTTDevice {
name: string = "";
identifiers: string[] = ["MQTT"];
serial_number: string = "";
configuration_url: string = "";
name: string = '';
identifiers: string[] = ['MQTT'];
serial_number: string = '';
configuration_url: string = '';
}
// export enum ENTITY_TYPE {
// light = 0,
// switch = 1,
// sensor = 2,
// binary_sensor = 3,
// button = 4,
// }
export class DeviceClass {
value: number = 0;
choices: string[] = ['None'];
export enum DEVICE_CLASS {
motion = 0,
movement = 1,
outlet = 2
constructor(choices: string[]) {
this.choices = choices;
}
toString(): string {
return this.choices[this.value];
}
}