Skip to content

Commit f930ff4

Browse files
committed
Use onUpdate and onSet from base
1 parent cbcbb9d commit f930ff4

7 files changed

Lines changed: 104 additions & 186 deletions

File tree

src/accessory/abstract/base.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export abstract class MQTTAccessory<C extends AccessoryConfig> {
8484
return this.config.info.name;
8585
}
8686

87-
protected getRawValue(property :keyof C, assert: boolean = true): string | undefined {
87+
protected getRawValue(property: keyof C, assert: boolean = true): string | undefined {
8888

8989
if (!property.toString().startsWith('value')) {
9090
throw new Error(`Trying to fetch value with unexpected property name '${property.toString()}'`);
@@ -122,17 +122,21 @@ export abstract class MQTTAccessory<C extends AccessoryConfig> {
122122
return assert(this.log, this.name, this.config, ...keys);
123123
}
124124

125-
protected async onUpdate(key: CharacteristicKey, value: PrimitiveTypes, logString: string) {
125+
protected onUpdate(key: CharacteristicKey, value: CharacteristicValue, logString: string | undefined = undefined): boolean {
126126

127127
if (value === this.get(key)) {
128-
return;
128+
return false;
129129
}
130130

131131
this.set(key, value);
132132

133133
this.accessoryService.updateCharacteristic(this.Characteristic[key], value);
134134

135-
this.logIfDesired(logString, value.toString());
135+
if (logString) {
136+
this.logIfDesired(logString, value.toString());
137+
}
138+
139+
return true;
136140
}
137141

138142
protected onSet(key: CharacteristicKey, value: CharacteristicValue, topic: keyof C, logString: string) {
@@ -141,9 +145,11 @@ export abstract class MQTTAccessory<C extends AccessoryConfig> {
141145
return;
142146
}
143147

144-
this.set(key, value);
148+
if (value !== this.get(key)) {
149+
this.logIfDesired(logString, value.toString());
150+
}
145151

146-
this.logIfDesired(logString, value.toString());
152+
this.set(key, value);
147153

148154
this.accessoryService.updateCharacteristic(this.Characteristic[key], value);
149155

src/accessory/abstract/statusActive.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import { MQTTAccessory } from './base.js';
44

55
import { strings } from '../../i18n/i18n.js';
66

7+
import { CharacteristicKey } from '../../model/enums.js';
78
import { CharacteristicType, StatusActiveConfig, ServiceType } from '../../model/types.js';
89

910
import { Log } from '../../tools/log.js';
1011

1112
export abstract class StatusActiveAccessory<C extends StatusActiveConfig = StatusActiveConfig> extends MQTTAccessory<C> {
1213

13-
private statusActive: CharacteristicValue = true;
14-
1514
constructor(Service: ServiceType, Characteristic: CharacteristicType, accessory: PlatformAccessory, config: C, log: Log, className: string) {
1615
super(Service, Characteristic, accessory, config, log, className);
1716

17+
this.set(CharacteristicKey.StatusActive, true);
18+
1819
this.accessoryService.getCharacteristic(Characteristic.StatusActive)
1920
.onGet(this.getStatusActive.bind(this));
2021
}
@@ -23,24 +24,21 @@ export abstract class StatusActiveAccessory<C extends StatusActiveConfig = Statu
2324
this.addTopicHandler('topicGetStatusActive', this.onStatusActiveUpdate.bind(this), false);
2425
}
2526

27+
private async getStatusActive(): Promise<CharacteristicValue> {
28+
return this.get(CharacteristicKey.StatusActive);
29+
}
30+
2631
private async onStatusActiveUpdate(topic: string, value: PrimitiveTypes): Promise<void> {
2732

2833
const statusActive = value === this.getPrimitiveValue('valueStatusActive');
29-
if (statusActive === this.statusActive) {
34+
if (!this.onUpdate(CharacteristicKey.StatusActive, statusActive)) {
3035
return;
3136
}
3237

33-
this.statusActive = statusActive;
34-
this.accessoryService.updateCharacteristic(this.Characteristic.StatusActive, this.statusActive);
35-
36-
if (this.statusActive) {
38+
if (statusActive) {
3739
this.logIfDesired(strings.accessory.statusActive);
3840
} else {
3941
this.log.warning(strings.accessory.statusInactive, this.name);
4042
}
4143
}
42-
43-
private async getStatusActive(): Promise<CharacteristicValue> {
44-
return this.statusActive;
45-
}
4644
}

src/accessory/lock.ts

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@ import { StatusActiveAccessory } from './abstract/statusActive.js';
44

55
import { strings } from '../i18n/i18n.js';
66

7+
import { CharacteristicKey } from '../model/enums.js';
78
import { CharacteristicType, LockMechanismConfig, ServiceType } from '../model/types.js';
89

910
import { Log } from '../tools/log.js';
1011

1112
export class LockMechanismAccessory extends StatusActiveAccessory<LockMechanismConfig> {
1213

13-
private currentState: CharacteristicValue;
14-
private targetState: CharacteristicValue;
15-
1614
constructor(Service: ServiceType, Characteristic: CharacteristicType, accessory: PlatformAccessory, config: LockMechanismConfig, log: Log) {
1715
super(Service, Characteristic, accessory, config, log, LockMechanismAccessory.name);
1816

19-
this.currentState = this.Characteristic.LockCurrentState.UNKNOWN;
20-
this.targetState = this.Characteristic.LockTargetState.SECURED;
17+
this.set(CharacteristicKey.LockCurrentState, Characteristic.LockCurrentState.UNKNOWN);
18+
this.set(CharacteristicKey.LockTargetState, Characteristic.LockTargetState.SECURED);
2119

2220
this.accessoryService.getCharacteristic(this.Characteristic.LockCurrentState)
2321
.onGet(this.getCurrentState.bind(this));
2422

2523
this.accessoryService.getCharacteristic(this.Characteristic.LockTargetState)
2624
.onGet(this.getTargetState.bind(this))
27-
.onSet(this.setTargetState.bind(this));
25+
.onSet(this.onSetTargetState.bind(this));
2826
}
2927

3028
protected getAccessoryService(): Service {
@@ -38,79 +36,42 @@ export class LockMechanismAccessory extends StatusActiveAccessory<LockMechanismC
3836
}
3937

4038
private async getCurrentState(): Promise<CharacteristicValue> {
41-
return this.currentState;
39+
return this.get(CharacteristicKey.LockCurrentState);
4240
}
4341

4442
private async getTargetState(): Promise<CharacteristicValue> {
45-
return this.targetState;
43+
return this.get(CharacteristicKey.LockTargetState);
4644
}
4745

4846
private async onCurrentStateUpdate(topic: string, value: PrimitiveTypes): Promise<void> {
4947

5048
const current = this.currentStateFromValue(value);
51-
if (current === this.currentState) {
49+
this.onUpdate(CharacteristicKey.LockTargetState, current);
50+
51+
if (!this.onUpdate(CharacteristicKey.LockCurrentState, current)) {
5252
return;
5353
}
5454

55-
this.currentState = current;
56-
this.accessoryService.updateCharacteristic(this.Characteristic.LockCurrentState, this.currentState);
57-
58-
this.targetState = this.currentState;
59-
this.accessoryService.updateCharacteristic(this.Characteristic.LockTargetState, this.targetState);
60-
61-
if (this.currentState === this.Characteristic.LockCurrentState.JAMMED) {
62-
this.log.error(this.stringForState(this.currentState), this.name);
55+
if (current === this.Characteristic.LockCurrentState.JAMMED) {
56+
this.log.error(this.stringForState(current), this.name);
6357
} else {
64-
this.logIfDesired(this.stringForState(this.currentState));
58+
this.logIfDesired(this.stringForState(current));
6559
}
6660
}
6761

6862
private async onTargetStateUpdate(topic: string, value: PrimitiveTypes): Promise<void> {
69-
7063
const target = this.targetStateFromValue(value);
71-
if (target === this.targetState) {
72-
return;
73-
}
74-
75-
this.targetState = target;
76-
this.accessoryService.updateCharacteristic(this.Characteristic.LockTargetState, this.targetState);
77-
78-
this.logIfDesired(this.stringForState(this.targetState, true));
64+
this.onUpdate(CharacteristicKey.LockTargetState, target, this.stringForState(target, true));
7965
}
8066

81-
private async setTargetState(value: CharacteristicValue) {
82-
83-
if (!this.assert('topicSetTargetState')) {
84-
return;
85-
}
67+
private async onSetTargetState(value: CharacteristicValue) {
8668

8769
const target = this.valueFromTargetState(value);
8870
if (target === undefined) {
89-
this.log.error(strings.lock.badTarget, this.name, value);
9071
return;
9172
}
9273

93-
if (this.targetState !== value) {
94-
this.logIfDesired(this.stringForState(value, true));
95-
}
96-
97-
this.targetState = value;
98-
99-
this.accessoryService.updateCharacteristic(this.Characteristic.LockTargetState, this.targetState);
100-
101-
this.publish(this.config.topicSetTargetState, target);
102-
}
103-
104-
private valueFromTargetState(value: CharacteristicValue): PrimitiveTypes | undefined {
105-
106-
switch (value) {
107-
case this.Characteristic.LockTargetState.SECURED:
108-
return this.getPrimitiveValue('valueLockStateSecured');
109-
case this.Characteristic.LockTargetState.UNSECURED:
110-
return this.getPrimitiveValue('valueLockStateUnsecured');
111-
default:
112-
return undefined;
113-
}
74+
this.onSet(CharacteristicKey.LockTargetState, target, 'topicSetTargetState', this.stringForState(value, true));
11475
}
11576

11677
private currentStateFromValue(value: PrimitiveTypes | undefined): CharacteristicValue {
@@ -146,6 +107,18 @@ export class LockMechanismAccessory extends StatusActiveAccessory<LockMechanismC
146107
}
147108
}
148109

110+
private valueFromTargetState(value: CharacteristicValue): PrimitiveTypes | undefined {
111+
switch (value) {
112+
case this.Characteristic.LockTargetState.SECURED:
113+
return this.getPrimitiveValue('valueLockStateSecured');
114+
case this.Characteristic.LockTargetState.UNSECURED:
115+
return this.getPrimitiveValue('valueLockStateUnsecured');
116+
default:
117+
this.log.error(strings.lock.badTarget, this.name, value);
118+
return undefined;
119+
}
120+
}
121+
149122
private stringForState(state: CharacteristicValue, future: boolean = false): string {
150123
switch(state) {
151124
case this.Characteristic.LockCurrentState.SECURED:

src/accessory/onoff/onoff.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,42 @@ import { StatusActiveAccessory } from '../abstract/statusActive.js';
44

55
import { strings } from '../../i18n/i18n.js';
66

7+
import { CharacteristicKey } from '../../model/enums.js';
78
import { CharacteristicType, OnOffConfig, ServiceType } from '../../model/types.js';
89

910
import { Log } from '../../tools/log.js';
1011

1112
export abstract class OnOffAccessory<C extends OnOffConfig = OnOffConfig> extends StatusActiveAccessory<C> {
1213

13-
private on: CharacteristicValue = false;
14-
1514
constructor(Service: ServiceType, Characteristic: CharacteristicType, accessory: PlatformAccessory, config: C, log: Log, className: string) {
1615
super(Service, Characteristic, accessory, config, log, className);
1716

17+
this.set(CharacteristicKey.On, false);
18+
1819
this.accessoryService.getCharacteristic(Characteristic.On)
1920
.onGet(this.getOn.bind(this))
20-
.onSet(this.setOn.bind(this));
21+
.onSet(this.onSetOn.bind(this));
2122
}
2223

2324
override addTopicHandlers(): void {
2425
super.addTopicHandlers();
2526
this.addTopicHandler('topicGetOn', this.onOnUpdate.bind(this));
2627
}
2728

29+
private async getOn(): Promise<CharacteristicValue> {
30+
return this.get(CharacteristicKey.On);
31+
}
2832

2933
private async onOnUpdate(topic: string, value: PrimitiveTypes): Promise<void> {
3034
const on = value === this.getPrimitiveValue('valueOn');
31-
if (on === this.on) {
32-
return;
33-
}
34-
35-
this.on = on;
36-
this.accessoryService.updateCharacteristic(this.Characteristic.On, this.on);
37-
38-
this.logIfDesired(this.stringForState(this.on));
39-
}
40-
41-
private async getOn(): Promise<CharacteristicValue> {
42-
return this.on;
35+
this.onUpdate(CharacteristicKey.On, on, this.stringForState(on));
4336
}
4437

45-
private async setOn(value: CharacteristicValue) {
46-
47-
if (!this.assert('topicSetOn')) {
48-
return;
49-
}
50-
38+
private async onSetOn(value: CharacteristicValue) {
5139
const on = value ? this.getRawValue('valueOn') : this.getRawValue('valueOff');
52-
53-
this.on = value;
54-
55-
this.logIfDesired(this.stringForState(this.on, true));
56-
57-
this.accessoryService.updateCharacteristic(this.Characteristic.On, this.on);
58-
59-
this.publish(this.config.topicSetOn, on);
40+
if (on !== undefined) {
41+
this.onSet(CharacteristicKey.On, on, 'topicSetOn', this.stringForState(on, true));
42+
}
6043
}
6144

6245
private stringForState(on: CharacteristicValue, future: boolean = false): string {

src/accessory/onoff/outlet.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import { OnOffAccessory } from './onoff.js';
44

55
import { strings } from '../../i18n/i18n.js';
66

7+
import { CharacteristicKey } from '../../model/enums.js';
78
import { CharacteristicType, OutletConfig, ServiceType } from '../../model/types.js';
89

910
import { Log } from '../../tools/log.js';
1011

1112
export class OutletAccessory extends OnOffAccessory<OutletConfig> {
1213

13-
private inUse: CharacteristicValue = false;
14-
15-
1614
constructor(Service: ServiceType, Characteristic: CharacteristicType, accessory: PlatformAccessory, config: OutletConfig, log: Log) {
1715
super(Service, Characteristic, accessory, config, log, OutletAccessory.name);
1816

17+
this.set(CharacteristicKey.OutletInUse, false);
18+
1919
this.accessoryService.getCharacteristic(this.Characteristic.OutletInUse)
2020
.onGet(this.getInUse.bind(this))
21-
.onSet(this.setInUse.bind(this));
21+
.onSet(this.onSetInUse.bind(this));
2222
}
2323

2424
protected getAccessoryService(): Service {
@@ -31,40 +31,22 @@ export class OutletAccessory extends OnOffAccessory<OutletConfig> {
3131
}
3232

3333
private async getInUse(): Promise<CharacteristicValue> {
34-
return this.inUse;
34+
return this.get(CharacteristicKey.OutletInUse);
3535
}
3636

3737
private async onInUseUpdate(topic: string, value: PrimitiveTypes): Promise<void> {
38-
3938
const inUse = value === this.getPrimitiveValue('valueOutletInUse');
40-
if (inUse === this.inUse) {
41-
return;
42-
}
43-
44-
this.inUse = inUse;
45-
this.accessoryService.updateCharacteristic(this.Characteristic.OutletInUse, this.inUse);
46-
47-
this.logIfDesired(this.stringForInUse(this.inUse));
39+
this.onUpdate(CharacteristicKey.OutletInUse, inUse, this.stringForInUse(inUse));
4840
}
4941

50-
private async setInUse(value: CharacteristicValue) {
51-
52-
if (!this.assert('topicSetOutletInUse')) {
53-
return;
54-
}
42+
private async onSetInUse(value: CharacteristicValue) {
5543

5644
const inUse = value ? this.getRawValue('valueOutletInUse') : this.getRawValue('valueOutletNotInUse');
5745
if (!inUse) {
5846
return;
5947
}
6048

61-
this.inUse = value;
62-
63-
this.logIfDesired(this.stringForInUse(this.inUse, true));
64-
65-
this.accessoryService.updateCharacteristic(this.Characteristic.OutletInUse, this.inUse);
66-
67-
this.publish(this.config.topicSetOutletInUse!, inUse!);
49+
this.onSet(CharacteristicKey.OutletInUse, inUse,'topicSetOutletInUse', this.stringForInUse(inUse, true));
6850
}
6951

7052
private stringForInUse(inUse: CharacteristicValue, future: boolean = false): string {

0 commit comments

Comments
 (0)