homebridge#PlatformAccessory TypeScript Examples

The following examples show how to use homebridge#PlatformAccessory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
protected constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    assert(client !== null, 'ZigBee client must be initialized');
    assert(platform !== null, 'Platform plugin must be initialized');
    assert(accessory !== null, 'Platform Accessory must be initialized');
    this.platform = platform;
    this.accessory = accessory;
    this.client = client;
    this.state = state;
  }
Example #2
Source File: platform.ts    From homebridge-esphome-ts with GNU General Public License v3.0 6 votes vote down vote up
public configureAccessory(accessory: PlatformAccessory): void {
        if (!this.blacklistSet.has(accessory.displayName)) {
            this.accessories.push(accessory);
            this.logIfDebug(`cached accessory ${accessory.displayName} was added`);
        } else {
            this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
            this.logIfDebug(`unregistered ${accessory.displayName} because it was blacklisted`);
        }
    }
Example #3
Source File: wiz.ts    From homebridge-wiz-lan with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info("Loading accessory from cache:", accessory.displayName);

    this.initAccessory(accessory);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #4
Source File: permit-join-accessory.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    zigBeeClient: ZigBeeClient
  ) {
    this.zigBeeClient = zigBeeClient;
    // Current progress status
    this.inProgress = false;
    // Save platform
    this.platform = platform;
    this.accessory = accessory;
    // Verify accessory
    const serialNumber = Math.random()
      .toString(36)
      .substr(2, 10);
    const Characteristic = platform.Characteristic;
    this.accessory
      .getService(platform.Service.AccessoryInformation)
      .setCharacteristic(Characteristic.Manufacturer, 'None')
      .setCharacteristic(Characteristic.Model, 'None')
      .setCharacteristic(Characteristic.SerialNumber, serialNumber)
      .setCharacteristic(Characteristic.FirmwareRevision, '1.0.0')
      .setCharacteristic(platform.Characteristic.Name, 'ZigBee Permit Join');

    this.switchService =
      this.accessory.getService(platform.Service.Switch) ||
      this.accessory.addService(platform.Service.Switch);

    this.accessory.on('identify', () => this.handleAccessoryIdentify());
    const characteristic = this.switchService.getCharacteristic(this.platform.Characteristic.On);
    characteristic.on(CharacteristicEventTypes.GET, callback => this.handleGetSwitchOn(callback));
    characteristic.on(CharacteristicEventTypes.SET, (value, callback) => {
      this.handleSetSwitchOn(value, callback);
    });
    // Disable permit join on start
    this.setPermitJoin(false).then(() => platform.log.info('Permit join disabled'));
  }
Example #5
Source File: pilot.ts    From homebridge-wiz-lan with Apache License 2.0 6 votes vote down vote up
function updatePilot(
  wiz: HomebridgeWizLan,
  accessory: PlatformAccessory,
  _: Device,
  pilot: Pilot | Error
) {
  const { Service } = wiz;
  const service = accessory.getService(Service.Outlet)!;

  service
    .getCharacteristic(wiz.Characteristic.On)
    .updateValue(pilot instanceof Error ? pilot : transformOnOff(pilot));
}
Example #6
Source File: touchlink-accessory.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    zigBee: ZigBeeClient
  ) {
    this.zigBee = zigBee;
    // Current progress status
    this.inProgress = false;
    // Save platform
    this.platform = platform;
    // Save logger
    this.log = platform.log;
    this.accessory = accessory;
    // Verify accessory
    const serialNumber = Math.random()
      .toString(36)
      .substr(2, 10);
    const Characteristic = platform.Characteristic;
    this.accessory
      .getService(platform.Service.AccessoryInformation)
      .setCharacteristic(Characteristic.Manufacturer, 'None')
      .setCharacteristic(Characteristic.Model, 'None')
      .setCharacteristic(Characteristic.SerialNumber, serialNumber)
      .setCharacteristic(Characteristic.FirmwareRevision, '1.0.0')
      .setCharacteristic(Characteristic.Name, 'ZigBee Touchlink');

    this.switchService =
      this.accessory.getService(platform.Service.Switch) ||
      this.accessory.addService(platform.Service.Switch);

    this.accessory.on('identify', () => this.handleAccessoryIdentify());
    const characteristic = this.switchService.getCharacteristic(this.platform.Characteristic.On);
    characteristic.on('get', callback => this.handleGetSwitchOn(callback));
    characteristic.on('set', (value, callback) =>
      this.handleSetSwitchOn(value as boolean, callback)
    );
  }
