From 138e98fbe57fb9f69507dc92f9b391e927b7dc12 Mon Sep 17 00:00:00 2001 From: Brandon McFarlin Date: Tue, 28 Jul 2020 10:47:03 -0400 Subject: [PATCH] fix sensor names --- package-lock.json | 2 +- package.json | 2 +- src/index.ts | 25 ++++++++++++++----------- src/nest-cam.ts | 18 ++++++++++++++++-- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6268186..2f7242e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "homebridge-nest-cam", - "version": "4.0.0-test.4", + "version": "4.0.0-test.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index ef634c3..89647c4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Homebridge Nest Cam", "name": "homebridge-nest-cam", - "version": "4.0.0-test.4", + "version": "4.0.0-test.5", "description": "Nest cam plugin for homebridge: https://homebridge.io/", "main": "dist/index.js", "license": "GPL-3.0", diff --git a/src/index.ts b/src/index.ts index af162e7..88e505c 100755 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,7 @@ class NestCamPlatform implements DynamicPlatformPlugin { private chimeSwitch = true; private audioSwitch = true; private structures: Array = []; - private alertTypes: Array = ['motion', 'sound', 'person', 'package-delivered', 'package-retrieved', 'face']; + private alertTypes: Array = ['Motion', 'Sound', 'Person', 'Package Delivered', 'Package Retrieved', 'face']; constructor(log: Logging, config: PlatformConfig, api: API) { this.log = log; @@ -246,24 +246,27 @@ class NestCamPlatform implements DynamicPlatformPlugin { } // Remove the previous switch services - const oldSwitchService = accessory.getService(hap.Service.Switch); - if (oldSwitchService) { + let oldSwitchService = accessory.getService(hap.Service.Switch); + while (oldSwitchService) { accessory.removeService(oldSwitchService); + oldSwitchService = accessory.getService(hap.Service.Switch); } // Remove the previous motion services - const oldMotionService = accessory.getService(hap.Service.MotionSensor); - if (oldMotionService) { + let oldMotionService = accessory.getService(hap.Service.MotionSensor); + while (oldMotionService) { accessory.removeService(oldMotionService); + oldMotionService = accessory.getService(hap.Service.MotionSensor); } // Remove the previous doorbell services - const oldDoorbellService = accessory.getService(hap.Service.Doorbell); - if (oldDoorbellService) { + let oldDoorbellService = accessory.getService(hap.Service.Doorbell); + while (oldDoorbellService) { accessory.removeService(oldDoorbellService); + oldDoorbellService = accessory.getService(hap.Service.Doorbell); } // Doorbell configuration this.createDoorbellService( - 'doorbell', + 'Doorbell', camera.info.capabilities.includes('indoor_chime') && this.doorbellAlerts, accessory, camera, @@ -337,12 +340,12 @@ class NestCamPlatform implements DynamicPlatformPlugin { const faces = await camera.getFaces(); if (faces) { faces.forEach((face: any) => { - camera.alertTypes.push(`face-${face.name}`); + camera.alertTypes.push(`Face - ${face.name}`); }); } } } else { - camera.alertTypes = ['motion', 'sound', 'person']; + camera.alertTypes = ['Motion', 'Sound', 'Person']; } camera.alertTypes.forEach((type) => { this.createMotionService( @@ -354,7 +357,7 @@ class NestCamPlatform implements DynamicPlatformPlugin { }); } else { this.createMotionService( - 'motion', + 'Motion', camera.info.capabilities.includes('detectors.on_camera') && this.motionDetection, accessory, camera, diff --git a/src/nest-cam.ts b/src/nest-cam.ts index 0850e06..98c973b 100644 --- a/src/nest-cam.ts +++ b/src/nest-cam.ts @@ -11,6 +11,19 @@ type OnlyBooleans = Pick< }[keyof T] >; +const sanitizeString = (str: string): string => { + if (str.includes('package')) { + // Package + return str.replace('-', ' ').replace(/(?:^|\s|["'([{])+\S/g, (match) => match.toUpperCase()); + } else if (str.includes('face')) { + // Face + return str.replace('-', ' - ').replace('face', 'Face'); + } else { + // Motion, Person, Sound + return str.replace(/(?:^|\s|["'([{])+\S/g, (match) => match.toUpperCase()); + } +}; + export const enum NestCamEvents { CAMERA_STATE_CHANGED = 'camera-change', CHIME_STATE_CHANGED = 'chime-change', @@ -183,6 +196,7 @@ export class NestCam extends EventEmitter { private setMotion(state: boolean, types: Array): void { types.forEach((type) => { + type = sanitizeString(type); const service = this.accessory.getService(`${this.accessory.displayName} ${type}`); if (service) { this.log.debug(`Setting ${this.accessory.displayName} ${type} Motion to ${state}`); @@ -202,9 +216,9 @@ export class NestCam extends EventEmitter { } private setDoorbell(): void { - const doorbellService = this.accessory.getService(`${this.accessory.displayName} doorbell`); + const doorbellService = this.accessory.getService(`${this.accessory.displayName} Doorbell`); if (doorbellService) { - this.log.debug(`Ringing ${this.accessory.displayName} doorbell`); + this.log.debug(`Ringing ${this.accessory.displayName} Doorbell`); doorbellService.updateCharacteristic( this.hap.Characteristic.ProgrammableSwitchEvent, this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,