Skip to content

Commit

Permalink
fix(home-assistant): fix message formatting for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed May 28, 2021
1 parent 424bcae commit ef5af5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.spec.ts
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -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
Expand Down

0 comments on commit ef5af5e

Please sign in to comment.