Skip to content

Commit

Permalink
feat(home-assistant): add health indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed Dec 13, 2020
1 parent 286bd22 commit 2ab6e42
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/integrations/home-assistant/home-assistant.health.spec.ts
@@ -0,0 +1,31 @@
import { HomeAssistantHealthIndicator } from './home-assistant.health';
import { HomeAssistantService } from './home-assistant.service';
import { HealthCheckError } from '@nestjs/terminus';

describe('HomeAssistantHealthIndicator', () => {
const serviceMock = {
isConnected: jest.fn(),
};
const healthIndicator = new HomeAssistantHealthIndicator(
(serviceMock as unknown) as HomeAssistantService
);

it('should report healthy if connection is established', () => {
serviceMock.isConnected.mockReturnValue(true);

const result = healthIndicator.connectionCheck();
expect(result['ha_mqtt_connected'].status).toEqual('up');
});

it('should report unhealthy if connection not established yet', () => {
serviceMock.isConnected.mockReturnValue(undefined);

expect(() => healthIndicator.connectionCheck()).toThrow(HealthCheckError);
});

it('should report unhealthy if connection lost', () => {
serviceMock.isConnected.mockReturnValue(false);

expect(() => healthIndicator.connectionCheck()).toThrow(HealthCheckError);
});
});
32 changes: 32 additions & 0 deletions src/integrations/home-assistant/home-assistant.health.ts
@@ -0,0 +1,32 @@
import {
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from '@nestjs/terminus';
import { Injectable, Optional } from '@nestjs/common';
import { HealthIndicatorService } from '../../status/health-indicator.service';
import { HomeAssistantService } from './home-assistant.service';

@Injectable()
export class HomeAssistantHealthIndicator extends HealthIndicator {
constructor(
private readonly homeAssistantService: HomeAssistantService,
@Optional() healthIndicatorService?: HealthIndicatorService
) {
super();
healthIndicatorService?.registerHealthIndicator(async () =>
this.connectionCheck()
);
}

connectionCheck(): HealthIndicatorResult {
const isHealthy = this.homeAssistantService.isConnected();
const result = this.getStatus('ha_mqtt_connected', isHealthy);

if (isHealthy) {
return result;
}

throw new HealthCheckError('No connection to MQTT broker', result);
}
}
6 changes: 4 additions & 2 deletions src/integrations/home-assistant/home-assistant.module.ts
Expand Up @@ -2,14 +2,16 @@ import { DynamicModule, Module } from '@nestjs/common';
import { HomeAssistantService } from './home-assistant.service';
import { ConfigModule } from '../../config/config.module';
import { EntitiesModule } from '../../entities/entities.module';
import { StatusModule } from '../../status/status.module';
import { HomeAssistantHealthIndicator } from './home-assistant.health';

@Module({})
export default class HomeAssistantModule {
static forRoot(): DynamicModule {
return {
module: HomeAssistantModule,
imports: [ConfigModule, EntitiesModule],
providers: [HomeAssistantService],
imports: [ConfigModule, EntitiesModule, StatusModule],
providers: [HomeAssistantService, HomeAssistantHealthIndicator],
};
}
}
7 changes: 7 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -253,6 +253,13 @@ export class HomeAssistantService
}
}

/**
* Checks if room-assistant is connected to the MQTT broker.
*/
isConnected(): boolean {
return this.mqttClient?.connected;
}

/**
* Handles broker re-connection events.
*/
Expand Down

0 comments on commit 2ab6e42

Please sign in to comment.