Skip to content

Commit

Permalink
Fix switch accessory & add support for Smart IR (wnykq) (#115)
Browse files Browse the repository at this point in the history
* Fix switch accessory & add support for Smart IR (wnykq)

* Run eslint --fix and fix bug with error in JSON.parse

* Add new temp sensor for IR devices and revert code for subdevices properties support
  • Loading branch information
bimusiek authored Nov 24, 2022
1 parent 046f7bd commit 49420f7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"main": "dist/index.js",
"scripts": {
"lint": "eslint src/**/**.ts --max-warnings=0",
"lint": "eslint src/**/*.ts --max-warnings=0",
"test": "jest",
"watch": "npm run build && npm link && nodemon",
"build": "rimraf ./dist && tsc",
Expand Down
4 changes: 4 additions & 0 deletions src/accessory/AccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import AirQualitySensorAccessory from './AirQualitySensorAccessory';
import HumanPresenceSensorAccessory from './HumanPresenceSensorAccessory';
import HumidifierAccessory from './HumidifierAccessory';
import AirPurifierAccessory from './AirPurifierAccessory';
import TemperatureHumidityIRSensorAccessory from './TemperatureHumidityIRSensorAccessory';


export default class AccessoryFactory {
Expand Down Expand Up @@ -104,6 +105,9 @@ export default class AccessoryFactory {
case 'co2bj':
handler = new CarbonDioxideSensorAccessory(platform, accessory);
break;
case 'wnykq':
handler = new TemperatureHumidityIRSensorAccessory(platform, accessory);
break;
case 'wsdcg':
handler = new TemperatureHumiditySensorAccessory(platform, accessory);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/accessory/SwitchAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class SwitchAccessory extends BaseAccessory {
this.accessory.removeService(oldService);
}

const schema = this.device.schema.filter((schema) => schema.code.startsWith('switch') && schema.type !== TuyaDeviceSchemaType.Boolean);
const schema = this.device.schema.filter((schema) => schema.code.startsWith('switch') && schema.type === TuyaDeviceSchemaType.Boolean);
for (const _schema of schema) {
const name = (schema.length === 1) ? this.device.name : _schema.code;
this.configureSwitch(_schema, name);
Expand Down
23 changes: 23 additions & 0 deletions src/accessory/TemperatureHumidityIRSensorAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';
import { configureHumiditySensor } from './TemperatureHumiditySensorAccessory/configureHumiditySensor';
import { configureTemperatureSensor } from './TemperatureHumiditySensorAccessory/configureTemperatureSensor';


export default class TemperatureHumidityIRSensorAccessory extends BaseAccessory {

constructor(platform: TuyaPlatform, accessory: PlatformAccessory) {
super(platform, accessory);

configureTemperatureSensor(this);
configureHumiditySensor(this);
}

requiredSchema() {
return [];
}



}
48 changes: 4 additions & 44 deletions src/accessory/TemperatureHumiditySensorAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaDeviceSchemaIntegerProperty } from '../device/TuyaDevice';
import { TuyaPlatform } from '../platform';
import { limit } from '../util/util';
import BaseAccessory from './BaseAccessory';
import { configureHumiditySensor } from './TemperatureHumiditySensorAccessory/configureHumiditySensor';
import { configureTemperatureSensor } from './TemperatureHumiditySensorAccessory/configureTemperatureSensor';

const SCHEMA_CODE = {
SENSOR_STATUS: ['va_temperature', 'va_humidity', 'humidity_value'],
Expand All @@ -13,54 +13,14 @@ export default class TemperatureHumiditySensorAccessory extends BaseAccessory {
constructor(platform: TuyaPlatform, accessory: PlatformAccessory) {
super(platform, accessory);

this.configureTemperatureSensor();
this.configureHumiditySensor();
configureTemperatureSensor(this);
configureHumiditySensor(this);
}

requiredSchema() {
return [SCHEMA_CODE.SENSOR_STATUS];
}

configureTemperatureSensor() {
const schema = this.getSchema('va_temperature');
if (!schema) {
this.log.warn('TemperatureSensor not supported.');
return;
}

const service = this.accessory.getService(this.Service.TemperatureSensor)
|| this.accessory.addService(this.Service.TemperatureSensor);

const property = schema.property as TuyaDeviceSchemaIntegerProperty;
const multiple = Math.pow(10, property ? property.scale : 0);
service.getCharacteristic(this.Characteristic.CurrentTemperature)
.onGet(() => {
const status = this.getStatus(schema.code)!;
// this.log.debug('CurrentTemperature:', 'property =', property, 'multiple =', multiple, 'status =', status);
return limit(status.value as number / multiple, -270, 100);
});

}

configureHumiditySensor() {
const schema = this.getSchema('va_humidity', 'humidity_value');
if (!schema) {
this.log.warn('HumiditySensor not supported.');
return;
}

const service = this.accessory.getService(this.Service.HumiditySensor)
|| this.accessory.addService(this.Service.HumiditySensor);

const property = schema.property as TuyaDeviceSchemaIntegerProperty;
const multiple = Math.pow(10, property ? property.scale : 0);
service.getCharacteristic(this.Characteristic.CurrentRelativeHumidity)
.onGet(() => {
const status = this.getStatus(schema.code)!;
// this.log.debug('CurrentRelativeHumidity:', 'property =', property, 'multiple =', multiple, 'status =', status);
return limit(status.value as number / multiple, 0, 100);
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TuyaDeviceSchemaIntegerProperty } from '../../device/TuyaDevice';
import { limit } from '../../util/util';
import BaseAccessory from '../BaseAccessory';

export function configureHumiditySensor(accessory: BaseAccessory) {
const schema = accessory.getSchema('va_humidity', 'humidity_value');
if (!schema) {
accessory.log.warn('HumiditySensor not supported.');
return;
}

const service = accessory.accessory.getService(accessory.Service.HumiditySensor)
|| accessory.accessory.addService(accessory.Service.HumiditySensor);

const property = schema.property as TuyaDeviceSchemaIntegerProperty;
const multiple = Math.pow(10, property ? property.scale : 0);
service.getCharacteristic(accessory.Characteristic.CurrentRelativeHumidity)
.onGet(() => {
const status = accessory.getStatus(schema.code)!;
// this.log.debug('CurrentRelativeHumidity:', 'property =', property, 'multiple =', multiple, 'status =', status);
return limit(status.value as number / multiple, 0, 100);
});

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TuyaDeviceSchemaIntegerProperty } from '../../device/TuyaDevice';
import { limit } from '../../util/util';
import BaseAccessory from '../BaseAccessory';

export function configureTemperatureSensor(accessory: BaseAccessory) {
const schema = accessory.getSchema('va_temperature');
if (!schema) {
accessory.log.warn('TemperatureSensor not supported.');
return;
}

const service = accessory.accessory.getService(accessory.Service.TemperatureSensor)
|| accessory.accessory.addService(accessory.Service.TemperatureSensor);

const property = schema.property as TuyaDeviceSchemaIntegerProperty;
const multiple = Math.pow(10, property ? property.scale : 0);
service.getCharacteristic(accessory.Characteristic.CurrentTemperature)
.onGet(() => {
const status = accessory.getStatus(schema.code)!;
// accessory.log.debug('CurrentTemperature:', 'property =', property, 'multiple =', multiple, 'status =', status);
return limit(status.value as number / multiple, -270, 100);
});

}
6 changes: 3 additions & 3 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ export class TuyaPlatform implements DynamicPlatformPlugin {
this.log.info(`Got home_id=${home_id}, name=${name}`);
if (this.options.homeWhitelist) {
if (this.options.homeWhitelist.includes(home_id)) {
this.log.info(`Found home_id=${home_id} in whitelist; including devices from this home.`);
homeIDList.push(home_id);
this.log.info(`Found home_id=${home_id} in whitelist; including devices from this home.`);
homeIDList.push(home_id);
} else {
this.log.info(`Did not find home_id=${home_id} in whitelist; excluding devices from this home.`);
this.log.info(`Did not find home_id=${home_id} in whitelist; excluding devices from this home.`);
}
} else {
homeIDList.push(home_id);
Expand Down

0 comments on commit 49420f7

Please sign in to comment.