Skip to content

Commit

Permalink
fix: include distances in API for room presence sensors
Browse files Browse the repository at this point in the history
Maps don't serialize into JSON well, which is why the implementation was
switched to use an object. The /entities endpoint will now show the
distances to the instances correctly.
  • Loading branch information
mKeRix committed Apr 13, 2020
1 parent cf3ddc5 commit 567327d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Expand Up @@ -42,9 +42,7 @@ describe('RoomPresenceDistanceSensor', () => {
it('should update the state with a further away measurement if timeout has passed', () => {
sensor.timeout = 60;
sensor.handleNewDistance('room1', 1);
sensor.distances.get('room1').lastUpdatedAt = new Date(
Date.now() - 61 * 1000
);
sensor.distances.room1.lastUpdatedAt = new Date(Date.now() - 61 * 1000);

sensor.handleNewDistance('room2', 5);
expect(sensor.state).toBe('room2');
Expand All @@ -54,9 +52,7 @@ describe('RoomPresenceDistanceSensor', () => {
it('should set the state to not_home if timeout has passed with no other measurements', () => {
sensor.timeout = 15;
sensor.handleNewDistance('room1', 1);
sensor.distances.get('room1').lastUpdatedAt = new Date(
Date.now() - 20 * 1000
);
sensor.distances.room1.lastUpdatedAt = new Date(Date.now() - 20 * 1000);

sensor.updateState();
expect(sensor.state).toBe(STATE_NOT_HOME);
Expand All @@ -66,7 +62,7 @@ describe('RoomPresenceDistanceSensor', () => {
it('should disable the timeout check if set to 0', () => {
sensor.timeout = 0;
sensor.handleNewDistance('room1', 3.3);
sensor.distances.get('room1').lastUpdatedAt = new Date(
sensor.distances.room1.lastUpdatedAt = new Date(
Date.now() - 60 * 60 * 1000
);

Expand All @@ -77,9 +73,7 @@ describe('RoomPresenceDistanceSensor', () => {
it('should not set the state to not_home if the timeout has not passed yet', () => {
sensor.timeout = 30;
sensor.handleNewDistance('room1', 1.2);
sensor.distances.get('room1').lastUpdatedAt = new Date(
Date.now() - 10 * 1000
);
sensor.distances.room1.lastUpdatedAt = new Date(Date.now() - 10 * 1000);

sensor.updateState();
expect(sensor.state).toBe('room1');
Expand Down Expand Up @@ -108,9 +102,7 @@ describe('RoomPresenceDistanceSensor', () => {
sensor.handleNewDistance('room2', 4);
sensor.handleNewDistance('room3', 5);
sensor.timeout = 10;
sensor.distances.get('room2').lastUpdatedAt = new Date(
Date.now() - 20 * 1000
);
sensor.distances.room2.lastUpdatedAt = new Date(Date.now() - 20 * 1000);
sensor.handleNewDistance('room1', 7);

expect(sensor.state).toBe('room3');
Expand All @@ -130,9 +122,7 @@ describe('RoomPresenceDistanceSensor', () => {
sensor.handleNewDistance('room1', 3);
sensor.handleNewDistance('room2', 4);
sensor.timeout = 5;
sensor.distances.get('room2').lastUpdatedAt = new Date(
Date.now() - 10 * 1000
);
sensor.distances.room2.lastUpdatedAt = new Date(Date.now() - 10 * 1000);
sensor.handleNewDistance('room1', 5, true);

expect(sensor.state).toBe(STATE_NOT_HOME);
Expand Down
Expand Up @@ -15,7 +15,7 @@ class TimedDistance {

export class RoomPresenceDistanceSensor extends Sensor {
timeout: number;
distances = new Map<string, TimedDistance>();
distances: { [instance: string]: TimedDistance } = {};

constructor(id: string, name: string, timeout: number) {
super(id, name, true);
Expand All @@ -34,7 +34,7 @@ export class RoomPresenceDistanceSensor extends Sensor {
distance: number,
outOfRange = false
): void {
this.distances.set(instanceName, new TimedDistance(distance, outOfRange));
this.distances[instanceName] = new TimedDistance(distance, outOfRange);
this.updateState();
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export class RoomPresenceDistanceSensor extends Sensor {
* @returns Tuple of the instance name and the timed distance to it
*/
protected getClosestInRange(): [string, TimedDistance] {
const distances = Array.from(this.distances.entries())
const distances = Array.from(Object.entries(this.distances))
.filter(value => {
return (
!value[1].outOfRange &&
Expand Down

0 comments on commit 567327d

Please sign in to comment.