diff --git a/src/integrations/home-assistant/home-assistant.service.spec.ts b/src/integrations/home-assistant/home-assistant.service.spec.ts index 89492dc..8fbcfa8 100644 --- a/src/integrations/home-assistant/home-assistant.service.spec.ts +++ b/src/integrations/home-assistant/home-assistant.service.spec.ts @@ -924,6 +924,32 @@ describe('HomeAssistantService', () => { expect(mockMqttClient.publish).toHaveBeenCalledTimes(1); }); + it('should format string array attribute updates correctly', async () => { + jest.useFakeTimers(); + await service.onModuleInit(); + const sensor = new Sensor('test', 'Test'); + service.handleNewEntity(sensor); + + sensor.attributes.nodes = ['abc', 'def']; + service.handleEntityUpdate( + sensor, + [ + { + newValue: ['abc', 'def'], + oldValue: undefined, + path: '/attributes/nodes', + }, + ], + true + ); + jest.runAllTimers(); + + expect(mockMqttClient.publish).toHaveBeenCalledWith( + 'room-assistant/sensor/test-instance-test/attributes', + JSON.stringify({ nodes: ['abc', 'def'] }) + ); + }); + it('should not publish attribute updates if sendAttributes is disabled', async () => { jest.useFakeTimers(); mockConfig.sendAttributes = false; diff --git a/src/integrations/home-assistant/home-assistant.service.ts b/src/integrations/home-assistant/home-assistant.service.ts index 62431bf..8fae92b 100644 --- a/src/integrations/home-assistant/home-assistant.service.ts +++ b/src/integrations/home-assistant/home-assistant.service.ts @@ -584,6 +584,10 @@ export class HomeAssistantService * @param mapper - Function to apply to all items */ private deepMap(obj: object, mapper: (v: object) => object): object { + if (!_.isObject(obj)) { + return mapper(obj); + } + const mappingMethod: ( obj: object, callback: (v: object) => object