UNPKG

homebridge-kasa-python

Version:

Plugin that uses Python-Kasa API to communicate with Kasa Devices.

95 lines 4.09 kB
import AccessoryInformation from '../accessoryInformation.js'; import { prefixLogger } from '../utils.js'; export default class HomeKitDevice { platform; kasaDevice; category; categoryName; log; deviceManager; homebridgeAccessory; isUpdating = false; constructor(platform, kasaDevice, category, categoryName) { this.platform = platform; this.kasaDevice = kasaDevice; this.category = category; this.categoryName = categoryName; this.deviceManager = platform.deviceManager; this.log = prefixLogger(platform.log, `[${this.name}]`); this.homebridgeAccessory = this.initializeAccessory(); this.homebridgeAccessory.on("identify" /* PlatformAccessoryEvent.IDENTIFY */, () => this.identify()); } initializeAccessory() { const uuid = this.platform.api.hap.uuid.generate(this.id); const homebridgeAccessory = this.platform.configuredAccessories.get(uuid); let platformAccessory; if (!homebridgeAccessory) { this.log.debug(`Creating new Platform Accessory [${this.id}] [${uuid}] category: ${this.categoryName}`); platformAccessory = new this.platform.api.platformAccessory(this.name, uuid, this.category); platformAccessory.context.deviceId = this.id; platformAccessory.context.lastSeen = this.kasaDevice.last_seen; platformAccessory.context.offline = this.kasaDevice.offline; this.platform.registerPlatformAccessory(platformAccessory); } else { this.log.debug(`Existing Platform Accessory found [${homebridgeAccessory.context.deviceId}] ` + `[${homebridgeAccessory.UUID}] category: ${this.categoryName}`); platformAccessory = homebridgeAccessory; this.updateAccessory(platformAccessory); } const accInfo = AccessoryInformation(this.platform.api.hap)(platformAccessory, this); if (!accInfo) { this.log.error('Could not retrieve default AccessoryInformation'); } return platformAccessory; } updateAccessory(platformAccessory) { this.correctAccessoryProperties(platformAccessory, { displayName: this.name, category: this.category, context: { deviceId: this.id, lastSeen: this.kasaDevice.last_seen, offline: this.kasaDevice.offline, }, }); this.platform.configuredAccessories.set(platformAccessory.UUID, platformAccessory); this.platform.api.updatePlatformAccessories([platformAccessory]); } correctAccessoryProperties(obj, properties) { for (const [key, expectedValue] of Object.entries(properties)) { if (obj[key] !== expectedValue) { this.log.debug(`Correcting Platform Accessory ${key} from: ${String(obj[key])} to: ${String(expectedValue)}`); obj[key] = expectedValue; } } } get id() { return this.kasaDevice.sys_info.device_id; } get name() { return this.kasaDevice.sys_info.alias; } get manufacturer() { return 'TP-Link'; } get model() { return `${this.kasaDevice.sys_info.model} ${this.kasaDevice.sys_info.hw_ver}`; } get serialNumber() { return this.kasaDevice.sys_info.mac; } get firmwareRevision() { return this.kasaDevice.sys_info.sw_ver; } addService(serviceConstructor, name, subType) { const serviceName = this.platform.getServiceName(serviceConstructor); this.log.debug(`Creating new ${serviceName} Service on ${name}${subType ? ` [${subType}]` : ''}`); return this.homebridgeAccessory.addService(serviceConstructor, name, subType ? subType : ''); } updateValue(service, characteristic, deviceAlias, value) { this.log.info(`Updating ${this.platform.lsc(service, characteristic)} on ${deviceAlias} to ${value}`); characteristic.updateValue(value); } } //# sourceMappingURL=index.js.map