Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
fix sensor names
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandawg93 committed Jul 28, 2020
1 parent 101cbef commit 138e98f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NestCamPlatform implements DynamicPlatformPlugin {
private chimeSwitch = true;
private audioSwitch = true;
private structures: Array<string> = [];
private alertTypes: Array<string> = ['motion', 'sound', 'person', 'package-delivered', 'package-retrieved', 'face'];
private alertTypes: Array<string> = ['Motion', 'Sound', 'Person', 'Package Delivered', 'Package Retrieved', 'face'];

constructor(log: Logging, config: PlatformConfig, api: API) {
this.log = log;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -354,7 +357,7 @@ class NestCamPlatform implements DynamicPlatformPlugin {
});
} else {
this.createMotionService(
'motion',
'Motion',
camera.info.capabilities.includes('detectors.on_camera') && this.motionDetection,
accessory,
camera,
Expand Down
18 changes: 16 additions & 2 deletions src/nest-cam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ type OnlyBooleans<T> = 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',
Expand Down Expand Up @@ -183,6 +196,7 @@ export class NestCam extends EventEmitter {

private setMotion(state: boolean, types: Array<string>): 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}`);
Expand All @@ -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,
Expand Down

0 comments on commit 138e98f

Please sign in to comment.