Skip to content

Commit

Permalink
feat(bluetooth-low-energy): allow id overrides
Browse files Browse the repository at this point in the history
Closes #433
  • Loading branch information
mKeRix committed Feb 14, 2021
1 parent 83d9581 commit e41034e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/integrations/bluetooth-low-energy.md
Expand Up @@ -86,6 +86,7 @@ The tag overrides object can be considered as a map with the BLE tag ID as key a

| Name | Type | Default | Description |
| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | String | | Changes the ID of the device within room-assistant, which is used for entity ID generation. Useful to hide peripheral addresses from publicly shared home automation configurations. |
| `name` | String | | Sets a friendly name for the device, which is sent to the home automation software for easier identification. |
| `measuredPower` | Number | | Overrides the [measured power](https://community.estimote.com/hc/en-us/articles/201636913-What-are-Broadcasting-Power-RSSI-and-other-characteristics-of-a-beacon-s-signal-) of a BLE tag, which is used for distance calculation. Should be the expected RSSI when the beacon is exactly 1 meter away from the room-assistant instance. |
| `batteryMask` | Number | `0x00000000` | If non-zero, extract the beacon's battery level from the major/minor fields. The mask operates on a 32bit value with major as the high two bytes and minor as the low two bytes. |
Expand Down
Expand Up @@ -21,6 +21,7 @@ export class BluetoothLowEnergyConfig {
}

class TagOverride {
id?: string;
name?: string;
measuredPower?: number;
batteryMask?: number;
Expand Down
Expand Up @@ -413,6 +413,34 @@ describe('BluetoothLowEnergyService', () => {
);
});

it('should apply a tag id override if it exists', async () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
.mockImplementation(() => undefined);
const allowlistSpy = jest.spyOn(service, 'isOnAllowlist').mockReturnValue(true);
jest.spyOn(service, 'isAllowlistEnabled').mockReturnValue(true);
mockConfig.tagOverrides = {
abcd: {
id: 'new-id',
},
};

await service.handleDiscovery({
id: 'abcd',
rssi: -12,
advertisement: {
localName: 'Test BLE Device',
},
} as Peripheral);

expect(handleDistanceSpy).toHaveBeenCalledWith(
expect.objectContaining({
tagId: 'new-id',
})
);
expect(allowlistSpy).toHaveBeenCalledWith('new-id')
});

it('should apply a tag name override if it exists', async () => {
const handleDistanceSpy = jest
.spyOn(service, 'handleNewDistance')
Expand Down
Expand Up @@ -93,6 +93,7 @@ export class BluetoothLowEnergyService
}

tag = await this.applyCompanionAppOverride(tag);
tag = this.applyOverrides(tag);

if (!this.seenIds.has(tag.id)) {
this.logger.log(
Expand All @@ -106,7 +107,6 @@ export class BluetoothLowEnergyService
(!this.isAllowlistEnabled() && this.isDenylistEnabled())) &&
!this.isOnDenylist(tag.id)
) {
tag = this.applyOverrides(tag);
tag.rssi = this.filterRssi(tag.id, tag.rssi);

const globalSettings = this.configService.get('global');
Expand Down Expand Up @@ -462,6 +462,9 @@ export class BluetoothLowEnergyService
protected applyOverrides(tag: Tag): Tag {
if (this.config.tagOverrides.hasOwnProperty(tag.id)) {
const overrides = this.config.tagOverrides[tag.id];
if (overrides.id !== undefined) {
tag.id = overrides.id;
}
if (overrides.name !== undefined) {
tag.name = overrides.name;
}
Expand Down

0 comments on commit e41034e

Please sign in to comment.