Skip to content

Commit

Permalink
feat(entities): added binary sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed Feb 1, 2020
1 parent 4ad5872 commit bbd4c40
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/entities/binary-sensor.ts
@@ -0,0 +1,5 @@
import { Entity } from './entity';

export class BinarySensor extends Entity {
state: boolean;
}
37 changes: 37 additions & 0 deletions src/integrations/home-assistant/binary-sensor-config.ts
@@ -0,0 +1,37 @@
import { EntityConfig } from './entity-config';

export class BinarySensorConfig extends EntityConfig {
payload_on = 'true';
payload_off = 'false';
device_class?: BinarySensorDeviceClass;

constructor(id: string, name: string) {
super('binary_sensor', id, name);
}
}

export enum BinarySensorDeviceClass {
BATTERY = 'battery',
COLD = 'cold',
CONNECTIVITY = 'connectivity',
DOOR = 'door',
GARAGE_DOOR = 'garage_door',
GAS = 'gas',
HEAT = 'heat',
LIGHT = 'light',
LOCK = 'lock',
MOISTURE = 'moisture',
MOTION = 'motion',
MOVING = 'moving',
OCCUPANCY = 'occupancy',
OPENING = 'opening',
PLUG = 'plug',
POWER = 'power',
PRESENCE = 'presence',
PROBLEM = 'problem',
SAFETY = 'safety',
SMOKE = 'smoke',
SOUND = 'sound',
VIBRATION = 'vibration',
WINDOW = 'window'
}
35 changes: 35 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.spec.ts
Expand Up @@ -17,6 +17,7 @@ import SystemData = Systeminformation.SystemData;
import { SensorConfig } from './sensor-config';
import { Entity } from '../../entities/entity';
import { Sensor } from '../../entities/sensor';
import { BinarySensor } from '../../entities/binary-sensor';

jest.mock('async-mqtt', () => {
return {
Expand Down Expand Up @@ -169,6 +170,40 @@ describe('HomeAssistantService', () => {
});
});

it('should publish configuration for a new binary sensor', async () => {
await service.onModuleInit();
service.handleNewEntity(new BinarySensor('bin-sensor', 'Binary'));

expect(mockMqttClient.publish).toHaveBeenCalledWith(
'homeassistant/binary_sensor/room-assistant/test-instance-bin-sensor/config',
expect.any(String),
{
qos: 0,
retain: true
}
);
expect(mockMqttClient.publish).toHaveBeenCalledWith(
'room-assistant/binary_sensor/test-instance-bin-sensor/status',
'online',
{
qos: 0,
retain: true
}
);
expect(JSON.parse(mockMqttClient.publish.mock.calls[0][1])).toMatchObject({
unique_id: 'room-assistant-test-instance-bin-sensor',
name: 'Binary',
state_topic:
'room-assistant/binary_sensor/test-instance-bin-sensor/state',
json_attributes_topic:
'room-assistant/binary_sensor/test-instance-bin-sensor/attributes',
availability_topic:
'room-assistant/binary_sensor/test-instance-bin-sensor/status',
payload_on: 'true',
payload_off: 'false'
});
});

it('should include device information in the discovery message', async () => {
mockSystem.mockResolvedValue({
serial: 'abcd',
Expand Down
4 changes: 4 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -19,6 +19,8 @@ import { EntitiesEventEmitter } from '../../entities/entities.events';
import { EntityCustomization } from '../../entities/entity-customization.interface';
import { makeId } from '../../util/id';
import { DISTRIBUTED_DEVICE_ID } from './home-assistant.const';
import { BinarySensor } from '../../entities/binary-sensor';
import { BinarySensorConfig } from './binary-sensor-config';

const PROPERTY_BLACKLIST = ['component', 'configTopic'];

Expand Down Expand Up @@ -230,6 +232,8 @@ export class HomeAssistantService
): EntityConfig {
if (entity instanceof Sensor) {
return new SensorConfig(combinedId, entity.name);
} else if (entity instanceof BinarySensor) {
return new BinarySensorConfig(combinedId, entity.name);
} else {
return;
}
Expand Down

0 comments on commit bbd4c40

Please sign in to comment.