Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homekit: Added Common Humidifer/Dehumidifer Settings to Thermostat and Fans #1699

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
163 changes: 150 additions & 13 deletions plugins/homekit/src/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sdk, { AirQuality, AirQualitySensor, CO2Sensor, DeviceProvider, Fan, FanMode, NOXSensor, OnOff, PM10Sensor, PM25Sensor, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, VOCSensor } from "@scrypted/sdk";
import sdk, { AirQuality, AirQualitySensor, CO2Sensor, DeviceProvider, Fan, FanMode, HumidityMode, HumiditySensor, HumiditySetting, HumiditySettingStatus, NOXSensor, OnOff, PM10Sensor, PM25Sensor, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, VOCSensor } from "@scrypted/sdk";
import { bindCharacteristic } from "../common";
import { Accessory, Characteristic, CharacteristicEventTypes, Service, uuid } from '../hap';
import type { HomeKitPlugin } from "../main";
Expand Down Expand Up @@ -96,24 +96,161 @@ export function addCarbonDioxideSensor(device: ScryptedDevice & CO2Sensor, acces
return co2Service;
}

export function addFan(device: ScryptedDevice & Fan & OnOff, accessory: Accessory): Service {
if (!device.interfaces.includes(ScryptedInterface.OnOff) && !device.interfaces.includes(ScryptedInterface.Fan))
function commonHumidifierDehumidifier(mode: HumidityMode, subtype: string, name: string, device: ScryptedDevice & HumiditySetting & HumiditySensor, accessory: Accessory): Service {
function currentState(mode: HumidityMode) {
switch(mode) {
case HumidityMode.Humidify:
return Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING;
case HumidityMode.Dehumidify:
return Characteristic.CurrentHumidifierDehumidifierState.DEHUMIDIFYING;
case HumidityMode.Off:
return Characteristic.CurrentHumidifierDehumidifierState.INACTIVE;
default:
return Characteristic.CurrentHumidifierDehumidifierState.IDLE;
}
}

function targetState(mode: HumidityMode) {
switch(mode) {
case HumidityMode.Humidify:
return Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER;
case HumidityMode.Dehumidify:
return Characteristic.TargetHumidifierDehumidifierState.DEHUMIDIFIER;
default:
return Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER;
}
}

const service = accessory.addService(Service.HumidifierDehumidifier, name, subtype);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.Active,
() => {
if (!device.humiditySetting?.mode)
return false;
if (device.humiditySetting.mode === mode)
return true;
if (device.humiditySetting.mode === HumidityMode.Auto)
return true;
return false;
});
service.getCharacteristic(Characteristic.Active).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
mode: value ? mode : HumidityMode.Off
});
});

bindCharacteristic(device, ScryptedInterface.HumiditySensor, service, Characteristic.CurrentRelativeHumidity,
() => device.humidity);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.CurrentHumidifierDehumidifierState,
() => currentState(device.humiditySetting?.activeMode));

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.TargetHumidifierDehumidifierState,
() => targetState(device.humiditySetting?.mode));

service.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
mode: value === Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER
? HumidityMode.Humidify
: value === Characteristic.TargetHumidifierDehumidifierState.DEHUMIDIFIER
? HumidityMode.Dehumidify
: HumidityMode.Auto
});
});

function targetHumidity(setting: HumiditySettingStatus) {
if (!setting)
return 0;

if (setting?.availableModes.includes(HumidityMode.Humidify)
&& setting?.availableModes.includes(HumidityMode.Dehumidify)) {
if (setting?.activeMode === HumidityMode.Humidify)
return setting?.humidifierSetpoint;
if (setting?.activeMode === HumidityMode.Dehumidify)
return setting?.dehumidifierSetpoint;

return 0;
}

if (setting?.availableModes.includes(HumidityMode.Humidify))
return setting?.humidifierSetpoint;

if (setting?.availableModes.includes(HumidityMode.Dehumidify))
return setting?.dehumidifierSetpoint;

return 0;
}

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.TargetRelativeHumidity,
() => targetHumidity(device.humiditySetting));

return service;
}

export function addHumidifier(device: ScryptedDevice & HumiditySetting & HumiditySensor, accessory: Accessory): Service {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs export

var service = commonHumidifierDehumidifier(HumidityMode.Humidify, "humidifier", device.name + " Humidifier", device, accessory);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.RelativeHumidityHumidifierThreshold,
() => device.humiditySetting?.humidifierSetpoint);
service.getCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
humidifierSetpoint: value as number,
});
});