Example #7
Source File: temperature.ts    From homebridge-wiz-lan with Apache License 2.0 6 votes vote down vote up
export function initTemperature(
  accessory: PlatformAccessory,
  device: Device,
  wiz: HomebridgeWizLan
) {
  const { Characteristic, Service } = wiz;
  const service = accessory.getService(Service.Lightbulb)!;
  service
    .getCharacteristic(Characteristic.ColorTemperature)
    .on("get", (callback) =>
      getPilot(
        wiz,
        accessory,
        device,
        (pilot) => callback(null, transformTemperature(pilot)),
        callback
      )
    )
    .on(
      "set",
      (newValue: CharacteristicValue, next: CharacteristicSetCallback) => {
        setPilot(
          wiz,
          accessory,
          device,
          {
            temp: miredToKelvin(Number(newValue)),
            r: undefined,
            g: undefined,
            b: undefined,
          },
          updateColorTemp(device, accessory, wiz, next)
        );
      }
    );
}
Example #8
Source File: aqara-opple-switch.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    device: Device
  ) {
    super(platform, accessory, client, device);

    this.withBattery = true;
    this.buttons = [
      {
        index: 1,
        displayName: 'button_1',
        subType: 'top_left',
      },
      {
        index: 2,
        displayName: 'button_2',
        subType: 'top_right',
      },
    ];
  }
Example #9
Source File: onOff.ts    From homebridge-wiz-lan with Apache License 2.0 6 votes vote down vote up
export function initOnOff(
  accessory: PlatformAccessory,
  device: Device,
  wiz: HomebridgeWizLan
) {
  const { Characteristic, Service } = wiz;
  const service = accessory.getService(Service.Lightbulb)!;
  service
    .getCharacteristic(Characteristic.On)
    .on("get", callback =>
      getPilot(
        wiz,
        accessory,
        device,
        pilot => callback(null, transformOnOff(pilot)),
        callback
      )
    )
    .on(
      "set",
      (newValue: CharacteristicValue, next: CharacteristicSetCallback) => {
        setPilot(wiz, accessory, device, { state: Boolean(newValue) }, next);
      }
    );
}
Example #10
Source File: aqara-opple-switch.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    device: Device
  ) {
    super(platform, accessory, client, device);

    this.withBattery = true;
    this.buttons = [
      {
        index: 3,
        displayName: 'button_1',
        subType: 'top_left',
      },
      {
        index: 4,
        displayName: 'button_2',
        subType: 'top_right',
      },
      {
        index: 1,
        displayName: 'button_3',
        subType: 'bottom_left',
      },
      {
        index: 2,
        displayName: 'button_4',
        subType: 'bottom_right',
      },
    ];
  }
Example #11
Source File: color.ts    From homebridge-wiz-lan with Apache License 2.0 6 votes vote down vote up
function initSaturation(
  accessory: PlatformAccessory,
  device: Device,
  wiz: HomebridgeWizLan
) {
  const { Characteristic, Service } = wiz;
  const service = accessory.getService(Service.Lightbulb)!;
  service
    .getCharacteristic(Characteristic.Saturation)
    .on("get", (callback) =>
      getPilot(
        wiz,
        accessory,
        device,
        pilot => callback(null, transformSaturation(pilot)),
        callback
      )
    )
    .on(
      "set",
      (newValue: CharacteristicValue, next: CharacteristicSetCallback) => {
        setPilot(
          wiz,
          accessory,
          device,
          {
            temp: undefined,
            ...hsvToColor(
              pilotToColor(cachedPilot[device.mac]).hue / 360,
              Number(newValue) / 100,
              wiz
            ),
          },
          updateColorTemp(device, accessory, wiz, next)
        );
      }
    );
}
Example #12
Source File: zig-bee-accessory.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    device: Device
  ) {
    this.client = client;
    this.ieeeAddr = device.ieeeAddr;
    this.platform = platform;
    this.log = this.platform.log;
    this.state = {};
    this.accessory = accessory;
    this.accessory.context = device;
    this.entity = this.client.resolveEntity(device);
    this.isOnline = true;
    assert(this.entity !== null, 'ZigBee Entity resolution failed');
    const Characteristic = platform.Characteristic;
    this.accessory
      .getService(this.platform.Service.AccessoryInformation)
      .setCharacteristic(Characteristic.Manufacturer, device.manufacturerName)
      .setCharacteristic(Characteristic.Model, device.modelID)
      .setCharacteristic(Characteristic.SerialNumber, device.ieeeAddr)
      .setCharacteristic(Characteristic.SoftwareRevision, `${device.softwareBuildID}`)
      .setCharacteristic(Characteristic.HardwareRevision, `${device.hardwareVersion}`)
      .setCharacteristic(Characteristic.Name, this.friendlyName);
    this.accessory.on('identify', () => this.handleAccessoryIdentify());
  }
