Skip to content

Commit

Permalink
Fix: address changes in how iOS 16+ handle naming additional services.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjdhjd committed Apr 30, 2023
1 parent 0b97f00 commit 0a95775
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
33 changes: 25 additions & 8 deletions src/protect-camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,15 @@ export class ProtectCamera extends ProtectDevice {
// Add individual contact sensors for each object detection type, if needed.
for(const smartDetectType of this.ufp.featureFlags.smartDetectTypes) {

const contactName = this.accessory.displayName + " " + smartDetectType.charAt(0).toUpperCase() + smartDetectType.slice(1);

// See if we already have this contact sensor configured.
let contactService = this.accessory.getServiceById(this.hap.Service.ContactSensor, ProtectReservedNames.CONTACT_MOTION_SMARTDETECT + "." + smartDetectType);

// If not, let's add it.
if(!contactService) {

contactService = new this.hap.Service.ContactSensor(this.accessory.displayName + " " + smartDetectType.charAt(0).toUpperCase() + smartDetectType.slice(1),
ProtectReservedNames.CONTACT_MOTION_SMARTDETECT + "." + smartDetectType);
contactService = new this.hap.Service.ContactSensor(contactName, ProtectReservedNames.CONTACT_MOTION_SMARTDETECT + "." + smartDetectType);

// Something went wrong, we're done here.
if(!contactService) {
Expand All @@ -299,6 +300,8 @@ export class ProtectCamera extends ProtectDevice {
}

// Initialize the sensor.
contactService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
contactService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, contactName);
contactService.updateCharacteristic(this.hap.Characteristic.ContactSensorState, false);
}

Expand Down Expand Up @@ -351,9 +354,11 @@ export class ProtectCamera extends ProtectDevice {
}
}

const triggerName = this.accessory.displayName + " Doorbell Trigger";

// Add the switch to the camera, if needed.
if(!triggerService) {
triggerService = new this.hap.Service.Switch(this.accessory.displayName + " Doorbell Trigger", ProtectReservedNames.SWITCH_DOORBELL_TRIGGER);
triggerService = new this.hap.Service.Switch(triggerName, ProtectReservedNames.SWITCH_DOORBELL_TRIGGER);

if(!triggerService) {
this.log.error("Unable to add the doorbell trigger.");
Expand Down Expand Up @@ -391,6 +396,8 @@ export class ProtectCamera extends ProtectDevice {
});

// Initialize the switch.
triggerService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
triggerService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, triggerName);
triggerService.updateCharacteristic(this.hap.Characteristic.On, false);

this.log.info("Enabling doorbell automation trigger.");
Expand Down Expand Up @@ -735,10 +742,12 @@ export class ProtectCamera extends ProtectDevice {
return false;
}

const switchName = this.accessory.displayName + " HKSV Recording";

// Add the switch to the camera, if needed.
if(!switchService) {

switchService = new this.hap.Service.Switch(this.accessory.displayName + " HKSV Recording", ProtectReservedNames.SWITCH_HKSV_RECORDING);
switchService = new this.hap.Service.Switch(switchName, ProtectReservedNames.SWITCH_HKSV_RECORDING);

if(!switchService) {

Expand Down Expand Up @@ -767,6 +776,8 @@ export class ProtectCamera extends ProtectDevice {
});

// Initialize the switch.
switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, switchName);
switchService.updateCharacteristic(this.hap.Characteristic.On, this.accessory.context.hksvRecording as boolean);

this.log.info("Enabling HomeKit Secure Video recording switch.");
Expand Down Expand Up @@ -794,10 +805,12 @@ export class ProtectCamera extends ProtectDevice {
return false;
}

const switchName = this.accessory.displayName + " Dynamic Bitrate";

// Add the switch to the camera, if needed.
if(!switchService) {

switchService = new this.hap.Service.Switch(this.accessory.displayName + " Dynamic Bitrate", ProtectReservedNames.SWITCH_DYNAMIC_BITRATE);
switchService = new this.hap.Service.Switch(switchName, ProtectReservedNames.SWITCH_DYNAMIC_BITRATE);

if(!switchService) {

Expand Down Expand Up @@ -856,6 +869,8 @@ export class ProtectCamera extends ProtectDevice {
});

// Initialize the switch.
switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, switchName);
switchService.updateCharacteristic(this.hap.Characteristic.On, this.accessory.context.dynamicBitrate as boolean);

this.log.info("Enabling the dynamic streaming bitrate adjustment switch.");
Expand Down Expand Up @@ -888,12 +903,12 @@ export class ProtectCamera extends ProtectDevice {
continue;
}

const switchName = this.accessory.displayName + " UFP Recording " + ufpRecordingSetting.charAt(0).toUpperCase() + ufpRecordingSetting.slice(1);

// Add the switch to the camera, if needed.
if(!switchService) {

switchService = new this.hap.Service.Switch(
this.accessory.displayName + " UFP Recording " + ufpRecordingSetting.charAt(0).toUpperCase() + ufpRecordingSetting.slice(1),
ufpRecordingSwitchType);
switchService = new this.hap.Service.Switch(switchName, ufpRecordingSwitchType);

if(!switchService) {

Expand Down Expand Up @@ -960,6 +975,8 @@ export class ProtectCamera extends ProtectDevice {
});

// Initialize the recording switch state.
switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, switchName);
switchService.updateCharacteristic(this.hap.Characteristic.On, this.ufp.recordingSettings.mode === ufpRecordingSetting);
switchesEnabled.push(ufpRecordingSetting);
}
Expand Down
15 changes: 12 additions & 3 deletions src/protect-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ export abstract class ProtectDevice extends ProtectBase {

this.log.info("Enabling motion sensor switch.");

const switchName = this.accessory.displayName + " Motion Events";

// Add the switch to the camera, if needed.
if(!switchService) {

switchService = new this.hap.Service.Switch(this.accessory.displayName + " Motion Events", ProtectReservedNames.SWITCH_MOTION_SENSOR);
switchService = new this.hap.Service.Switch(switchName, ProtectReservedNames.SWITCH_MOTION_SENSOR);

if(!switchService) {

Expand Down Expand Up @@ -289,6 +291,8 @@ export abstract class ProtectDevice extends ProtectBase {
this.accessory.context.detectMotion = true;
}

switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, switchName);
switchService.updateCharacteristic(this.hap.Characteristic.On, this.accessory.context.detectMotion as boolean);

return true;
Expand All @@ -310,10 +314,12 @@ export abstract class ProtectDevice extends ProtectBase {
return false;
}

const triggerName = this.accessory.displayName + " Motion Trigger";

// Add the switch to the camera, if needed.
if(!triggerService) {

triggerService = new this.hap.Service.Switch(this.accessory.displayName + " Motion Trigger", ProtectReservedNames.SWITCH_MOTION_TRIGGER);
triggerService = new this.hap.Service.Switch(triggerName, ProtectReservedNames.SWITCH_MOTION_TRIGGER);

if(!triggerService) {
this.log.error("Unable to add motion sensor trigger.");
Expand Down Expand Up @@ -365,6 +371,8 @@ export abstract class ProtectDevice extends ProtectBase {
});

// Initialize the switch.
triggerService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
triggerService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, triggerName);
triggerService.updateCharacteristic(this.hap.Characteristic.On, false);

this.log.info("Enabling motion sensor automation trigger.");
Expand Down Expand Up @@ -393,7 +401,8 @@ export abstract class ProtectDevice extends ProtectBase {
return true;
}

public isOptionEnabled(option: string, defaultReturnValue = true, address = "", addressOnly = false): boolean {
// Utility for checking feature options on a device.
public hasFeature(option: string, defaultReturnValue = true, address = "", addressOnly = false): boolean {

return optionEnabled(this.platform.configOptions, this.nvr.ufp, this.ufp, option, defaultReturnValue, address, addressOnly);
}
Expand Down
4 changes: 3 additions & 1 deletion src/protect-doorbell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ProtectDoorbell extends ProtectCamera {
continue;
}

this.log.info("Discovered doorbell message switch%s: %s.", entry.duration ? " (" + (entry.duration / 1000).toString() + " seconds)" : "", entry.text);
this.log.info("Enabled doorbell message switch%s: %s.", entry.duration ? " (" + (entry.duration / 1000).toString() + " seconds)" : "", entry.text);

// Use the message switch, if it already exists.
let switchService = this.accessory.getServiceById(this.hap.Service.Switch, entry.type + "." + entry.text);
Expand All @@ -123,6 +123,8 @@ export class ProtectDoorbell extends ProtectCamera {
this.messageSwitches.push({ duration: duration, service: switchService, state: false, text: entry.text, type: entry.type }) - 1;

// Configure the message switch.
switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, entry.text);
switchService
.getCharacteristic(this.hap.Characteristic.On)
?.onGet(this.getSwitchState.bind(this, this.messageSwitches[this.messageSwitches.length - 1]))
Expand Down
1 change: 1 addition & 0 deletions src/protect-liveviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class ProtectLiveviews extends ProtectBase {
if(!this.liveviews?.some(x => regexSecuritySystemLiveview.test(x.name))) {

if(this.securityAccessory) {

this.log.info("No plugin-specific liveviews found. Disabling the security system accessory associated with this UniFi Protect controller.");

// Unregister the accessory and delete it's remnants from HomeKit and the plugin.
Expand Down
6 changes: 5 additions & 1 deletion src/protect-securitysystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ export class ProtectSecuritySystem extends ProtectBase {

}

const switchName = this.accessory.displayName + " Security Alarm";

// Add the security alarm switch to the security system.
if(!switchService) {

switchService = new this.hap.Service.Switch(this.accessory.displayName + " Security Alarm");
switchService = new this.hap.Service.Switch(switchName);

if(!switchService) {

Expand All @@ -263,6 +265,8 @@ export class ProtectSecuritySystem extends ProtectBase {
});

// Initialize the value.
switchService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
switchService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, switchName);
switchService.updateCharacteristic(this.hap.Characteristic.On, this.isAlarmTriggered);

return true;
Expand Down
6 changes: 5 additions & 1 deletion src/protect-sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,19 @@ export class ProtectSensor extends ProtectDevice {
// Add the service to the accessory, if needed.
if(!contactService) {

contactService = new this.hap.Service.ContactSensor(this.accessory.displayName + " Alarm Sound", ProtectReservedNames.CONTACT_SENSOR_ALARM_SOUND);
const contactName = this.accessory.displayName + " Alarm Sound";
contactService = new this.hap.Service.ContactSensor(contactName, ProtectReservedNames.CONTACT_SENSOR_ALARM_SOUND);

if(!contactService) {

this.log.error("Unable to add alarm sound contact sensor.");
return false;
}

contactService.addOptionalCharacteristic(this.hap.Characteristic.ConfiguredName);
contactService.updateCharacteristic(this.hap.Characteristic.ConfiguredName, contactName);
this.accessory.addService(contactService);

this.log.info("Enabling alarm sound contact sensor.");
}

Expand Down

0 comments on commit 0a95775

Please sign in to comment.