return service;
}

export function addDehumidifer(device: ScryptedDevice & HumiditySetting & HumiditySensor, accessory: Accessory): Service {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs export

var service = commonHumidifierDehumidifier(HumidityMode.Dehumidify, "dehumidifier", device.name + " Dehumidifier", device, accessory);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.RelativeHumidityDehumidifierThreshold,
() => device.humiditySetting?.dehumidifierSetpoint);
service.getCharacteristic(Characteristic.RelativeHumidityDehumidifierThreshold).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
dehumidifierSetpoint: value as number,
});
});

return service;
}

export function addHumiditySetting(device: ScryptedDevice & HumiditySetting & HumiditySensor, accessory: Accessory): Service {
if (!device.interfaces.includes(ScryptedInterface.HumiditySetting) && !device.interfaces.includes(ScryptedInterface.HumiditySensor))
return undefined;

var service;

if (device.humiditySetting?.availableModes.includes(HumidityMode.Humidify)) {
service = addHumidifier(device, accessory);
}

if (device.humiditySetting?.availableModes.includes(HumidityMode.Dehumidify)) {
service = addDehumidifer(device, accessory);
}

return service;
}

export function addFan(device: ScryptedDevice & Fan, accessory: Accessory): Service {
if (!device.interfaces.includes(ScryptedInterface.Fan))
return undefined;

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

if (device.interfaces.includes(ScryptedInterface.OnOff)) {
bindCharacteristic(device, ScryptedInterface.OnOff, service, Characteristic.Active,
() => !!device.on);
bindCharacteristic(device, ScryptedInterface.OnOff, service, Characteristic.Active,
() => device.fan?.active);

service.getCharacteristic(Characteristic.Active).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
if (value)
device.turnOn();
else
device.turnOff();
service.getCharacteristic(Characteristic.Active).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setFan({
mode: value ? FanMode.Auto : FanMode.Manual,
});
}
});