Example #13
Source File: platform.ts    From homebridge-konnected with MIT License 6 votes vote down vote up
/**
   * Homebridge's startup restoration of cached accessories from disk.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info(`Loading accessory from cache: ${accessory.displayName} (${accessory.context.device.serialNumber})`);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #14
Source File: battery-service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform, accessory, client, state);
    this.service =
      this.accessory.getService(platform.Service.BatteryService) ||
      this.accessory.addService(platform.Service.BatteryService);
  }
Example #15
Source File: platform.ts    From homebridge-plugin-template with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info('Loading accessory from cache:', accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #16
Source File: outlet-service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform, accessory, client, state);
    this.service =
      this.accessory.getService(platform.Service.Outlet) ||
      this.accessory.addService(platform.Service.Outlet);
  }
Example #17
Source File: platform.ts    From homebridge-iRobot with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info('Loading accessory from cache:', accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    if (accessory.context.pluginVersion === undefined || accessory.context.pluginVersion < 3) {
      this.log.warn('Removing Old Accessory:', accessory.displayName);
      this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
    } else {
      this.accessories.push(accessory);
    }
  }
Example #18
Source File: sensor-service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    service: WithUUID<typeof Service>,
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform, accessory, client, state);
    this.service = this.accessory.getService(service) || this.accessory.addService(service);
  }
Example #19
Source File: platform.ts    From homebridge-iRobot with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info('Loading accessory from cache:', accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #20
Source File: switch-service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform, accessory, client, state);
    this.service =
      this.accessory.getService(platform.Service.Switch) ||
      this.accessory.addService(platform.Service.Switch);
  }
Example #21
Source File: platform.ts    From homebridge-plugin-eufy-security with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info('Loading accessory from cache:', accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #22
Source File: thermostat-service-builder.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: ZigbeeNTHomebridgePlatform,
    accessory: PlatformAccessory,
    client: ZigBeeClient,
    state: DeviceState
  ) {
    super(platform, accessory, client, state);

    this.service =
      this.accessory.getService(platform.Service.Thermostat) ||
      this.accessory.addService(platform.Service.Thermostat);
  }
Example #23
Source File: platform.ts    From homebridge-screenlogic with MIT License 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  public configureAccessory(accessory: PlatformAccessory) {
    this.log.info('Loading accessory from cache:', accessory.displayName)

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.restoredAccessories.push(accessory)
  }
Example #24
Source File: platform.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
private createHapAccessory(name: string) {
    const uuid = this.generateUUID(name);
    const existingAccessory = this.getAccessoryByUUID(uuid);
    if (existingAccessory) {
      this.log.info(`Reuse accessory from cache with uuid ${uuid} and name ${name}`);
      return existingAccessory;
    } else {
      const accessory = new this.PlatformAccessory(name, uuid);
      this.log.warn(`Registering new accessory with uuid ${uuid} and name ${name}`);
      this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
      this.accessories.set(uuid, accessory);
      return accessory;
    }
  }
Example #25
Source File: platform.ts    From homebridge-plugin-govee with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.info("Loading accessory from cache:", accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #26
Source File: platform.ts    From homebridge-eufy-security with Apache License 2.0 6 votes vote down vote up
/**
   * This function is invoked when homebridge restores cached accessories from disk at startup.
   * It should be used to setup event handlers for characteristics and update respective values.
   */
  configureAccessory(accessory: PlatformAccessory) {
    this.log.debug('Loading accessory from cache:', accessory.displayName);

    // add the restored accessory to the accessories cache so we can track if it has already been registered
    this.accessories.push(accessory);
  }
