diff --git a/plugins/homekit/src/types/index.ts b/plugins/homekit/src/types/index.ts index 257498214..6dcb658e7 100644 --- a/plugins/homekit/src/types/index.ts +++ b/plugins/homekit/src/types/index.ts @@ -13,3 +13,4 @@ import './thermostat'; import './vacuum'; import './outlet'; import './notifier'; +import './windowcovering' diff --git a/plugins/homekit/src/types/windowcovering.ts b/plugins/homekit/src/types/windowcovering.ts new file mode 100644 index 000000000..2228855c9 --- /dev/null +++ b/plugins/homekit/src/types/windowcovering.ts @@ -0,0 +1,48 @@ +import { 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'; +import type { HomeKitPlugin } from "../main"; + +addSupportedType({ + type: ScryptedDeviceType.WindowCovering, + probe(device: DummyDevice): boolean { + return device.interfaces.includes(ScryptedInterface.Entry) && device.interfaces.includes(ScryptedInterface.EntrySensor); + }, + getAccessory: async (device: ScryptedDevice & Entry & EntrySensor, 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.GET, (callback: NodeCallback) => { + callback(null, targetState); + }) + .on(CharacteristicEventTypes.SET, (value: CharacteristicValue, callback: CharacteristicSetCallback) => { + callback(); + if (value === 100) { + targetState = 100; + device.openEntry(); + } + else { + targetState = 0; + device.closeEntry(); + } + }) + + return accessory; + } +});