homebridge#CharacteristicGetCallback TypeScript Examples
The following examples show how to use
homebridge#CharacteristicGetCallback.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: lighbulb-service-builder.ts From homebridge-zigbee-nt with Apache License 2.0 | 6 votes |
private withSaturation(): LighbulbServiceBuilder {
const Characteristic = this.platform.Characteristic;
this.state.color = {
...this.state.color,
s: 100,
};
this.service
.getCharacteristic(Characteristic.Saturation)
.on(
CharacteristicEventTypes.SET,
async (saturation: number, callback: CharacteristicSetCallback) => {
try {
if (this.isOnline) {
await this.client.setSaturation(this.device, saturation);
return callback();
} else {
return callback(new Error('Device is offline'));
}
} catch (e) {
return callback(e);
}
}
);
this.service
.getCharacteristic(Characteristic.Saturation)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.isOnline) {
this.log.debug(`Reading Saturation for ${this.friendlyName}: ${this.state.color.s}`);
this.client.getSaturation(this.device).catch((e) => this.log.error(e.message));
callback(null, get(this.state, 'color.s', 100));
} else {
return callback(new Error('Device is offline'));
}
});
return this;
}
Example #2
Source File: wled-accessory.ts From homebridge-simple-wled with ISC License | 6 votes |
registerCharacteristicHue(): void {
this.lightService.getCharacteristic(this.hap.Characteristic.Hue)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
let colorArray = this.HSVtoRGB(this.hue, this.saturation, this.currentBrightnessToPercent());
this.colorArray = colorArray;
if (this.debug)
this.log("Current hue: " + this.hue + "%");
callback(undefined, this.hue);
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.hue = value as number;
this.turnOffAllEffects();
let colorArray = this.HSVtoRGB(this.hue, this.saturation, this.currentBrightnessToPercent());
this.host.forEach((host) => {
httpSendData(`http://${host}/json`, "POST", { "bri": this.brightness, "seg": [{ "col": [colorArray] }] }, (error: any, response: any) => { if (error) this.log("Error while changing color of WLED " + this.name + " (" + host + ")"); });
if (this.prodLogging)
this.log("Changed color to " + colorArray + " on host " + host);
})
this.colorArray = colorArray;
callback();
});
}
Example #3
Source File: ikea-motion-sensor.ts From homebridge-zigbee-nt with Apache License 2.0 | 6 votes |
getAvailableServices(): Service[] {
const Service = this.platform.api.hap.Service;
const Characteristic = this.platform.api.hap.Characteristic;
this.sensorService =
this.accessory.getService(Service.MotionSensor) ||
this.accessory.addService(Service.MotionSensor);
this.sensorService.setCharacteristic(Characteristic.Name, this.friendlyName);
this.sensorService
.getCharacteristic(Characteristic.MotionDetected)
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
if (this.state.occupancy) {
this.log.debug(`Motion detected for sensor ${this.friendlyName}`);
}
callback(null, this.state.occupancy === true);
});
this.sensorService
.getCharacteristic(Characteristic.StatusLowBattery)
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
callback(
null,
this.state.battery && this.state.battery <= 10
? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
: Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
);
});
this.batteryService =
this.accessory.getService(Service.BatteryService) ||
this.accessory.addService(Service.BatteryService);
return [this.sensorService, this.batteryService];
}
Example #4
Source File: platform.ts From homebridge-screenlogic with MIT License | 6 votes |
/** convenience function to add an `on('get')` handler which refreshes accessory values if needed */
public triggersRefreshIfNeded(service: Service, type: CharacteristicType): void {
const characteristic = service.getCharacteristic(type)
characteristic.on('get', (callback: CharacteristicGetCallback) => {
// just return current cached value, and refresh if needed
callback(null, characteristic.value)
if (this.refreshIfNeeded()) {
this.log.debug('triggered refresh on get', service.displayName, characteristic.displayName)
}
})
}
Example #5
Source File: platformL530Accessory.ts From homebridge-tapo-p100 with Apache License 2.0 | 6 votes |
/**
* Handle the "GET" requests from HomeKit
* These are sent when HomeKit wants to know the current state of the accessory.
*
*/
getOn(callback: CharacteristicGetCallback) {
// implement your own code to check if the device is on
this.l530.getDeviceInfo().then((response) => {
if(response){
const isOn = response.device_on;
this.platform.log.debug('Get Characteristic On ->', isOn);
if (this.fakeGatoHistoryService) {
this.fakeGatoHistoryService.addEntry({
time: new Date().getTime() / 1000,
status: + isOn,
});
}
// you must call the callback function
// the first argument should be null if there were no errors
// the second argument should be the value to return
// you must call the callback function
if(isOn !== undefined){
callback(null, isOn);
} else{
callback(new Error('unreachable'), isOn);
}
} else{
callback(new Error('unreachable'), false);
}
}).catch(() => {
callback(new Error('unreachable'), false);
});
}
Example #6
Source File: wled-accessory.ts From homebridge-simple-wled with ISC License | 6 votes |
registerCharacteristicAmbilightOnOff(): void {
this.ambilightService.getCharacteristic(this.hap.Characteristic.On)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.debug)
this.log("Current state of the switch was returned: " + (this.ambilightOn ? "ON" : "OFF"));
callback(undefined, this.ambilightOn);
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.ambilightOn = value as boolean;
if (this.ambilightOn) {
this.turnOnAmbilight();
} else {
this.turnOffAmbilight();
}
if (this.debug)
this.log("Switch state was set to: " + (this.ambilightOn ? "ON" : "OFF"));
callback();
});
}
Example #7
Source File: platformL510EAccessory.ts From homebridge-tapo-p100 with Apache License 2.0 | 6 votes |
/**
* Handle the "GET" requests from HomeKit
* These are sent when HomeKit wants to know the current state of the accessory.
*
*/
getBrightness(callback: CharacteristicGetCallback) {
this.l510e.getDeviceInfo().then((response) => {
if(response){
const brightness = response.brightness;
if(brightness !== undefined){
this.platform.log.debug('Get Characteristic Brightness ->', brightness);
// you must call the callback function
// the first argument should be null if there were no errors
// the second argument should be the value to return
// you must call the callback function
callback(null, brightness);
} else{
callback(new Error('unreachable'), 0);
}
} else{
callback(new Error('unreachable'), 0);
}
}).catch(() => {
callback(new Error('unreachable'), 0);
});
}
Example #8
Source File: platformAccessory.ts From HomebridgeMagicHome-DynamicPlatform with Apache License 2.0 | 6 votes |
/**
** @getOn
* instantly retrieve the current on/off state stored in our object
* next call this.getState() which will update all values asynchronously as they are ready
*/
getOn(callback: CharacteristicGetCallback) {
const isOn = this.lightState.isOn;
//update state with actual values asynchronously
this.updateLocalState();
this.logs.debug('Get Characteristic On -> %o for device: %o ', isOn, this.myDevice.displayName);
callback(null, isOn);
}
Example #9
Source File: accessory.ts From homebridge-nest-cam with GNU General Public License v3.0 | 6 votes |
createSwitchService(
name: string,
serviceType: ServiceType,
_key: keyof Properties,
cb: (value: CharacteristicValue) => Promise<void>,
): void {
const service = this.createService(serviceType, name);
this.log.debug(`Creating switch for ${this.accessory.displayName} ${name}.`);
service
.setCharacteristic(this.hap.Characteristic.On, this.camera.info.properties[_key])
.getCharacteristic(this.hap.Characteristic.On)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
cb(value);
this.log.info(`Setting ${this.accessory.displayName} ${name} to ${value ? 'on' : 'off'}`);
callback();
})
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(undefined, this.camera.info.properties[_key]);
});
}
Example #10
Source File: platformAccessory.ts From homebridge-plugin-govee with Apache License 2.0 | 6 votes |
getCurrentRelativeHumidity(callback: CharacteristicGetCallback) {
this.platform.log.debug(
"getCurrentRelativeHumidity",
this.lastReading?.humidity,
"offset",
this.HUMIDITY_OFFSET
);
callback(null, this.lastReading?.humidity + this.HUMIDITY_OFFSET);
}
Example #11
Source File: wled-accessory.ts From homebridge-simple-wled with ISC License | 6 votes |
registerCharacteristicBrightness(): void {
this.lightService.getCharacteristic(this.hap.Characteristic.Brightness)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.debug)
this.log("Current brightness: " + this.brightness);
callback(undefined, this.currentBrightnessToPercent());
})
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.brightness = Math.round(255 / 100 * (value as number));
this.httpSetBrightness();
if (this.prodLogging)
this.log("Set brightness to " + value + "% " + this.brightness);
callback();
});
if (this.showEffectControl) {
// EFFECT SPEED
this.speedService.getCharacteristic(this.hap.Characteristic.Brightness)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
callback(undefined, Math.round(this.effectSpeed / 2.55));
}).on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.effectSpeed = value as number;
this.effectSpeed = Math.round(this.effectSpeed * 2.55);
if (this.prodLogging)
this.log("Speed set to " + this.effectSpeed);
this.effectsService.setCharacteristic(this.Characteristic.ActiveIdentifier, this.lastPlayedEffect);
callback();
});
}
}
Example #12
Source File: platformP110Accessory.ts From homebridge-tapo-p100 with Apache License 2.0 | 6 votes |
/**
* Handle the "GET" requests from HomeKit
* These are sent when HomeKit wants to know the current state of the accessory.
*
*/
getTotalConsumption(callback: CharacteristicGetCallback) {
const consumption = this.p110.getPowerConsumption();
// you must call the callback function
// the first argument should be null if there were no errors
// the second argument should be the value to return
// you must call the callback function
callback(null, consumption.total);
}
Example #13
Source File: platformAccessory.d.ts From homebridge-plugin-govee with Apache License 2.0 | 5 votes |
getCurrentRelativeHumidity(callback: CharacteristicGetCallback): void;
Example #14
Source File: lighbulb-service-builder.ts From homebridge-zigbee-nt with Apache License 2.0 | 5 votes |
public withBrightness(): LighbulbServiceBuilder {
const Characteristic = this.Characteristic;
this.state.brightness_percent = 100;
this.service
.getCharacteristic(Characteristic.Brightness)
.on(
CharacteristicEventTypes.SET,
async (brightnessPercent: number, callback: CharacteristicSetCallback) => {
try {
if (this.isOnline) {
Object.assign(
this.state,
await this.client.setBrightnessPercent(this.device, brightnessPercent)
);
return callback();
} else {
return callback(new Error('Device is offline'));
}
} catch (e) {
return callback(e);
}
}
);
this.service
.getCharacteristic(Characteristic.Brightness)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.isOnline) {
this.client.getBrightnessPercent(this.device).catch((e) => {
this.log.error(e.message);
});
this.log.debug(
`Reading Brightness for ${this.friendlyName}: ${this.state.brightness_percent}`
);
return callback(null, get(this.state, 'brightness_percent', 100));
} else {
return callback(new Error('Device is offline'));
}
});
return this;
}
Example #15
Source File: platformAccessory.ts From homebridge-plugin-govee with Apache License 2.0 | 5 votes |
getStatusLowBattery(callback: CharacteristicGetCallback) {
this.platform.log.debug("getStatusLowBattery");
callback(null, this.lastReading?.battery <= this.LOW_BATTERY_THRESHOLD);
}
Example #16
Source File: doorbell-platform-accessory.ts From homebridge-plugin-eufy-security with Apache License 2.0 | 5 votes |
handleProgrammableSwitchEventGet(callback: CharacteristicGetCallback) {
callback(null, null);
}
Example #17
Source File: xiaomi-vibration-sensor.ts From homebridge-zigbee-nt with Apache License 2.0 | 5 votes |
getAvailableServices() {
const Characteristic = this.platform.Characteristic;
this.contactService =
this.accessory.getService(this.platform.Service.ContactSensor) ||
this.accessory.addService(this.platform.Service.ContactSensor);
this.contactService
.getCharacteristic(Characteristic.ContactSensorState)
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
this.log.debug(
`XiaomiVibrationSensor get Vibration Sensor State for ${this.accessory.displayName}`,
this.state
);
const vibrationDetected = this.state.strength || this.state.action;
callback(
null,
vibrationDetected
? Characteristic.ContactSensorState.CONTACT_DETECTED
: Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
);
});
const supportedServices = [this.contactService];
if (this.supports('battery')) {
this.batteryService = new BatteryServiceBuilder(
this.platform,
this.accessory,
this.client,
this.state
)
.withBatteryPercentage()
.build();
supportedServices.push(this.batteryService);
}
return supportedServices;
}
Example #18
Source File: index.ts From homebridge-fordpass with GNU General Public License v3.0 | 4 votes |
configureAccessory(accessory: PlatformAccessory): void {
const self = this;
this.log.info(`Configuring accessory ${accessory.displayName}`);
accessory.on(PlatformAccessoryEvent.IDENTIFY, () => {
this.log.info(`${accessory.displayName} identified!`);
});
const vehicle = new Vehicle(accessory.context.name, accessory.context.vin, this.config, this.log);
const fordAccessory = new FordpassAccessory(accessory);
// Create Lock service
const defaultState = hap.Characteristic.LockTargetState.UNSECURED;
const lockService = fordAccessory.createService(hap.Service.LockMechanism);
const switchService = fordAccessory.createService(hap.Service.Switch);
const batteryService = fordAccessory.createService(
hap.Service.Battery,
this.config.options?.batteryName || 'Fuel Level',
);
lockService.setCharacteristic(hap.Characteristic.LockCurrentState, defaultState);
lockService
.setCharacteristic(hap.Characteristic.LockTargetState, defaultState)
.getCharacteristic(hap.Characteristic.LockTargetState)
.on(CharacteristicEventTypes.SET, async (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.log.debug(`${value ? 'Locking' : 'Unlocking'} ${accessory.displayName}`);
let commandId = '';
let command = Command.LOCK;
if (value === hap.Characteristic.LockTargetState.UNSECURED) {
command = Command.UNLOCK;
}
commandId = await vehicle.issueCommand(command);
let tries = 30;
this.pendingLockUpdate = true;
const self = this;
const interval = setInterval(async () => {
if (tries > 0) {
const status = await vehicle.commandStatus(command, commandId);
if (status?.status === 200) {
lockService.updateCharacteristic(hap.Characteristic.LockCurrentState, value);
self.pendingLockUpdate = false;
clearInterval(interval);
}
tries--;
} else {
self.pendingLockUpdate = false;
clearInterval(interval);
}
}, 1000);
callback(undefined, value);
})
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
// Return cached value immediately then update properly
let lockNumber = hap.Characteristic.LockTargetState.UNSECURED;
const lockStatus = vehicle?.info?.lockStatus.value;
if (lockStatus === 'LOCKED') {
lockNumber = hap.Characteristic.LockTargetState.SECURED;
}
callback(undefined, lockNumber);
const status = await vehicle.status();
if (status) {
let lockNumber = hap.Characteristic.LockTargetState.UNSECURED;
const lockStatus = status.lockStatus.value;
if (lockStatus === 'LOCKED') {
lockNumber = hap.Characteristic.LockTargetState.SECURED;
}
lockService.updateCharacteristic(hap.Characteristic.LockCurrentState, lockNumber);
lockService.updateCharacteristic(hap.Characteristic.LockTargetState, lockNumber);
} else {
self.log.error(`Cannot get information for ${accessory.displayName} lock`);
}
});
switchService
.setCharacteristic(hap.Characteristic.On, false)
.getCharacteristic(hap.Characteristic.On)
.on(CharacteristicEventTypes.SET, async (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
this.log.debug(`${value ? 'Starting' : 'Stopping'} ${accessory.displayName}`);
if (value as boolean) {
await vehicle.issueCommand(Command.START);
} else {
await vehicle.issueCommand(Command.STOP);
}
callback(undefined, value);
})
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
// Return cached value immediately then update properly
const engineStatus = vehicle?.info?.remoteStartStatus.value || 0;
callback(undefined, engineStatus);
const status = await vehicle.status();
if (status) {
let started = false;
const engineStatus = status.remoteStartStatus.value || 0;
if (engineStatus > 0) {
started = true;
}
switchService.updateCharacteristic(hap.Characteristic.On, started);
} else {
self.log.error(`Cannot get information for ${accessory.displayName} engine`);
}
});
batteryService
.setCharacteristic(hap.Characteristic.BatteryLevel, 100)
.getCharacteristic(hap.Characteristic.BatteryLevel)
.on(CharacteristicEventTypes.GET, async (callback: CharacteristicGetCallback) => {
// Return cached value immediately then update properly
const fuel = vehicle?.info?.fuel?.fuelLevel;
const battery = vehicle?.info?.batteryFillLevel?.value;
let level = fuel || battery || 100;
if (level > 100) {
level = 100;
}
if (level < 0) {
level = 0;
}
callback(undefined, level);
const status = await vehicle.status();
if (status) {
const fuel = status.fuel?.fuelLevel;
const battery = status.batteryFillLevel?.value;
const chargingStatus = vehicle?.info?.chargingStatus?.value;
let level = fuel || battery || 100;
if (level > 100) {
level = 100;
}
if (level < 0) {
level = 0;
}
batteryService.updateCharacteristic(hap.Characteristic.BatteryLevel, level);
if (battery) {
if (chargingStatus === 'ChargingAC') {
batteryService.updateCharacteristic(
hap.Characteristic.ChargingState,
hap.Characteristic.ChargingState.CHARGING,
);
} else {
batteryService.updateCharacteristic(
hap.Characteristic.ChargingState,
hap.Characteristic.ChargingState.NOT_CHARGING,
);
}
} else {
batteryService.updateCharacteristic(
hap.Characteristic.ChargingState,
hap.Characteristic.ChargingState.NOT_CHARGEABLE,
);
}
if (level < 10) {
batteryService.updateCharacteristic(
hap.Characteristic.StatusLowBattery,
hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW,
);
} else {
batteryService.updateCharacteristic(
hap.Characteristic.StatusLowBattery,
hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
);
}
} else {
self.log.error(`Cannot get information for ${accessory.displayName} engine`);
}
});
this.vehicles.push(vehicle);
this.accessories.push(accessory);
}
Example #19
Source File: lighbulb-service-builder.ts From homebridge-zigbee-nt with Apache License 2.0 | 4 votes |
/**
* Special treatment for bulbs supporting only XY colors (IKEA Tådfri for example)
* HomeKit only knows about HSB, so we need to manually convert values
*/
public withColorXY(): LighbulbServiceBuilder {
const Characteristic = this.Characteristic;
this.state.brightness_percent = 100;
this.state.color = {
...this.state.color,
s: 100,
hue: 360,
};
const updateClientColor = async () => {
const hsb = new HSBType(
this.state.color.hue,
this.state.color.s,
this.state.brightness_percent
);
const [x, y] = hsb.toXY();
const state = await this.client.setColorXY(this.device, x, y);
const resultHsbType = HSBType.fromXY(state.color.x, state.color.y);
this.state.color.hue = resultHsbType.hue;
this.state.color.s = resultHsbType.saturation;
};
this.service
.getCharacteristic(Characteristic.Hue)
.on(CharacteristicEventTypes.SET, async (h: number, callback: CharacteristicSetCallback) => {
try {
if (this.isOnline) {
/* Update state immediately so other requests use the latest settings */
this.state.color.hue = h;
await updateClientColor();
return callback(null);
} else {
return callback(new Error('Device is offline'));
}
} catch (e) {
return callback(e);
}
});
this.service
.getCharacteristic(Characteristic.Hue)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.isOnline) {
/* Trigger read that will be reported later by internalUpdate */
this.client.getColorXY(this.device).catch((e) => this.log.error(e.message));
return callback(null, get(this.state, 'color.hue', 360));
} else {
return callback(new Error('Device is offline'));
}
});
this.service
.getCharacteristic(Characteristic.Saturation)
.on(
CharacteristicEventTypes.SET,
async (saturation: number, callback: CharacteristicSetCallback) => {
try {
if (this.isOnline) {
/* Update state immediately so other requests use the latest settings */
this.state.color.s = saturation;
await updateClientColor();
return callback();
} else {
return callback(new Error('Device is offline'));
}
} catch (e) {
return callback(e);
}
}
);
this.service
.getCharacteristic(Characteristic.Saturation)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.isOnline) {
/* Trigger read that will be reported later by internalUpdate */
this.client.getColorXY(this.device).catch((e) => this.log.error(e.message));
return callback(null, get(this.state, 'color.s', 100));
} else {
return callback(new Error('Device is offline'));
}
});
this.service
.getCharacteristic(Characteristic.Brightness)
.on(
CharacteristicEventTypes.SET,
async (brightnessPercent: number, callback: CharacteristicSetCallback) => {
try {
if (this.isOnline) {
/* Update state immediately so other requests use the latest settings */
this.state.brightness_percent = brightnessPercent;
Object.assign(
this.state,
await this.client.setBrightnessPercent(this.device, brightnessPercent)
);
return callback(null, get(this.state, 'brightness_percent', 100));
} else {
return callback(new Error('Device is offline'));
}
} catch (e) {
return callback(e);
}
}
);
this.service
.getCharacteristic(Characteristic.Brightness)
.on(CharacteristicEventTypes.GET, (callback: CharacteristicGetCallback) => {
if (this.isOnline) {
/* Trigger read that will be reported later by internalUpdate */
this.client.getBrightnessPercent(this.device).catch((e) => this.log.error(e.message));
return callback(null, get(this.state, 'brightness_percent', 100));
} else {
return callback(new Error('Device is offline'));
}
});
return this;
}