Merge into Main #1

Merged
tom_trgr merged 15 commits from dev/devices into main 2024-08-02 08:16:31 +02:00
5 changed files with 166 additions and 26 deletions
Showing only changes of commit bbc4dd6e53 - Show all commits

View File

@@ -1,6 +1,16 @@
import { DeviceClass, MQTTEntity } from './mqtt_base';
import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base';
export class MqttBinary extends MQTTEntity {
override ent_type: string = 'binary';
override attrs: Set<string> = new Set([
'name',
'uniq_id',
'stat_t',
'pl_on',
'pl_off',
'dev_cla',
]);
dev_cla: DeviceClass = new DeviceClass([
'battery',
'battery_charging',

View File

@@ -1,9 +1,48 @@
import { MQTTEntity } from './mqtt_base';
import { iMQTTEntityBase, MQTTEntity } from './mqtt_base';
export class MqttLight extends MQTTEntity {
cmd_t: string = 'command/topic';
bri_cmd_t: string = 'brightness/command/topic';
pl_on: string = '1';
pl_off: string = '0';
val_tpl: string = '';
bri_cmd_t: string = 'brightness/command/topic';
_cmd_t: string = 'command/topic';
override readonly ent_type: string = 'light';
override attrs: Set<string> = new Set([
'name',
'uniq_id',
'stat_t',
'pl_on',
'pl_off',
'val_tpl',
'bri_cmd_t',
'cmd_t',
]);
get cmd_t() {
return this._cmd_t;
}
set cmd_t(data: string) {
this._cmd_t = data;
}
override set name(data: string) {
super.name = data;
this.topic_updates.next('cmd_t');
this.topic_updates.next('bri_cmd_t');
}
override get name() {
return this._name;
}
override set uniq_id(data: string) {
super.uniq_id = data;
this.topic_updates.next('cmd_t');
this.topic_updates.next('bri_cmd_t');
}
override get uniq_id() {
return this._uniq_id;
}
}

View File

@@ -1,5 +1,16 @@
import { MQTTEntity } from './mqtt_base';
import { iMQTTEntityBase, MQTTEntity } from './mqtt_base';
export class MqttSensor extends MQTTEntity {
export class MqttSensor extends MQTTEntity implements iMqttSensor {
unit_of_meas: string = 'meassure';
override ent_type: string = 'sensor';
override attrs: Set<string> = new Set([
'name',
'uniq_id',
'stat_t',
'unit_of_meas',
]);
}
export interface iMqttSensor extends iMQTTEntityBase {
unit_of_meas: string;
}

View File

@@ -1,8 +1,43 @@
import { DeviceClass, MQTTEntity } from './mqtt_base';
import { EntityService } from '../_services/entity.service';
import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base';
export class MqttSwitch extends MQTTEntity {
override ent_type: string = 'switch';
override attrs: Set<string> = new Set([
'name',
'uniq_id',
'stat_t',
'dev_cla',
'cmd_t',
'pl_on',
'pl_off',
]);
dev_cla: DeviceClass = new DeviceClass(['switch', 'outlet']);
cmd_t: string = 'command/topic';
pl_on: string = '1';
pl_off: string = '0';
override set name(name: string) {
super.name = name;
this.topic_updates.next('cmd_t');
}
override set uniq_id(uniq_id: string) {
super.uniq_id = uniq_id;
this.topic_updates.next('cmd_t');
}
override get name() {
return this._name;
}
override get uniq_id() {
return this._uniq_id;
}
}
export interface iMqttSwitch extends iMQTTEntityBase {
cmd_t: string;
pl_on: string;
pl_off: string;
dev_cla: DeviceClass;
}

View File

@@ -1,18 +1,49 @@
export class MQTTEntity {
name: string = '';
stat_t: string = '';
uniq_id: string = '';
// dev: MQTTDevice | null = null;
// entity_type: ENTITY_TYPE = 0;
import { EventEmitter, Injectable } from '@angular/core';
import { EntityService } from '../_services/entity.service';
setProperty(name: unknown, value: any): void {
if (!this.hasOwnProperty(String(name))) return;
this[name as keyof this] = value;
@Injectable()
export class MQTTEntity implements iMQTTEntityBase {
protected _name: string = '';
protected _stat_t: string = '';
protected _uniq_id: string = '';
attrs = new Set(['name', 'stat_t', 'uniq_id']);
topic_updates = new EventEmitter<string>();
readonly ent_type: string = 'base';
get name() {
return this._name;
}
getProperty(name: string): any {
if (!this.hasOwnProperty(name)) return;
return this[name as keyof this];
set name(name: string) {
this._name = name;
this._uniq_id = btoa(this._name).slice(-7).slice(0, 5);
this.topic_updates.next('stat_t');
}
get stat_t() {
return this._stat_t;
}
set stat_t(stat: string) {
this._stat_t = stat;
}
get uniq_id() {
return this._uniq_id;
}
set uniq_id(uniq_id: string) {
this._uniq_id = uniq_id;
this.topic_updates.next('stat_t');
}
get display_name() {
if (this._name != '' && this._uniq_id != '') {
return this._name + '_' + this._uniq_id;
} else {
return this._name;
}
}
toJSON(): any {
@@ -24,14 +55,15 @@ export class MQTTEntity {
}
return jsonObject;
}
getProperty(name: string): any {
if (!this.attrs.has(name)) return;
return this[name as keyof this];
}
// toJSON(): any {
// return {
// name: this.name,
// stat_t: this.stat_t,
// uniq_id: this.uniq_id,
// };
// }
setProperty(name: string, value: any) {
if (!this.attrs.has(name)) return;
this[name as keyof this] = value;
}
}
export class MQTTDevice {
@@ -53,3 +85,16 @@ export class DeviceClass {
return this.choices[this.value];
}
}
export interface iMQTTEntityBase {
// name: string;
// stat_t: string;
// uniq_id: string;
// display_name: string;
attrs: Set<string>;
readonly ent_type: string;
topic_updates: EventEmitter<string>;
toJSON: () => string;
}