Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(home-assistant): add health indicator
- Loading branch information
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/integrations/home-assistant/home-assistant.health.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters