ring-client-api
Version:
Unofficial API for Ring doorbells, cameras, security alarm system and smart lighting
90 lines (89 loc) • 2.68 kB
JavaScript
import { BehaviorSubject } from 'rxjs';
import { deviceTypesWithVolume } from "./ring-types.js";
import { filter, map } from 'rxjs/operators';
import { Subscribed } from "./subscribed.js";
import { logError } from "./util.js";
export class RingDevice extends Subscribed {
onData;
zid;
id;
deviceType;
categoryId;
onComponentDevices;
initialData;
location;
assetId;
constructor(initialData, location, assetId) {
super();
this.initialData = initialData;
this.location = location;
this.assetId = assetId;
this.onData = new BehaviorSubject(initialData);
this.zid = initialData.zid;
this.id = this.zid;
this.deviceType = this.initialData.deviceType;
this.categoryId = this.initialData.categoryId;
this.onComponentDevices = this.location.onDevices.pipe(map((devices) => devices.filter(({ data }) => data.parentZid === this.id)));
this.addSubscriptions(location.onDeviceDataUpdate
.pipe(filter((update) => update.zid === this.zid))
.subscribe((update) => this.updateData(update)));
}
updateData(update) {
this.onData.next(Object.assign({}, this.data, update));
}
get data() {
return this.onData.getValue();
}
get name() {
return this.data.name;
}
get supportsVolume() {
return (deviceTypesWithVolume.includes(this.data.deviceType) &&
this.data.volume !== undefined);
}
setVolume(volume) {
if (isNaN(volume) || volume < 0 || volume > 1) {
throw new Error('Volume must be between 0 and 1');
}
if (!this.supportsVolume) {
throw new Error(`Volume can only be set on ${deviceTypesWithVolume.join(', ')}`);
}
return this.setInfo({ device: { v1: { volume } } });
}
setInfo(body) {
return this.location.sendMessage({
msg: 'DeviceInfoSet',
datatype: 'DeviceInfoSetType',
dst: this.assetId,
body: [
{
zid: this.zid,
...body,
},
],
});
}
sendCommand(commandType, data = {}) {
this.setInfo({
command: {
v1: [
{
commandType,
data,
},
],
},
}).catch(logError);
}
toString() {
return this.toJSON();
}
toJSON() {
return JSON.stringify({
data: this.data,
}, null, 2);
}
disconnect() {
this.unsubscribe();
}
}