Skip to content

Commit

Permalink
homekit: window covering wip
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jun 24, 2024
1 parent d39e4e3 commit 1fb4cfd
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions plugins/homekit/src/types/windowcovering.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entry, EntrySensor, ScryptedDevice, ScryptedDeviceType, ScryptedInterface } from '@scrypted/sdk';
import { Brightness, Entry, EntrySensor, ScryptedDevice, ScryptedDeviceType, ScryptedInterface } from '@scrypted/sdk';
import { addSupportedType, bindCharacteristic, DummyDevice, } from '../common';
import { Characteristic, CharacteristicEventTypes, CharacteristicSetCallback, CharacteristicValue, NodeCallback, Service } from '../hap';
import { makeAccessory } from './common';
Expand All @@ -7,38 +7,62 @@ import type { HomeKitPlugin } from "../main";
addSupportedType({
type: ScryptedDeviceType.WindowCovering,
probe(device: DummyDevice): boolean {
return device.interfaces.includes(ScryptedInterface.Entry) && device.interfaces.includes(ScryptedInterface.EntrySensor);
return (device.interfaces.includes(ScryptedInterface.Entry) || device.interfaces.includes(ScryptedInterface.Brightness))
&& device.interfaces.includes(ScryptedInterface.EntrySensor);
},
getAccessory: async (device: ScryptedDevice & Entry & EntrySensor, homekitPlugin: HomeKitPlugin) => {
getAccessory: async (device: ScryptedDevice & Entry & EntrySensor & Brightness, homekitPlugin: HomeKitPlugin) => {
const accessory = makeAccessory(device, homekitPlugin);

const service = accessory.addService(Service.WindowCovering, device.name);

bindCharacteristic(device, ScryptedInterface.EntrySensor, service, Characteristic.CurrentPosition,
() => !!device.entryOpen ? 100 : 0);

bindCharacteristic(device, ScryptedInterface.EntrySensor, service, Characteristic.TargetPosition,
() => !!device.entryOpen ? 100 : 0);

let props = {
minValue: 0,
maxValue: 100,
minStep: 100,
};
let targetState = !!device.entryOpen ? 100 : 0;
service.getCharacteristic(Characteristic.TargetPosition)
.setProps(props)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
callback();
if (value === 100) {
targetState = 100;
device.openEntry();
}
else {
targetState = 0;
device.closeEntry();
}
})
if (device.interfaces.includes(ScryptedInterface.Brightness)) {
bindCharacteristic(device, ScryptedInterface.EntrySensor, service, Characteristic.CurrentPosition,
() => !!device.entryOpen ? 100 : 0);

let targetPosition = !!device.entryOpen ? 100 : 0;
bindCharacteristic(device, ScryptedInterface.EntrySensor, service, Characteristic.TargetPosition,
() => targetPosition);

const props = {
minValue: 0,
maxValue: 100,
minStep: 100,
};
service.getCharacteristic(Characteristic.TargetPosition)
.setProps(props)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
callback();
if (value === 100) {
targetPosition = 100;
device.openEntry();
}
else {
targetPosition = 0;
device.closeEntry();
}
});
}
else if (device.interfaces.includes(ScryptedInterface.Brightness)) {
bindCharacteristic(device, ScryptedInterface.Brightness, service, Characteristic.CurrentPosition,
() => device.brightness || 0);

let targetPosition = device.brightness || 0;
bindCharacteristic(device, ScryptedInterface.Brightness, service, Characteristic.TargetPosition,
() => targetPosition);

const props = {
minValue: 0,
maxValue: 100,
minStep: 1,
};
service.getCharacteristic(Characteristic.TargetPosition)
.setProps(props)
.on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
callback();
targetPosition = value as number;
device.setBrightness(targetPosition);
});
}

return accessory;
}
Expand Down

0 comments on commit 1fb4cfd

Please sign in to comment.