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

Fix switch accessory & add support for Smart IR (wnykq) #115

Merged
merged 3 commits into from
Nov 24, 2022
Merged
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: 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);
0x5e marked this conversation as resolved.
Show resolved Hide resolved
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.`);
Copy link
Owner

Choose a reason for hiding this comment

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

I've noticed this and I don't know why npm run lint not return warnings for me and github actions, but ./node_modules/.bin/eslint src/**/**.ts --max-warnings=0 works. Do you know why?

Copy link
Author

Choose a reason for hiding this comment

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

In my case, using eslint directly also works:

❯ ./node_modules/.bin/eslint src/**/**.ts --max-warnings=0
❯ echo $?
0

Do you have some additional global config specified? Or some prettier rules if you are using VSCode?

Copy link
Author

Choose a reason for hiding this comment

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

Shouldnt it be

eslint src/**/*.ts --max-warnings=0

? with one asterisk before .ts

Besides this, no clue why it wouldnt return errors in actions.

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