-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b46c9c
commit 3561e0c
Showing
3 changed files
with
122 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
AccessoryPlugin, | ||
CharacteristicValue, | ||
Service, | ||
Nullable, | ||
} from 'homebridge'; | ||
|
||
import { SmartHomeNGPlatform } from '../platform'; | ||
|
||
export class HumiditySensor implements AccessoryPlugin { | ||
private readonly deviceService: Service; | ||
private readonly informationService: Service; | ||
|
||
public name: string; | ||
private currentHumidity = 0; | ||
|
||
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { | ||
this.name = accessory.name; | ||
this.deviceService = new this.platform.Service.HumiditySensor(accessory.name); | ||
|
||
// create handlers for required characteristics | ||
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity) | ||
.onGet(this.getCurrentHumidity.bind(this)); | ||
|
||
this.informationService = | ||
new this.platform.Service.AccessoryInformation() | ||
.setCharacteristic(this.platform.Characteristic.Manufacturer, accessory.manufacturer) | ||
.setCharacteristic(this.platform.Characteristic.Model, accessory.model) | ||
.setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.currenthumidity); | ||
|
||
this.platform.shng.addMonitor(accessory.currenthumidity, this.shngCallback.bind(this)); | ||
this.platform.log.info('HumiditySensor', accessory.name, 'created!'); | ||
} | ||
|
||
identify(): void { | ||
this.platform.log.info('Identify!'); | ||
} | ||
|
||
getServices(): Service[] { | ||
return [this.informationService, this.deviceService]; | ||
} | ||
|
||
getCurrentHumidity(): Nullable<CharacteristicValue> { | ||
this.platform.log.debug('getCurrentHumidity:', this.accessory.name, '=', this.currentHumidity); | ||
return this.currentHumidity; | ||
} | ||
|
||
shngCallback(value: unknown): void { | ||
this.platform.log.debug('shngCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); | ||
if (typeof value === 'number') { | ||
this.currentHumidity = value; | ||
} else { | ||
this.platform.log.warn('Unknown type', typeof value, 'received for', this.accessory.name + ':', value); | ||
} | ||
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.currentHumidity); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters