diff --git a/docs/integrations/home-assistant.md b/docs/integrations/home-assistant.md index a14b26c..d9e548e 100644 --- a/docs/integrations/home-assistant.md +++ b/docs/integrations/home-assistant.md @@ -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 diff --git a/src/integrations/home-assistant/home-assistant.config.ts b/src/integrations/home-assistant/home-assistant.config.ts index 7bb1af1..a00e4a4 100644 --- a/src/integrations/home-assistant/home-assistant.config.ts +++ b/src/integrations/home-assistant/home-assistant.config.ts @@ -3,4 +3,5 @@ import { IClientOptions } from 'async-mqtt'; export class HomeAssistantConfig { mqttUrl = 'mqtt://localhost:1883'; mqttOptions: IClientOptions = {}; + sendAttributes = true; } diff --git a/src/integrations/home-assistant/home-assistant.service.spec.ts b/src/integrations/home-assistant/home-assistant.service.spec.ts index b08886d..ea2ba76 100644 --- a/src/integrations/home-assistant/home-assistant.service.spec.ts +++ b/src/integrations/home-assistant/home-assistant.service.spec.ts @@ -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 { @@ -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 = { @@ -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: [ @@ -66,6 +76,8 @@ describe('HomeAssistantService', () => { .useValue(entitiesService) .overrideProvider(ClusterService) .useValue(clusterService) + .overrideProvider(ConfigService) + .useValue(configService) .compile(); module.useLogger(loggerService); @@ -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 }); diff --git a/src/integrations/home-assistant/home-assistant.service.ts b/src/integrations/home-assistant/home-assistant.service.ts index 9c1cb95..755e9da 100644 --- a/src/integrations/home-assistant/home-assistant.service.ts +++ b/src/integrations/home-assistant/home-assistant.service.ts @@ -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; }