All files / src/irdevice camera.ts

2.32% Statements 2/86
0% Branches 0/1
0% Functions 0/1
2.32% Lines 2/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                    1x             1x                                                                                                                                                                                                                                  
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
 *
 * camera.ts: @switchbot/homebridge-switchbot.
 */
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
import type { bodyChange, irdevice } from 'node-switchbot'
 
import type { SwitchBotPlatform } from '../platform.js'
import type { irDevicesConfig } from '../settings.js'
 
import { irdeviceBase } from './irdevice.js'
 
/**
 * Platform Accessory
 * An instance of this class is created for each accessory your platform registers
 * Each accessory may expose multiple services of different service types.
 */
export class Camera extends irdeviceBase {
  // Services
  private Switch: {
    Name: CharacteristicValue
    Service: Service
    On: CharacteristicValue
  }
 
  constructor(
    readonly platform: SwitchBotPlatform,
    accessory: PlatformAccessory,
    device: irdevice & irDevicesConfig,
  ) {
    super(platform, accessory, device)
    // Set category
    accessory.category = this.hap.Categories.CAMERA
 
    // Initialize Switch Service
    accessory.context.Switch = accessory.context.Switch ?? {}
    this.Switch = {
      Name: accessory.displayName,
      Service: accessory.getService(this.hap.Service.Switch) ?? accessory.addService(this.hap.Service.Switch) as Service,
      On: accessory.context.On ?? false,
    }
    accessory.context.Switch = this.Switch as object
 
    this.Switch.Service.setCharacteristic(this.hap.Characteristic.Name, this.Switch.Name).getCharacteristic(this.hap.Characteristic.On).onGet(() => {
      return this.Switch.On
    }).onSet(this.OnSet.bind(this))
  }
 
  async OnSet(value: CharacteristicValue): Promise<void> {
    this.debugLog(`On: ${value}`)
 
    this.Switch.On = value
    if (this.Switch.On) {
      this.pushOnChanges()
    } else {
      this.pushOffChanges()
    }
  }
 
  /**
   * Pushes the requested changes to the SwitchBot API
   * deviceType    commandType     Command           command parameter           Description
   * Camera -        "command"       "turnOff"         "default"          =        set to OFF state
   * Camera -        "command"       "turnOn"          "default"          =        set to ON state
   * Camera -        "command"       "volumeAdd"       "default"          =        volume up
   * Camera -        "command"       "volumeSub"       "default"          =        volume down
   * Camera -        "command"       "channelAdd"      "default"          =        next channel
   * Camera -        "command"       "channelSub"      "default"          =        previous channel
   */
  async pushOnChanges(): Promise<void> {
    this.debugLog(`pushOnChanges On: ${this.Switch.On}, disablePushOn: ${this.deviceDisablePushOn}`)
    if (this.Switch.On && !this.deviceDisablePushOn) {
      const commandType: string = await this.commandType()
      const command: string = await this.commandOn()
      const bodyChange: bodyChange = {
        command,
        parameter: 'default',
        commandType,
      }
      await this.pushChanges(bodyChange)
    }
  }
 
  async pushOffChanges(): Promise<void> {
    this.debugLog(`pushOffChanges On: ${this.Switch.On}, disablePushOff: ${this.deviceDisablePushOff}`)
    if (!this.Switch.On && !this.deviceDisablePushOff) {
      const commandType: string = await this.commandType()
      const command: string = await this.commandOff()
      const bodyChange: bodyChange = {
        command,
        parameter: 'default',
        commandType,
      }
      await this.pushChanges(bodyChange)
    }
  }
 
  async pushChanges(bodyChange: any): Promise<void> {
    this.debugLog('pushChanges')
    if (this.device.connectionType === 'OpenAPI') {
      this.infoLog(`Sending request to SwitchBot API, body: ${JSON.stringify(bodyChange)}`)
      try {
        const response = await this.pushChangeRequest(bodyChange)
        const deviceStatus: any = response.body
        await this.pushStatusCodes(deviceStatus)
        if (await this.successfulStatusCodes(deviceStatus)) {
          await this.successfulPushChange(deviceStatus, bodyChange)
          await this.updateHomeKitCharacteristics()
        } else {
          await this.statusCode(deviceStatus.statusCode)
        }
      } catch (e: any) {
        await this.apiError(e)
        await this.pushChangeError(e)
      }
    } else {
      this.warnLog(`Connection Type: ${this.device.connectionType}, commands will not be sent to OpenAPI`)
    }
  }
 
  async updateHomeKitCharacteristics(): Promise<void> {
    this.debugLog('updateHomeKitCharacteristics')
    // Active
    await this.updateCharacteristic(this.Switch.Service, this.hap.Characteristic.On, this.Switch.On, 'On')
  }
 
  async apiError(e: any): Promise<void> {
    this.Switch.Service.updateCharacteristic(this.hap.Characteristic.On, e)
  }
}