Skip to content

Commit

Permalink
Add ClimateEntity and LockEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBaulig committed Mar 1, 2024
1 parent d43a205 commit 141c12b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/entities/ClimateEntity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import Entity from '../Entity';
import filterObject from '../filterObject';

export default class ClimateEntity extends Entity {
constructor(controller, data) {
super(controller, data);
}

async set({mode, target_temperature_high, target_temperature_low, target_temperature} = {}) {
const query = filterObject({
mode,
target_temperature_high,
target_temperature_low,
target_temperature
});

const args = [this.getPostURL('set')];
if (Object.values(query).length > 0) {
args.push(query);
}

return this.controller.post(...args);
}
}
19 changes: 19 additions & 0 deletions src/entities/LockEntity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Entity from '../Entity';

export default class LockEntity extends Entity {
constructor(controller, data) {
super(controller, data);
}

async open() {
return this.controller.post(this.getPostURL('open'));
}

async lock() {
return this.controller.post(this.getPostURL('lock'));
}

async unlock() {
return this.controller.post(this.getPostURL('unlock'));
}
}
4 changes: 4 additions & 0 deletions src/entities/createEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import FanEntity from './FanEntity';
import SensorEntity from './SensorEntity';
import TextSensorEntity from './TextSensorEntity';
import TextEntity from './TextEntity';
import LockEntity from './LockEntity';
import ClimateEntity from './ClimateEntity';

const entityTypeMap = {
'binary_sensor': BinarySensorEntity,
Expand All @@ -23,6 +25,8 @@ const entityTypeMap = {
'sensor': SensorEntity,
'text_sensor': TextSensorEntity,
'text': TextEntity,
'lock': LockEntity,
'climate': ClimateEntity,
}


Expand Down
19 changes: 19 additions & 0 deletions src/entities/tests/ClimateEntity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {jest, expect, test} from '@jest/globals';
import {createController, getLastController} from '../../tests/helpers.js';
import ClimateEntity from '../ClimateEntity';

const createData = jest.fn(() => ({
id: 'climate-name',
state: 'HEATING',
}));

function createClimateEntity({controller = createController(), data = createData()} = {}) {
return new ClimateEntity(controller, data);
}

test('it should post to set when set is called', () =>{
const entity = createClimateEntity();
const controller = getLastController();
entity.set({mode: 'HEAT_COOL', target_temperature_high: 72, target_temperature_low: 68});
expect(controller.post).toHaveBeenLastCalledWith(entity.getPostURL('set'), {mode: 'HEAT_COOL', target_temperature_high: 72, target_temperature_low: 68});
});
34 changes: 34 additions & 0 deletions src/entities/tests/LockEntity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {jest, expect, test} from '@jest/globals';
import {createController, getLastController} from '../../tests/helpers.js';
import LockEntity from '../LockEntity';

const createData = jest.fn((extra = {}) => Object.assign({
id: 'lock-name',
state: 'UNLOCKED',
}, extra));


function createLockEntity({controller = createController(), data = createData()} = {}) {
return new LockEntity(controller, data);
}

test('it should post to open when open is called', () =>{
const entity = createLockEntity();
const controller = getLastController();
entity.open();
expect(controller.post).toHaveBeenLastCalledWith(entity.getPostURL('open'));
});

test('it should post to lock when lock is called', () =>{
const entity = createLockEntity();
const controller = getLastController();
entity.lock();
expect(controller.post).toHaveBeenLastCalledWith(entity.getPostURL('lock'));
});

test('it should post to unlock when unlock is called', () =>{
const entity = createLockEntity();
const controller = getLastController();
entity.unlock();
expect(controller.post).toHaveBeenLastCalledWith(entity.getPostURL('unlock'));
});

0 comments on commit 141c12b

Please sign in to comment.