|
1 | 1 | import { |
2 | | - Characteristic, CharacteristicProps, CharacteristicSetHandler, CharacteristicValue, |
| 2 | + Characteristic, CharacteristicProps, CharacteristicValue, |
3 | 3 | PartialAllowingNull, Perms, PrimitiveTypes, Service, |
4 | 4 | } from 'homebridge'; |
5 | 5 |
|
@@ -27,9 +27,6 @@ export type PublishHandler = (topic: string, value: PrimitiveTypes) => void; |
27 | 27 | type NumberCallback = (value: number) => void; |
28 | 28 | type BooleanCallback = (value: boolean) => void |
29 | 29 |
|
30 | | -const AVAILABILITY_KEY = 'Available'; |
31 | | -const HAP_COMMUNICATION_FAILURE = -70402; |
32 | | - |
33 | 30 | export abstract class Common<C extends Assertable> { |
34 | 31 |
|
35 | 32 | constructor( |
@@ -100,113 +97,6 @@ export abstract class Common<C extends Assertable> { |
100 | 97 | return Properties.set(this.identifier, key, value, persist || this.useStoredProperties); |
101 | 98 | } |
102 | 99 |
|
103 | | - protected get isAvailable(): boolean { |
104 | | - return Properties.get(this.identifier, AVAILABILITY_KEY) !== false; |
105 | | - } |
106 | | - |
107 | | - protected set isAvailable(value: boolean) { |
108 | | - |
109 | | - if (!Properties.set(this.identifier, AVAILABILITY_KEY, value, this.useStoredProperties)) { |
110 | | - return; |
111 | | - } |
112 | | - |
113 | | - if (value) { |
114 | | - this.logIfDesired(LogType.ALWAYS, strings.accessory.available); |
115 | | - } else { |
116 | | - this.logIfDesired(LogType.WARNING, strings.accessory.unavailable); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - protected setup( |
121 | | - characteristicKey: CharacteristicKey, defaultValue: CharacteristicValue, |
122 | | - getTopicKey: keyof C, onUpdateHandler: OnUpdateHandler, assertGetTopic: boolean, |
123 | | - setTopicKey: keyof C | undefined = undefined, onSetHandler: CharacteristicSetHandler | undefined = undefined, |
124 | | - ): Characteristic | undefined { |
125 | | - |
126 | | - const characteristic = this.setupGet(characteristicKey, defaultValue, getTopicKey, onUpdateHandler, assertGetTopic); |
127 | | - if (!characteristic) { |
128 | | - return; |
129 | | - } |
130 | | - |
131 | | - this.setupSet(characteristicKey, setTopicKey, onSetHandler); |
132 | | - |
133 | | - return characteristic; |
134 | | - } |
135 | | - |
136 | | - private setupGet(characteristicKey: CharacteristicKey, defaultValue: CharacteristicValue, |
137 | | - getTopicKey: keyof C, onUpdateHandler: OnUpdateHandler, assertTopic: boolean, |
138 | | - ): Characteristic | undefined { |
139 | | - |
140 | | - if (!getTopicKey.toString().startsWith('topic')) { |
141 | | - throw new Error(`Trying to fetch topic with unexpected property name '${getTopicKey.toString()}'`); |
142 | | - } |
143 | | - |
144 | | - if (assertTopic) { |
145 | | - this.assert(getTopicKey); |
146 | | - } |
147 | | - |
148 | | - if (this.config[getTopicKey] === undefined) { |
149 | | - for (const characteristic of this.service.characteristics) { |
150 | | - if (characteristic.UUID === this.characteristicFromKey(characteristicKey).UUID) { |
151 | | - this.service.removeCharacteristic(characteristic); |
152 | | - break; |
153 | | - } |
154 | | - } |
155 | | - return; |
156 | | - } |
157 | | - |
158 | | - if (this.isOptionalCharacteristic(characteristicKey)) { |
159 | | - this.service.addOptionalCharacteristic(this.characteristicFromKey(characteristicKey)); |
160 | | - } |
161 | | - |
162 | | - const characteristic = this.service.getCharacteristic(this.characteristicFromKey(characteristicKey)); |
163 | | - |
164 | | - const startingValue = !this.useStoredProperties ? defaultValue : (this.getProperty(characteristicKey) ?? defaultValue); |
165 | | - characteristic.setValue(startingValue); |
166 | | - |
167 | | - this.setProperty(characteristicKey, startingValue); |
168 | | - |
169 | | - characteristic.onGet( async (): Promise<CharacteristicValue> => { |
170 | | - if (!this.isAvailable) { |
171 | | - throw new this.HapStatusError(HAP_COMMUNICATION_FAILURE); |
172 | | - } |
173 | | - return this.getProperty(characteristicKey) ?? startingValue; |
174 | | - }); |
175 | | - |
176 | | - const onUpdateHandlerWrapper: OnUpdateHandler = async (topic, value) => { |
177 | | - this.isAvailable = true; |
178 | | - await onUpdateHandler(topic, value); |
179 | | - }; |
180 | | - |
181 | | - this.addTopicHandler({ topic: this.config[getTopicKey] as string, handler: onUpdateHandlerWrapper }); |
182 | | - |
183 | | - return characteristic; |
184 | | - } |
185 | | - |
186 | | - protected setupSet(characteristicKey: CharacteristicKey, |
187 | | - setTopicKey: keyof C | undefined = undefined, onSetHandler: CharacteristicSetHandler | undefined = undefined, |
188 | | - ) { |
189 | | - |
190 | | - if (setTopicKey === undefined) { |
191 | | - return; |
192 | | - } |
193 | | - |
194 | | - if (!setTopicKey.toString().startsWith('topic')) { |
195 | | - throw new Error(`Trying to fetch topic with unexpected property name '${setTopicKey.toString()}'`); |
196 | | - } |
197 | | - |
198 | | - if (!onSetHandler) { |
199 | | - throw new Error(`Missing onSetHandler for topic '${setTopicKey.toString()}'`); |
200 | | - } |
201 | | - |
202 | | - if (this.isOptionalCharacteristic(characteristicKey)) { |
203 | | - this.service.addOptionalCharacteristic(this.characteristicFromKey(characteristicKey)); |
204 | | - } |
205 | | - |
206 | | - const characteristic = this.service.getCharacteristic(this.characteristicFromKey(characteristicKey)); |
207 | | - characteristic.onSet(onSetHandler); |
208 | | - } |
209 | | - |
210 | 100 | protected setupTopicless(characteristicKey: CharacteristicKey, defaultValue: CharacteristicValue, |
211 | 101 | onSetCallback?: (value: CharacteristicValue, changed: boolean) => (void), props?: PartialAllowingNull<CharacteristicProps>): Characteristic | undefined { |
212 | 102 |
|
|
0 commit comments