homebridge-ring
Version:
Homebridge plugin for Ring doorbells, cameras, security alarm system and smart lighting
117 lines (116 loc) • 4.89 kB
JavaScript
import { hap } from "./hap.js";
import { BaseDataAccessory } from "./base-data-accessory.js";
import { logInfo } from 'ring-client-api/util';
const minutesFor24Hours = 24 * 60;
export class Chime extends BaseDataAccessory {
device;
accessory;
config;
constructor(device, accessory, config) {
super();
this.device = device;
this.accessory = accessory;
this.config = config;
const { Characteristic, Service } = hap, snoozeService = this.getService(Service.Switch, device.name + ' Snooze', 'snooze'), playDingService = this.getService(Service.Switch, device.name + ' Play Ding', 'play-ding'), playMotionService = this.getService(Service.Switch, device.name + ' Play Motion', 'play-motion'), hasNightlight = device.data.settings.night_light_settings !== undefined;
// Snooze Switch
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: snoozeService,
getValue: (data) => Boolean(data.do_not_disturb.seconds_left),
setValue: (snooze) => {
if (snooze) {
logInfo(device.name + ' snoozed for 24 hours');
return device.snooze(minutesFor24Hours);
}
logInfo(device.name + ' snooze cleared');
return device.clearSnooze();
},
requestUpdate: () => device.requestUpdate(),
});
snoozeService.setPrimaryService(true);
// Speaker Service
this.registerCharacteristic({
characteristicType: Characteristic.Mute,
serviceType: Service.Speaker,
getValue: () => false,
});
this.registerLevelCharacteristic({
characteristicType: Characteristic.Volume,
serviceType: Service.Speaker,
getValue: (data) => data.settings.volume,
setValue: (volume) => device.setVolume(volume),
requestUpdate: () => device.requestUpdate(),
});
this.getService(Service.Speaker)
.getCharacteristic(Characteristic.Volume)
.setProps({
minValue: 0,
maxValue: 11,
});
// Play Sound Switches
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: playDingService,
getValue: () => false,
setValue: (play) => {
if (!play) {
return;
}
setTimeout(() => {
playDingService
.getCharacteristic(Characteristic.On)
.updateValue(false);
}, 1000);
return this.device.playSound('ding');
},
requestUpdate: () => device.requestUpdate(),
});
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: playMotionService,
getValue: () => false,
setValue: (play) => {
if (!play) {
return;
}
setTimeout(() => {
playMotionService
.getCharacteristic(Characteristic.On)
.updateValue(false);
}, 1000);
return this.device.playSound('motion');
},
requestUpdate: () => device.requestUpdate(),
});
// Nightlight (if supported)
if (hasNightlight) {
const nightlightService = this.getService(Service.Lightbulb, device.name + ' Nightlight', 'nightlight');
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: nightlightService,
getValue: (data) => Boolean(data.settings.night_light_settings?.light_sensor_enabled),
setValue: (enabled) => {
logInfo(`${device.name} nightlight ${enabled ? 'enabled' : 'disabled'}`);
return device.setNightlightEnabled(enabled);
},
requestUpdate: () => device.requestUpdate(),
});
}
// Accessory Information Service
this.registerCharacteristic({
characteristicType: Characteristic.Manufacturer,
serviceType: Service.AccessoryInformation,
getValue: () => 'Ring',
});
this.registerCharacteristic({
characteristicType: Characteristic.Model,
serviceType: Service.AccessoryInformation,
getValue: () => device.model,
});
this.registerCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: Service.AccessoryInformation,
getValue: (data) => data.device_id || 'Unknown',
});
}
}