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 support for MQTT device trackers
No auto discovery sadly, but this is still easier than having to write custom automations to have device tracker based on the room presence. Closes #183
- Loading branch information
Showing
11 changed files
with
198 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Entity } from './entity'; | ||
|
||
export class DeviceTracker extends Entity { | ||
state: boolean; | ||
} |
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
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
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,10 @@ | ||
import { EntityConfig } from './entity-config'; | ||
|
||
export class DeviceTrackerConfig extends EntityConfig { | ||
readonly payloadHome = 'true'; | ||
readonly payloadNotHome = 'false'; | ||
|
||
constructor(id: string, name: string) { | ||
super('device_tracker', id, name); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/integrations/room-presence/room-presence-device-tracker.proxy.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,34 @@ | ||
import { RoomPresenceDistanceSensor } from './room-presence-distance.sensor'; | ||
import { DeviceTracker } from '../../entities/device-tracker'; | ||
import { RoomPresenceDeviceTrackerProxyHandler } from './room-presence-device-tracker.proxy'; | ||
|
||
describe('RoomPresenceDeviceTrackerProxyHandler', () => { | ||
let proxy: RoomPresenceDistanceSensor; | ||
let deviceTracker: DeviceTracker; | ||
|
||
beforeEach(() => { | ||
deviceTracker = new DeviceTracker('test-tracker', 'Test Tracker'); | ||
|
||
const sensor = new RoomPresenceDistanceSensor( | ||
'test-sensor', | ||
'Test Sensor', | ||
0 | ||
); | ||
proxy = new Proxy<RoomPresenceDistanceSensor>( | ||
sensor, | ||
new RoomPresenceDeviceTrackerProxyHandler(deviceTracker) | ||
); | ||
}); | ||
|
||
it('should set the device tracker to true when home', () => { | ||
proxy.handleNewDistance('room1', 0.5); | ||
|
||
expect(deviceTracker.state).toBeTruthy(); | ||
}); | ||
|
||
it('should set the device tracker to false when not home', () => { | ||
proxy.updateState(); | ||
|
||
expect(deviceTracker.state).toBeFalsy(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
src/integrations/room-presence/room-presence-device-tracker.proxy.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,20 @@ | ||
import { | ||
RoomPresenceDistanceSensor, | ||
STATE_NOT_HOME | ||
} from './room-presence-distance.sensor'; | ||
import { DeviceTracker } from '../../entities/device-tracker'; | ||
|
||
export class RoomPresenceDeviceTrackerProxyHandler | ||
implements ProxyHandler<RoomPresenceDistanceSensor> { | ||
constructor(private readonly deviceTracker: DeviceTracker) {} | ||
|
||
set(target: RoomPresenceDistanceSensor, p: PropertyKey, value: any): boolean { | ||
target[p] = value; | ||
|
||
if (p === 'state') { | ||
this.deviceTracker.state = value != STATE_NOT_HOME; | ||
} | ||
|
||
return true; | ||
} | ||
} |