@@ -5,7 +5,7 @@ import { EveCharacteristic, isEveCharacteristic } from '../characteristic/eve.js
55import { strings } from '../../i18n/i18n.js' ;
66
77import { CharacteristicKey , TimeUnits } from '../../model/enums.js' ;
8- import { CharacteristicType , TemperatureConfig , TimeoutConfig } from '../../model/types.js' ;
8+ import { CharacteristicType , HapStatusErrorType , TemperatureConfig , TimeoutConfig } from '../../model/types.js' ;
99
1010import { debounce } from '../../tools/debounce.js' ;
1111import { Log , LogType } from '../../tools/log.js' ;
@@ -23,6 +23,9 @@ export type PublishHandler = (topic: string, value: PrimitiveTypes) => void;
2323type NumberCallback = ( value : number ) => void ;
2424type BooleanCallback = ( value : boolean ) => void
2525
26+ const AVAILABILITY_KEY = 'Available' ;
27+ const HAP_COMMUNICATION_FAILURE = - 70402 ;
28+
2629export abstract class Common < C extends Assertable > {
2730
2831 public readonly topicHandlers : TopicHandler [ ] = [ ] ;
@@ -33,6 +36,7 @@ export abstract class Common<C extends Assertable> {
3336
3437 protected abstract get service ( ) : Service ;
3538 protected abstract get Characteristic ( ) : CharacteristicType ;
39+ protected abstract get HapStatusError ( ) : HapStatusErrorType ;
3640
3741 protected abstract get log ( ) : Log ;
3842 protected abstract get disableLogging ( ) : boolean ;
@@ -93,6 +97,23 @@ export abstract class Common<C extends Assertable> {
9397 Properties . set ( this . identifier , key , value ) ;
9498 }
9599
100+ protected get isAvailable ( ) : boolean {
101+ return Properties . get ( this . identifier , AVAILABILITY_KEY ) !== false ;
102+ }
103+
104+ protected set isAvailable ( value : boolean ) {
105+
106+ if ( ! Properties . set ( this . identifier , AVAILABILITY_KEY , value ) ) {
107+ return ;
108+ }
109+
110+ if ( value ) {
111+ this . logIfDesired ( LogType . ALWAYS , strings . accessory . available ) ;
112+ } else {
113+ this . logIfDesired ( LogType . WARNING , strings . accessory . unavailable ) ;
114+ }
115+ }
116+
96117 protected setup (
97118 characteristicKey : CharacteristicKey , defaultValue : CharacteristicValue ,
98119 getTopicKey : keyof C , onUpdateHandler : OnUpdateHandler , assertGetTopic : boolean ,
@@ -143,10 +164,18 @@ export abstract class Common<C extends Assertable> {
143164 this . setProperty ( characteristicKey , startingValue ) ;
144165
145166 characteristic . onGet ( async ( ) : Promise < Nullable < CharacteristicValue > > => {
167+ if ( ! this . isAvailable ) {
168+ throw new this . HapStatusError ( HAP_COMMUNICATION_FAILURE ) ;
169+ }
146170 return this . getProperty ( characteristicKey ) ?? null ;
147171 } ) ;
148172
149- this . topicHandlers . push ( { topic : this . config [ getTopicKey ] as string , handler : onUpdateHandler } ) ;
173+ const onUpdateHandlerWrapper : OnUpdateHandler = async ( topic , value ) => {
174+ this . isAvailable = true ;
175+ await onUpdateHandler ( topic , value ) ;
176+ } ;
177+
178+ this . topicHandlers . push ( { topic : this . config [ getTopicKey ] as string , handler : onUpdateHandlerWrapper } ) ;
150179
151180 return characteristic ;
152181 }
0 commit comments