zigbee2mqtt/lib/model/device.ts
Koen Kanters fb081316e2
More TypeScript refactoring (#8721)
* Update

* u

* Updates

* Updates

* updates

* Updates

* Update
2021-09-11 14:51:29 +02:00

62 lines
2.4 KiB
TypeScript

/* eslint-disable brace-style */
import * as settings from '../util/settings';
// @ts-ignore
import zhc from 'zigbee-herdsman-converters';
export default class Device {
private device: ZHDevice;
private _definition: Definition;
get zhDevice(): ZHDevice {return this.device;}
get ieeeAddr(): string {return this.device.ieeeAddr;}
get ID(): string {return this.device.ieeeAddr;}
get settings(): DeviceSettings {return {...settings.get().device_options, ...settings.getDevice(this.ieeeAddr)};}
get name(): string {return this.settings.friendlyName;}
get lastSeen(): number {return this.device.lastSeen;}
get modelID(): string {return this.device.modelID;}
get manufacturerName(): string {return this.device.manufacturerName;}
get interviewing(): boolean {return this.device.interviewing;}
get type(): 'Coordinator' | 'Router' | 'EndDevice' | 'Unknown' | 'GreenPower' {return this.device.type;}
get powerSource(): string {return this.device.powerSource;}
get definition(): Definition | undefined {
if (!this._definition && !this.device.interviewing) {
this._definition = zhc.findByDevice(this.device);
}
return this._definition;
}
constructor(device: ZHDevice) {
this.device = device;
}
async ping(disableRecovery: boolean): Promise<void> {await this.device.ping(disableRecovery);}
async removeFromNetwork(): Promise<void> {await this.device.removeFromNetwork();}
endpoint(key?: string): ZHEndpoint {
let endpoint: ZHEndpoint;
if (key == null) key = 'default';
if (this.definition?.endpoint) {
const ID = this.definition?.endpoint?.(this.device)[key];
if (ID) endpoint = this.device.getEndpoint(ID);
else if (key === 'default') endpoint = this.device.endpoints[0];
else return null;
} else {
/* istanbul ignore next */
if (key !== 'default') return null;
endpoint = this.device.endpoints[0];
}
return endpoint;
}
isXiaomiDevice(): boolean {
const xiaomiManufacturerID = [4151, 4447];
/* istanbul ignore next */
return this.zhDevice.modelID !== 'lumi.router' && xiaomiManufacturerID.includes(this.zhDevice.manufacturerID) &&
(!this.zhDevice.manufacturerName || !this.zhDevice.manufacturerName.startsWith('Trust'));
}
isRouter(): boolean {return this.zhDevice.type === 'Router';}
}