homebridge-kasa-python
Version:
Plugin that uses Python-Kasa API to communicate with Kasa Devices.
160 lines • 6.82 kB
JavaScript
export function buildOnDescriptor(C, setState, syncGroup) {
return {
type: C.On,
name: 'On',
writable: true,
syncGroup,
syncHomeKitValueAfterSet: !!syncGroup,
getInitial: context => (context.child ? context.child.state : context.device.state) ?? false,
getCurrent: context => (context.child ? context.child.state : context.device.state) ?? false,
applySet: setState
? async (value, context) => {
await setState(value, context);
const state = Boolean(value);
if (context.child) {
context.child.state = state;
}
else {
context.device.state = state;
}
}
: undefined,
};
}
export function buildBrightnessDescriptor(C, setBrightness) {
return {
type: C.Brightness,
name: 'Brightness',
writable: true,
getInitial: context => (context.child ? context.child.brightness : context.device.brightness) ?? 0,
getCurrent: context => (context.child ? context.child.brightness : context.device.brightness) ?? 0,
applySet: async (value, context) => {
const brightness = Number(value);
await setBrightness(brightness, context);
if (context.child) {
context.child.brightness = brightness;
}
else {
context.device.brightness = brightness;
}
},
};
}
export function buildColorTemperatureDescriptor(C, setColorTemp) {
return {
type: C.ColorTemperature,
name: 'ColorTemperature',
writable: true,
getInitial: context => (context.child ? context.child.color_temp : context.device.color_temp) ?? 0,
getCurrent: context => (context.child ? context.child.color_temp : context.device.color_temp) ?? 0,
applySet: async (value, context) => {
const colorTemp = Number(value);
await setColorTemp(colorTemp, context);
if (context.child) {
context.child.color_temp = colorTemp;
}
else {
context.device.color_temp = colorTemp;
}
},
};
}
export function buildHSVDescriptors(C, enqueueHSV, syncGroup) {
return [
{
type: C.Hue,
name: 'Hue',
writable: true,
syncGroup,
syncHomeKitValueAfterSet: !!syncGroup,
getInitial: context => (context.child ? context.child.hsv?.hue : context.device.hsv?.hue) ?? 0,
getCurrent: context => (context.child ? context.child.hsv?.hue : context.device.hsv?.hue) ?? 0,
applySet: async (value, context) => enqueueHSV({ hue: Number(value) }, context),
},
{
type: C.Saturation,
name: 'Saturation',
writable: true,
syncGroup,
syncHomeKitValueAfterSet: !!syncGroup,
getInitial: context => (context.child ? context.child.hsv?.saturation : context.device.hsv?.saturation) ?? 0,
getCurrent: context => (context.child ? context.child.hsv?.saturation : context.device.hsv?.saturation) ?? 0,
applySet: async (value, context) => enqueueHSV({ saturation: Number(value) }, context),
},
];
}
export function buildOutletInUseDescriptor(C, useEnergyState, syncGroup) {
const energy = (context) => {
const rawPower = context.child ? context.child.energy?.power : context.device.energy?.power;
const powerWatts = Number(rawPower ?? 0);
return Number.isFinite(powerWatts) && powerWatts > context.platform.config.energyOptions.powerThreshold;
};
const state = (context) => (context.child ? context.child.state : context.device.state) ?? false;
return {
type: C.OutletInUse,
name: 'OutletInUse',
writable: false,
syncGroup: useEnergyState ? undefined : syncGroup,
debouncePolls: 2,
syncHomeKitValueAfterSet: !useEnergyState && !!syncGroup,
getInitial: (context) => (useEnergyState ? energy(context) : state(context)),
getCurrent: (context) => (useEnergyState ? energy(context) : state(context)),
};
}
export function buildEnergyDescriptors(energyCharacteristics) {
const definitions = [
[energyCharacteristics.Volts, 'voltage', 'Volts'],
[energyCharacteristics.Amperes, 'current', 'Amperes'],
[energyCharacteristics.Watts, 'power', 'Watts'],
[energyCharacteristics.KiloWattHours, 'total', 'KiloWattHours'],
];
return definitions.map(([characteristicType, field, label]) => ({
type: characteristicType,
name: label,
writable: false,
getInitial: context => (context.child ? context.child.energy?.[field] : context.device.energy?.[field]) ?? 0,
getCurrent: context => (context.child ? context.child.energy?.[field] : context.device.energy?.[field]) ?? 0,
}));
}
export function buildFanActiveDescriptor(C, setActive) {
return {
type: C.Active,
name: 'Active',
writable: true,
getInitial: context => ((context.child ? context.child.state : context.device.state) ? C.Active.ACTIVE : C.Active.INACTIVE),
getCurrent: context => ((context.child ? context.child.state : context.device.state) ? C.Active.ACTIVE : C.Active.INACTIVE),
applySet: async (value, context) => {
const active = value === C.Active.ACTIVE;
await setActive(active, context);
if (context.child) {
context.child.state = active;
}
else {
context.device.state = active;
}
},
};
}
export function buildFanRotationDescriptor(C, setRotation) {
return {
type: C.RotationSpeed,
name: 'RotationSpeed',
writable: true,
syncHomeKitValueAfterSet: true,
getInitial: context => (context.child ? context.child.fan_speed_level : context.device.fan_speed_level) ?? 0,
getCurrent: context => (context.child ? context.child.fan_speed_level : context.device.fan_speed_level) ?? 0,
applySet: async (value, context) => {
const fan_speed_level = Math.min(100, Math.ceil(Math.max(0, Number(value) || 0) / 25) * 25);
await setRotation(fan_speed_level, context);
if (context.child) {
context.child.fan_speed_level = fan_speed_level;
context.child.state = fan_speed_level > 0;
}
else {
context.device.fan_speed_level = fan_speed_level;
context.device.state = fan_speed_level > 0;
}
},
};
}
//# sourceMappingURL=descriptorHelpers.js.map