Skip to content

Commit

Permalink
Update naming logic of Dimmer, Switch, Valve. (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e authored Nov 22, 2022
1 parent c765f74 commit 046f7bd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 81 deletions.
12 changes: 2 additions & 10 deletions src/accessory/BaseAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { PlatformAccessory, Service, Characteristic } from 'homebridge';
import { debounce } from 'debounce';

import { TuyaDeviceSchema, TuyaDeviceStatus } from '../device/TuyaDevice';
import { TuyaDeviceStatus } from '../device/TuyaDevice';
import { TuyaPlatform } from '../platform';
import { limit } from '../util/util';
import { PrefixLogger } from '../util/Logger';
Expand Down Expand Up @@ -32,12 +32,7 @@ export default class BaseAccessory {
this.addAccessoryInfoService();
this.addBatteryService();

for (const schema of this.device.schema) {
this.configureService(schema);
}

this.onDeviceStatusUpdate(this.device.status);

}

addAccessoryInfoService() {
Expand All @@ -58,7 +53,7 @@ export default class BaseAccessory {
return;
}

const { BATTERY_LEVEL_NORMAL, BATTERY_LEVEL_LOW} = this.Characteristic.StatusLowBattery;
const { BATTERY_LEVEL_NORMAL, BATTERY_LEVEL_LOW } = this.Characteristic.StatusLowBattery;
const service = this.accessory.getService(this.Service.Battery)
|| this.accessory.addService(this.Service.Battery);

Expand Down Expand Up @@ -176,9 +171,6 @@ export default class BaseAccessory {
return [];
}

configureService(schema: TuyaDeviceSchema) {

}

async onDeviceInfoUpdate(info) {
// name, online, ...
Expand Down
63 changes: 20 additions & 43 deletions src/accessory/DimmerAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,37 @@ export default class DimmerAccessory extends BaseAccessory {
) {
super(platform, accessory);

this.configure();
}

requiredSchema() {
return [SCHEMA_CODE.ON, SCHEMA_CODE.BRIGHTNESS];
}

getOnSchema(index: number) {
if (index === 0) {
return this.getSchema('switch', 'switch_led');
}
return this.getSchema(`switch_${index}`, `switch_led_${index}`);
}

getBrightnessSchema(index: number) {
if (index === 0) {
return this.getSchema('bright_value');
}
return this.getSchema(`bright_value_${index}`);
}


configure() {

const oldService = this.accessory.getService(this.Service.Lightbulb);
if (oldService && oldService?.subtype === undefined) {
this.platform.log.warn('Remove old service:', oldService.UUID);
this.accessory.removeService(oldService);
}

for (let index = 0; index <= 3; index++) {
const schema = this.getBrightnessSchema(index);
if (!schema) {
continue;
}

const name = (index === 0) ? this.device.name : `${this.device.name} - ${index}`;
const schema = this.device.schema.filter((schema) => schema.code.startsWith('bright_value'));
for (const _schema of schema) {
const suffix = _schema.code.replace('bright_value', '');
const name = (schema.length === 1) ? this.device.name : _schema.code;

const service = this.accessory.getService(schema.code)
|| this.accessory.addService(this.Service.Lightbulb, name, schema.code);
const service = this.accessory.getService(_schema.code)
|| this.accessory.addService(this.Service.Lightbulb, name, _schema.code);

service.setCharacteristic(this.Characteristic.Name, name);
if (!service.testCharacteristic(this.Characteristic.ConfiguredName)) {
service.addOptionalCharacteristic(this.Characteristic.ConfiguredName); // silence warning
service.setCharacteristic(this.Characteristic.ConfiguredName, name);
}

this.configureOn(service, index);
this.configureBrightness(service, index);
this.configureOn(service, suffix);
this.configureBrightness(service, suffix);
}
}

configureOn(service: Service, index: number) {
const schema = this.getOnSchema(index);
requiredSchema() {
return [SCHEMA_CODE.ON, SCHEMA_CODE.BRIGHTNESS];
}

configureOn(service: Service, suffix: string) {
const schema = this.getSchema('switch' + suffix, 'switch_led' + suffix);
if (!schema) {
return;
}
Expand All @@ -86,8 +63,8 @@ export default class DimmerAccessory extends BaseAccessory {
});
}

configureBrightness(service: Service, index: number) {
const schema = this.getBrightnessSchema(index);
configureBrightness(service: Service, suffix: string) {
const schema = this.getSchema('bright_value' + suffix);
if (!schema) {
return;
}
Expand All @@ -100,8 +77,8 @@ export default class DimmerAccessory extends BaseAccessory {
minStep: 1,
};

const minStatus = this.getStatus(`brightness_min_${index}`);
const maxStatus = this.getStatus(`brightness_max_${index}`);
const minStatus = this.getStatus('brightness_min' + suffix);
const maxStatus = this.getStatus('brightness_max' + suffix);
if (minStatus && maxStatus && maxStatus.value > minStatus.value) {
const minValue = Math.ceil(remap(minStatus.value as number, 0, range, 0, 100));
const maxValue = Math.floor(remap(maxStatus.value as number, 0, range, 0, 100));
Expand Down Expand Up @@ -135,8 +112,8 @@ export default class DimmerAccessory extends BaseAccessory {
// brightness range updated
if (status.length !== this.device.status.length) {
for (const _status of status) {
if (!_status.code.startsWith('brightness_min_')
&& !_status.code.startsWith('brightness_max_')) {
if (!_status.code.startsWith('brightness_min')
&& !_status.code.startsWith('brightness_max')) {
continue;
}

Expand Down
36 changes: 24 additions & 12 deletions src/accessory/SwitchAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaDeviceSchema, TuyaDeviceSchemaType } from '../device/TuyaDevice';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';

const SCHEMA_CODE = {
Expand All @@ -7,6 +9,25 @@ const SCHEMA_CODE = {

export default class SwitchAccessory extends BaseAccessory {

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

const oldService = this.accessory.getService(this.mainService());
if (oldService && oldService?.subtype === undefined) {
this.platform.log.warn('Remove old service:', oldService.UUID);
this.accessory.removeService(oldService);
}

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);
}
}

requiredSchema() {
return [SCHEMA_CODE.ON];
}
Expand All @@ -15,16 +36,7 @@ export default class SwitchAccessory extends BaseAccessory {
return this.Service.Switch;
}

configureService(schema: TuyaDeviceSchema) {
if (!schema.code.startsWith('switch')
|| schema.type !== TuyaDeviceSchemaType.Boolean) {
return;
}

let name = this.device.name;
if (schema.code !== 'switch') {
name += ` - ${schema.code.replace('switch_', '')}`;
}
configureSwitch(schema: TuyaDeviceSchema, name: string) {

const service = this.accessory.getService(schema.code)
|| this.accessory.addService(this.mainService(), name, schema.code);
Expand All @@ -37,8 +49,8 @@ export default class SwitchAccessory extends BaseAccessory {

service.getCharacteristic(this.Characteristic.On)
.onGet(() => {
const status = this.getStatus(schema.code);
return status!.value as boolean;
const status = this.getStatus(schema.code)!;
return status.value as boolean;
})
.onSet((value) => {
this.sendCommands([{
Expand Down
43 changes: 28 additions & 15 deletions src/accessory/ValveAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaDeviceSchema, TuyaDeviceSchemaType } from '../device/TuyaDevice';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';

const SCHEMA_CODE = {
Expand All @@ -7,20 +9,30 @@ const SCHEMA_CODE = {

export default class ValveAccessory extends BaseAccessory {

requiredSchema() {
return [SCHEMA_CODE.ON];
}
constructor(
public readonly platform: TuyaPlatform,
public readonly accessory: PlatformAccessory,
) {
super(platform, accessory);

configureService(schema: TuyaDeviceSchema) {
if (!schema.code.startsWith('switch')
|| schema.type !== TuyaDeviceSchemaType.Boolean) {
return;
const oldService = this.accessory.getService(this.Service.Valve);
if (oldService && oldService?.subtype === undefined) {
this.platform.log.warn('Remove old service:', oldService.UUID);
this.accessory.removeService(oldService);
}

let name = this.device.name;
if (schema.code !== 'switch') {
name += ` - ${schema.code.replace('switch_', '')}`;
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.configureValve(_schema, name);
}
}

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

configureValve(schema: TuyaDeviceSchema, name: string) {

const service = this.accessory.getService(schema.code)
|| this.accessory.addService(this.Service.Valve, name, schema.code);
Expand All @@ -34,19 +46,20 @@ export default class ValveAccessory extends BaseAccessory {

service.getCharacteristic(this.Characteristic.InUse)
.onGet(() => {
const status = this.getStatus(schema.code);
return status?.value as boolean;
const status = this.getStatus(schema.code)!;
return status.value as boolean;
});

const { INACTIVE, ACTIVE } = this.Characteristic.Active;
service.getCharacteristic(this.Characteristic.Active)
.onGet(() => {
const status = this.getStatus(schema.code);
return status?.value as boolean;
const status = this.getStatus(schema.code)!;
return (status.value as boolean) ? ACTIVE : INACTIVE;
})
.onSet(value => {
this.sendCommands([{
code: schema.code,
value: (value as number === 1) ? true : false,
value: (value as number === ACTIVE) ? true : false,
}]);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/device/TuyaDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default class TuyaDevice {

constructor(obj: Partial<TuyaDevice>) {
Object.assign(this, obj);
this.status.sort((a, b) => a.code > b.code ? 1 : -1);
}

}
3 changes: 2 additions & 1 deletion src/device/TuyaDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export default class TuyaDeviceManager extends EventEmitter {
this.log.error(error);
}
}
return Object.values(schemas) as TuyaDeviceSchema[];

return Object.values(schemas).sort((a, b) => a.code > b.code ? 1 : -1) as TuyaDeviceSchema[];
}


Expand Down

0 comments on commit 046f7bd

Please sign in to comment.