Skip to content

Commit

Permalink
feat(home-assistant): allow disabling of attribute updates
Browse files Browse the repository at this point in the history
This option may be helpful for people that want to reduce the amount of
events that Home Assistant needs to process, especially when using
chatty integrations like BLE.
  • Loading branch information
mKeRix committed Dec 6, 2020
1 parent d5b92cf commit 9d2eb5b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 5 additions & 4 deletions docs/integrations/home-assistant.md
Expand Up @@ -32,10 +32,11 @@ device_tracker:

## Settings

| Name | Type | Default | Description |
| ------------- | ----------------------------- | ----------------------- | ------------------------------------------- |
| `mqttUrl` | String | `mqtt://localhost:1883` | Connection string for your MQTT broker. |
| `mqttOptions` | [MQTT Options](#mqtt-options) | | Additional options for the MQTT connection. |
| Name | Type | Default | Description |
| ---------------- | ----------------------------- | ----------------------- | ------------------------------------------------------------ |
| `mqttUrl` | String | `mqtt://localhost:1883` | Connection string for your MQTT broker. |
| `mqttOptions` | [MQTT Options](#mqtt-options) | | Additional options for the MQTT connection. |
| `sendAttributes` | Boolean | `true` | Whether entity attributes should be forwarded to Home Assistant or not. May be disabled to reduce the number of messages that Home Assistant needs to process. |

### MQTT Options

Expand Down
1 change: 1 addition & 0 deletions src/integrations/home-assistant/home-assistant.config.ts
Expand Up @@ -3,4 +3,5 @@ import { IClientOptions } from 'async-mqtt';
export class HomeAssistantConfig {
mqttUrl = 'mqtt://localhost:1883';
mqttOptions: IClientOptions = {};
sendAttributes = true;
}
27 changes: 27 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.spec.ts
Expand Up @@ -25,6 +25,9 @@ import { DISTRIBUTED_DEVICE_ID } from './home-assistant.const';
import { EntitiesModule } from '../../entities/entities.module';
import { EntitiesService } from '../../entities/entities.service';
import { ClusterService } from '../../cluster/cluster.service';
import { HomeAssistantConfig } from './home-assistant.config';
import { ConfigService } from '../../config/config.service';
import c from 'config';

jest.mock('async-mqtt', () => {
return {
Expand All @@ -40,6 +43,7 @@ jest.mock('systeminformation', () => {
describe('HomeAssistantService', () => {
let service: HomeAssistantService;
let emitter: EventEmitter;
let mockConfig: HomeAssistantConfig;
const mockMqtt = mocked(mqtt, true);
const mockSystem = mocked(system);
const loggerService = {
Expand All @@ -50,9 +54,15 @@ describe('HomeAssistantService', () => {
const entitiesService = {
refreshStates: jest.fn(),
};
const configService = {
get: jest.fn().mockImplementation((key: string) => {
return key === 'homeAssistant' ? mockConfig : c.get(key);
}),
};
const clusterService = {};

beforeEach(async () => {
mockConfig = new HomeAssistantConfig();
emitter = new EventEmitter();
const module: TestingModule = await Test.createTestingModule({
imports: [
Expand All @@ -66,6 +76,8 @@ describe('HomeAssistantService', () => {
.useValue(entitiesService)
.overrideProvider(ClusterService)
.useValue(clusterService)
.overrideProvider(ConfigService)
.useValue(configService)
.compile();
module.useLogger(loggerService);

Expand Down Expand Up @@ -501,6 +513,21 @@ describe('HomeAssistantService', () => {
expect(mockMqttClient.publish).toHaveBeenCalledTimes(1);
});

it('should not publish attribute updates if sendAttributes is disabled', async () => {
jest.useFakeTimers();
mockConfig.sendAttributes = false;

await service.onModuleInit();
service.handleNewEntity(new Sensor('test', 'Test'));
service.handleNewAttributes('test', { stateUpdated: true });
jest.runAllTimers();

expect(mockMqttClient.publish).not.toHaveBeenCalledWith(
'room-assistant/sensor/test-instance-test/attributes',
expect.anything()
);
});

it('should ignore attribute updates if the entity is not registered with Home Assistant', () => {
jest.useFakeTimers();
service.handleNewAttributes('test', { stateUpdated: true });
Expand Down
2 changes: 1 addition & 1 deletion src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -205,7 +205,7 @@ export class HomeAssistantService
const config = this.entityConfigs.get(
this.getCombinedId(entityId, distributed)
);
if (config === undefined) {
if (config === undefined || !this.config.sendAttributes) {
return;
}

Expand Down

0 comments on commit 9d2eb5b

Please sign in to comment.