@@ -4,27 +4,25 @@ import { StatusActiveAccessory } from './abstract/statusActive.js';
44
55import { strings } from '../i18n/i18n.js' ;
66
7+ import { CharacteristicKey } from '../model/enums.js' ;
78import { CharacteristicType , LockMechanismConfig , ServiceType } from '../model/types.js' ;
89
910import { Log } from '../tools/log.js' ;
1011
1112export 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 :
0 commit comments