Example #27
Source File: platform.ts    From homebridge-zigbee-nt with Apache License 2.0 6 votes vote down vote up
constructor(
    public readonly log: Logger,
    public readonly config: ZigBeeNTPlatformConfig,
    public readonly api: API
  ) {
    const packageJson = JSON.parse(
      fs.readFileSync(`${path.resolve(__dirname, '../package.json')}`, 'utf-8')
    );
    this.accessories = new Map<string, PlatformAccessory>();
    this.homekitAccessories = new Map<string, ZigBeeAccessory>();
    this.permitJoinAccessory = null;
    this.PlatformAccessory = this.api.platformAccessory;
    this.log.info(
      `Initializing platform: ${this.config.name} - v${packageJson.version} (API v${api.version})`
    );
    if (config.devices) {
      config.devices.forEach(config => {
        this.log.info(
          `Registering custom configured device ${config.manufacturer} - ${config.models.join(
            ', '
          )}`
        );
        registerAccessoryFactory(
          config.manufacturer,
          config.models,
          (
            platform: ZigbeeNTHomebridgePlatform,
            accessory: PlatformAccessory,
            client: ZigBeeClient,
            device: Device
          ) => new ConfigurableAccessory(platform, accessory, client, device, config.services)
        );
      });
    }
    this.api.on(APIEvent.DID_FINISH_LAUNCHING, () => this.startZigBee());
    this.api.on(APIEvent.SHUTDOWN, () => this.stopZigbee());
  }
Example #28
Source File: index.ts    From homebridge-fordpass with GNU General Public License v3.0 6 votes vote down vote up
async addVehicles(connection: Connection): Promise<void> {
    const vehicles = await connection.getVehicles();
    vehicles?.forEach(async (vehicle: VehicleConfig) => {
      vehicle.vin = vehicle.vin.toUpperCase();
      const name = vehicle.nickName || vehicle.vehicleType;
      const uuid = hap.uuid.generate(vehicle.vin);
      const accessory = new Accessory(name, uuid);
      accessory.context.name = name;
      accessory.context.vin = vehicle.vin;

      const accessoryInformation = accessory.getService(hap.Service.AccessoryInformation);
      if (accessoryInformation) {
        accessoryInformation.setCharacteristic(hap.Characteristic.Manufacturer, 'Ford');
        accessoryInformation.setCharacteristic(hap.Characteristic.Model, name);
        accessoryInformation.setCharacteristic(hap.Characteristic.SerialNumber, vehicle.vin);
      }

      // Only add new cameras that are not cached
      if (!this.accessories.find((x: PlatformAccessory) => x.UUID === uuid)) {
        this.log.debug(`New vehicle found: ${name}`);
        this.configureAccessory(accessory); // abusing the configureAccessory here
        this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
      }
    });

    // Remove vehicles that were removed from config
    this.accessories.forEach((accessory: PlatformAccessory<Record<string, string>>) => {
      if (!vehicles?.find((x: VehicleConfig) => x.vin === accessory.context.vin)) {
        this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
        const index = this.accessories.indexOf(accessory);
        if (index > -1) {
          this.accessories.splice(index, 1);
          this.vehicles.slice(index, 1);
        }
      }
    });
  }
Example #29
Source File: SmartLockAccessory.ts    From homebridge-eufy-security with Apache License 2.0 6 votes vote down vote up
constructor(
    platform: EufySecurityPlatform,
    accessory: PlatformAccessory,
    eufyDevice: Lock,
  ) {
    super(platform, accessory, eufyDevice);

    this.platform.log.debug(this.accessory.displayName, 'Constructed SmartLock');

    this.SmartLock = eufyDevice;

    this.service =
      this.accessory.getService(this.platform.Service.LockMechanism) ||
      this.accessory.addService(this.platform.Service.LockMechanism);

    this.service.setCharacteristic(
      this.platform.Characteristic.Name,
      accessory.displayName,
    );

    // create handlers for required characteristics
    this.service
      .getCharacteristic(this.platform.Characteristic.LockCurrentState)
      .onGet(this.handleLockCurrentStateGet.bind(this));

    this.service
      .getCharacteristic(this.platform.Characteristic.LockTargetState)
      .onGet(this.handleLockTargetStateGet.bind(this))
      .onSet(this.handleLockTargetStateSet.bind(this));

    this.SmartLock.on('locked', (device: Device, lock: boolean) =>
      this.onDeviceLockPushNotification(device, lock),
    );
    
  }