platformAccessory.ts_ 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
  2. import { ExampleHomebridgePlatform } from './platform'
  3. /**
  4. * Platform Accessory
  5. * An instance of this class is created for each accessory your platform registers
  6. * Each accessory may expose multiple services of different service types.
  7. */
  8. export class ExamplePlatformAccessory {
  9. private service: Service
  10. /**
  11. * These are just used to create a working example
  12. * You should implement your own code to track the state of your accessory
  13. */
  14. private exampleStates = {
  15. On: false,
  16. Brightness: 100,
  17. }
  18. constructor(
  19. private readonly platform: ExampleHomebridgePlatform,
  20. private readonly accessory: PlatformAccessory,
  21. ) {
  22. // set accessory information
  23. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  24. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Default-Manufacturer')
  25. .setCharacteristic(this.platform.Characteristic.Model, 'Default-Model')
  26. .setCharacteristic(this.platform.Characteristic.SerialNumber, 'Default-Serial')
  27. // get the LightBulb service if it exists, otherwise create a new LightBulb service
  28. // you can create multiple services for each accessory
  29. this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb)
  30. // set the service name, this is what is displayed as the default name on the Home app
  31. // in this example we are using the name we stored in the `accessory.context` in the `discoverDevices` method.
  32. this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.device.exampleDisplayName)
  33. // each service must implement at-minimum the "required characteristics" for the given service type
  34. // see https://developers.homebridge.io/#/service/Lightbulb
  35. // register handlers for the On/Off Characteristic
  36. this.service.getCharacteristic(this.platform.Characteristic.On)
  37. .onSet(this.setOn.bind(this)) // SET - bind to the `setOn` method below
  38. .onGet(this.getOn.bind(this)) // GET - bind to the `getOn` method below
  39. // register handlers for the Brightness Characteristic
  40. this.service.getCharacteristic(this.platform.Characteristic.Brightness)
  41. .onSet(this.setBrightness.bind(this)) // SET - bind to the 'setBrightness` method below
  42. /**
  43. * Creating multiple services of the same type.
  44. *
  45. * To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
  46. * when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
  47. * this.accessory.getService('NAME') || this.accessory.addService(this.platform.Service.Lightbulb, 'NAME', 'USER_DEFINED_SUBTYPE_ID')
  48. *
  49. * The USER_DEFINED_SUBTYPE must be unique to the platform accessory (if you platform exposes multiple accessories, each accessory
  50. * can use the same sub type id.)
  51. */
  52. // Example: add two "motion sensor" services to the accessory
  53. const motionSensorOneService = this.accessory.getService('Motion Sensor One Name') ||
  54. this.accessory.addService(this.platform.Service.MotionSensor, 'Motion Sensor One Name', 'YourUniqueIdentifier-1')
  55. const motionSensorTwoService = this.accessory.getService('Motion Sensor Two Name') ||
  56. this.accessory.addService(this.platform.Service.MotionSensor, 'Motion Sensor Two Name', 'YourUniqueIdentifier-2')
  57. /**
  58. * Updating characteristics values asynchronously.
  59. *
  60. * Example showing how to update the state of a Characteristic asynchronously instead
  61. * of using the `on('get')` handlers.
  62. * Here we change update the motion sensor trigger states on and off every 10 seconds
  63. * the `updateCharacteristic` method.
  64. *
  65. */
  66. let motionDetected = false
  67. setInterval(() => {
  68. // EXAMPLE - inverse the trigger
  69. motionDetected = !motionDetected
  70. // push the new value to HomeKit
  71. motionSensorOneService.updateCharacteristic(this.platform.Characteristic.MotionDetected, motionDetected)
  72. motionSensorTwoService.updateCharacteristic(this.platform.Characteristic.MotionDetected, !motionDetected)
  73. this.platform.log.debug('Triggering motionSensorOneService:', motionDetected)
  74. this.platform.log.debug('Triggering motionSensorTwoService:', !motionDetected)
  75. }, 10000)
  76. }
  77. /**
  78. * Handle "SET" requests from HomeKit
  79. * These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
  80. */
  81. async setOn(value: CharacteristicValue) {
  82. // implement your own code to turn your device on/off
  83. this.exampleStates.On = value as boolean
  84. this.platform.log.debug('Set Characteristic On ->', value)
  85. }
  86. /**
  87. * Handle the "GET" requests from HomeKit
  88. * These are sent when HomeKit wants to know the current state of the accessory, for example, checking if a Light bulb is on.
  89. *
  90. * GET requests should return as fast as possbile. A long delay here will result in
  91. * HomeKit being unresponsive and a bad user experience in general.
  92. *
  93. * If your device takes time to respond you should update the status of your device
  94. * asynchronously instead using the `updateCharacteristic` method instead.
  95. * @example
  96. * this.service.updateCharacteristic(this.platform.Characteristic.On, true)
  97. */
  98. async getOn(): Promise<CharacteristicValue> {
  99. // implement your own code to check if the device is on
  100. const isOn = this.exampleStates.On
  101. this.platform.log.debug('Get Characteristic On ->', isOn)
  102. // if you need to return an error to show the device as "Not Responding" in the Home app:
  103. // throw new this.platform.api.hap.HapStatusError(this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE)
  104. return isOn
  105. }
  106. /**
  107. * Handle "SET" requests from HomeKit
  108. * These are sent when the user changes the state of an accessory, for example, changing the Brightness
  109. */
  110. async setBrightness(value: CharacteristicValue) {
  111. // implement your own code to set the brightness
  112. this.exampleStates.Brightness = value as number
  113. this.platform.log.debug('Set Characteristic Brightness -> ', value)
  114. }
  115. }