-
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.
Merge pull request #13 from chester4444/master
Added GarageDoor and HumiditySensor
- Loading branch information
Showing
6 changed files
with
268 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,135 @@ | ||
import { | ||
AccessoryPlugin, | ||
CharacteristicValue, | ||
Service, | ||
Nullable, | ||
} from 'homebridge'; | ||
|
||
import { SmartHomeNGPlatform } from '../platform'; | ||
|
||
export class GarageDoor implements AccessoryPlugin { | ||
private readonly deviceService: Service; | ||
private readonly informationService: Service; | ||
|
||
public name: string; | ||
private targetDoorState = this.platform.Characteristic.CurrentDoorState.CLOSED; | ||
private currentDoorState = this.platform.Characteristic.CurrentDoorState.STOPPED; | ||
private obstructionDetected = false; | ||
|
||
constructor(private readonly platform: SmartHomeNGPlatform, private readonly accessory) { | ||
this.name = accessory.name; | ||
this.deviceService = new this.platform.Service.GarageDoorOpener(accessory.name); | ||
|
||
// create handlers for required characteristics | ||
|
||
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.currentdoorstate); // FIXME | ||
|
||
if (accessory.currentdoorstate) { | ||
this.platform.shng.addMonitor(accessory.currentdoorstate, this.shngCurrentDoorStateCallback.bind(this)); | ||
this.deviceService.getCharacteristic(this.platform.Characteristic.CurrentDoorState) | ||
.onGet(this.getCurrentDoorState.bind(this)) | ||
.onSet(this.setCurrentDoorState.bind(this)); | ||
} else { | ||
this.platform.log.error('GarageDoor: missing \"currentdoorstate\" in config.json!'); | ||
} | ||
|
||
if (accessory.targetdoorstate) { | ||
this.platform.shng.addMonitor(accessory.targetdoorstate, this.shngTargetDoorStateCallback.bind(this)); | ||
this.deviceService.getCharacteristic(this.platform.Characteristic.TargetDoorState) | ||
.onGet(this.getTargetDoorState.bind(this)) | ||
.onSet(this.setTargetDoorState.bind(this)); | ||
} else { | ||
this.platform.log.error('GarageDoor: missing \"targetdoorstate\" in config.json!'); | ||
} | ||
|
||
if (accessory.obstructiondetected) { | ||
this.platform.shng.addMonitor(accessory.obstructiondetected, this.shngObstructionDetectedCallback.bind(this)); | ||
this.deviceService.getCharacteristic(this.platform.Characteristic.ObstructionDetected) | ||
.onGet(this.getObstructionDetected.bind(this)) | ||
.onSet(this.setObstructionDetected.bind(this)); | ||
|
||
} | ||
|
||
this.platform.log.info("GarageDoor '%s' created!", accessory.name); | ||
} | ||
|
||
identify(): void { | ||
this.platform.log.info('Identify!'); | ||
} | ||
|
||
getServices(): Service[] { | ||
return [this.informationService, this.deviceService]; | ||
} | ||
|
||
getCurrentDoorState(): Nullable<CharacteristicValue> { | ||
//getCurrentDoorState(value: CharacteristicValue) { | ||
// this.currentDoorState = value as number; | ||
this.platform.log.info('getCurrentDoorState:', this.accessory.name, 'is currently', this.currentDoorState); | ||
return this.currentDoorState; | ||
} | ||
|
||
setCurrentDoorState(value: CharacteristicValue) { | ||
this.currentDoorState = value as number; | ||
this.platform.log.info('setCurrentDoorState:', this.accessory.name, 'was set to', this.currentDoorState); | ||
this.platform.shng.setItem(this.accessory.currentdoorstate, this.currentDoorState); | ||
} | ||
|
||
getTargetDoorState(): Nullable<CharacteristicValue> { | ||
//getTargetDoorState(value: CharacteristicValue) { | ||
// this.targetDoorState = value as number; | ||
this.platform.log.info('getTargetDoorState:', this.accessory.name, 'is currently', this.targetDoorState); | ||
return this.targetDoorState; | ||
} | ||
|
||
setTargetDoorState(value: CharacteristicValue) { | ||
this.targetDoorState = value as number; | ||
this.platform.log.info('setTargetDoorState:', this.accessory.name, 'was set to', this.targetDoorState); | ||
this.platform.shng.setItem(this.accessory.targetdoorstate, this.targetDoorState); | ||
} | ||
|
||
getObstructionDetected(): Nullable<CharacteristicValue> { | ||
this.platform.log.info('getObstructionDetected:', this.accessory.name, 'is currently', this.obstructionDetected); | ||
return this.obstructionDetected; | ||
} | ||
|
||
setObstructionDetected(value: CharacteristicValue) { | ||
this.obstructionDetected = value as boolean; | ||
this.platform.log.info('setObstructionDetected:', this.accessory.name, 'was set to', this.obstructionDetected); | ||
this.platform.shng.setItem(this.accessory.obstructiondetected, this.obstructionDetected); | ||
} | ||
|
||
shngCurrentDoorStateCallback(value: unknown): void { | ||
this.platform.log.debug('shngCurrentDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); | ||
if (typeof value === 'number') { | ||
this.currentDoorState = value; | ||
this.deviceService.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, this.currentDoorState); | ||
|
||
} else { | ||
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); | ||
} | ||
} | ||
|
||
shngTargetDoorStateCallback(value: unknown): void { | ||
this.platform.log.debug('shngTargetDoorStateCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); | ||
if (typeof value === 'number') { | ||
this.targetDoorState = value; | ||
this.deviceService.updateCharacteristic(this.platform.Characteristic.TargetDoorState, this.targetDoorState); | ||
} else { | ||
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); | ||
} | ||
} | ||
|
||
shngObstructionDetectedCallback(value: unknown): void { | ||
this.platform.log.debug('shngObstructionDetectedCallback:', this.accessory.name, '=', value, '(' + typeof value + ')'); | ||
if (typeof value === 'boolean') { | ||
this.obstructionDetected = value; | ||
this.deviceService.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, this.obstructionDetected); | ||
} else { | ||
this.platform.log.warn('Unknown type ', typeof value, 'received for', this.accessory.name + ':', value); | ||
} | ||
} | ||
} |
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
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