if (device.fan?.counterClockwise !== undefined) {
bindCharacteristic(device, ScryptedInterface.Fan, service, Characteristic.RotationDirection,
Expand Down
104 changes: 46 additions & 58 deletions plugins/homekit/src/types/thermostat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fan, FanMode, HumidityMode, HumiditySensor, HumiditySetting, OnOff, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, TemperatureSetting, TemperatureUnit, Thermometer, ThermostatMode, AirQualitySensor, AirQuality, PM10Sensor, PM25Sensor, VOCSensor, NOXSensor, CO2Sensor } from '@scrypted/sdk';
import { Fan, FanMode, HumidityMode, HumiditySensor, HumiditySetting, OnOff, ScryptedDevice, ScryptedDeviceType, ScryptedInterface, TemperatureSetting, TemperatureUnit, Thermometer, ThermostatMode, AirQualitySensor, AirQuality, PM10Sensor, PM25Sensor, VOCSensor, NOXSensor, CO2Sensor, HumiditySettingStatus } from '@scrypted/sdk';
import { addSupportedType, bindCharacteristic, DummyDevice, } from '../common';
import { Characteristic, CharacteristicEventTypes, CharacteristicSetCallback, CharacteristicValue, Service } from '../hap';
import { addAirQualitySensor, addCarbonDioxideSensor, addFan, makeAccessory } from './common';
import { addAirQualitySensor, addCarbonDioxideSensor, addFan, addHumiditySetting, makeAccessory } from './common';
import type { HomeKitPlugin } from "../main";

addSupportedType({
Expand Down Expand Up @@ -178,72 +178,60 @@ addSupportedType({
() => device.humidity || 0);
}

if (device.interfaces.includes(ScryptedInterface.HumiditySetting) && device.interfaces.includes(ScryptedInterface.HumiditySensor)) {
const humidityService = accessory.addService(Service.HumidifierDehumidifier);
// add fan state to thermostat service even though it is not required or optional,
// in order to expose to Home Assistant HomeKit Controller under their climate entity
if (device.interfaces.includes(ScryptedInterface.Fan)) {
bindCharacteristic(device, ScryptedInterface.Fan, service, Characteristic.TargetFanState,
() => device.fan?.mode === FanMode.Manual
? Characteristic.TargetFanState.MANUAL
: Characteristic.TargetFanState.AUTO);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, humidityService, Characteristic.Active,
() => {
if (!device.humiditySetting?.mode)
return false;
if (device.humiditySetting.mode === HumidityMode.Off)
return false;
return true;
});
humidityService.getCharacteristic(Characteristic.Active).on(CharacteristicEventTypes.SET, (value, callback) => {
service.getCharacteristic(Characteristic.TargetFanState).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
mode: value ? HumidityMode.Auto : HumidityMode.Off
device.setFan({
mode: value === Characteristic.TargetFanState.MANUAL ? FanMode.Manual : FanMode.Auto,
});
});

bindCharacteristic(device, ScryptedInterface.HumiditySensor, humidityService, Characteristic.CurrentRelativeHumidity,
() => device.humidity || 0);
bindCharacteristic(device, ScryptedInterface.Fan, service, Characteristic.CurrentFanState,
() => !device.fan?.active
? Characteristic.CurrentFanState.INACTIVE
: !device.fan.speed
? Characteristic.CurrentFanState.IDLE
: Characteristic.CurrentFanState.BLOWING_AIR);
}

bindCharacteristic(device, ScryptedInterface.HumiditySetting, humidityService, Characteristic.CurrentHumidifierDehumidifierState,
() => !device.humiditySetting?.activeMode
? Characteristic.CurrentHumidifierDehumidifierState.INACTIVE
: device.humiditySetting.activeMode === HumidityMode.Dehumidify
? Characteristic.CurrentHumidifierDehumidifierState.DEHUMIDIFYING
: device.humiditySetting.activeMode === HumidityMode.Humidify
? Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING
: Characteristic.CurrentHumidifierDehumidifierState.IDLE);

bindCharacteristic(device, ScryptedInterface.HumiditySetting, humidityService, Characteristic.TargetHumidifierDehumidifierState,
() => !device.humiditySetting?.mode || device.humiditySetting?.mode === HumidityMode.Auto
? Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER_OR_DEHUMIDIFIER
: device.humiditySetting?.mode === HumidityMode.Dehumidify
? Characteristic.TargetHumidifierDehumidifierState.DEHUMIDIFIER
: Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER);
humidityService.getCharacteristic(Characteristic.TargetHumidifierDehumidifierState).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
mode: value === Characteristic.TargetHumidifierDehumidifierState.HUMIDIFIER
? HumidityMode.Humidify
: value === Characteristic.TargetHumidifierDehumidifierState.DEHUMIDIFIER
? HumidityMode.Dehumidify
: HumidityMode.Auto
});
});
// add relataive target humidity to thermostat service even though it is not required or optional,
// in order to expose to Home Assistant HomeKit Controller under their climate entity
if (device.interfaces.includes(ScryptedInterface.HumiditySetting)) {
function targetHumidity(setting: HumiditySettingStatus) {
if (!setting)
return 0;

bindCharacteristic(device, ScryptedInterface.HumiditySetting, humidityService, Characteristic.RelativeHumidityHumidifierThreshold,
() => device.humiditySetting?.humidifierSetpoint || 0);
humidityService.getCharacteristic(Characteristic.RelativeHumidityHumidifierThreshold).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
humidifierSetpoint: value as number,
});
});
if (setting?.availableModes.includes(HumidityMode.Humidify)
&& setting?.availableModes.includes(HumidityMode.Dehumidify)) {
if (setting?.activeMode === HumidityMode.Humidify)
return setting?.humidifierSetpoint;
if (setting?.activeMode === HumidityMode.Dehumidify)
return setting?.dehumidifierSetpoint;

bindCharacteristic(device, ScryptedInterface.HumiditySetting, humidityService, Characteristic.RelativeHumidityDehumidifierThreshold,
() => device.humiditySetting?.dehumidifierSetpoint || 0);
humidityService.getCharacteristic(Characteristic.RelativeHumidityDehumidifierThreshold).on(CharacteristicEventTypes.SET, (value, callback) => {
callback();
device.setHumidity({
dehumidifierSetpoint: value as number,
});
});
return 0;
}

if (setting?.availableModes.includes(HumidityMode.Humidify))
return setting?.humidifierSetpoint;

if (setting?.availableModes.includes(HumidityMode.Dehumidify))
return setting?.dehumidifierSetpoint;

return 0;
}

bindCharacteristic(device, ScryptedInterface.HumiditySetting, service, Characteristic.TargetRelativeHumidity,
() => targetHumidity(device.humiditySetting));
}

addHumiditySetting(device, accessory);
addFan(device, accessory);
addAirQualitySensor(device, accessory);
addCarbonDioxideSensor(device, accessory);
Expand Down