Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/homebridgeAccessories/componentHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { lightHelper } from './light';
import { binarySensorHelper } from './binarySensor';
import { coverHelper } from './cover';
import { sensorHelper } from './sensor';
import { switchHelper } from './switch';
import { PlatformAccessory } from 'homebridge';
Expand All @@ -12,4 +13,5 @@ export const componentHelpers = new Map<ComponentType, ComponentHelper>([
['binarySensor', binarySensorHelper],
['sensor', sensorHelper],
['switch', switchHelper],
['cover', coverHelper]
]);
45 changes: 45 additions & 0 deletions src/homebridgeAccessories/cover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CoverComponent } from 'esphome-ts';
import {
CharacteristicSetCallback,
CharacteristicEventTypes,
CharacteristicValue,
PlatformAccessory
} from 'homebridge';
import { Characteristic, Service } from '../index';

export const coverHelper = (component: CoverComponent, accessory: PlatformAccessory): boolean => {
if (component.assumedState) {
let methods = [component.open, component.close, component.stop];
let labels : { [key: string]: string; } = { "open" : '\u{25B2}' , "close" : '\u{25BC}' , "stop" : '\u{23F9}' }
methods.forEach((method) => {
let service = accessory.services.find((service) => service.UUID === Service.Switch.UUID && service.subtype === method.name);
if (!service) {
service = accessory.addService(new Service.Switch(labels[method.name], method.name))
}
let state = false;
service.getCharacteristic(Characteristic.On)?.on(
CharacteristicEventTypes.GET,
(callback: CharacteristicSetCallback) => {
callback(null, state);
},
);
service.getCharacteristic(Characteristic.On)?.on(
CharacteristicEventTypes.SET,
(on: CharacteristicValue, callback: CharacteristicSetCallback) => {
state = on as boolean;
if (state === true) {
method.call(component);
setTimeout(function () {
service?.setCharacteristic(Characteristic.On, false);
}.bind(this), 200);
}
callback();
},
);
});
return true;
} else {
// TODO actual HomeKit cover
return false;
}
};