From 141c12b47eb13649bc7c618027071808b000248d Mon Sep 17 00:00:00 2001 From: Daniel Baulig Date: Thu, 29 Feb 2024 18:40:56 -0800 Subject: [PATCH] Add ClimateEntity and LockEntity --- src/entities/ClimateEntity.js | 25 +++++++++++++++++ src/entities/LockEntity.js | 19 +++++++++++++ src/entities/createEntity.js | 4 +++ src/entities/tests/ClimateEntity.test.js | 19 +++++++++++++ src/entities/tests/LockEntity.test.js | 34 ++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 src/entities/ClimateEntity.js create mode 100644 src/entities/LockEntity.js create mode 100644 src/entities/tests/ClimateEntity.test.js create mode 100644 src/entities/tests/LockEntity.test.js diff --git a/src/entities/ClimateEntity.js b/src/entities/ClimateEntity.js new file mode 100644 index 0000000..1572891 --- /dev/null +++ b/src/entities/ClimateEntity.js @@ -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); + } +} diff --git a/src/entities/LockEntity.js b/src/entities/LockEntity.js new file mode 100644 index 0000000..277ccf3 --- /dev/null +++ b/src/entities/LockEntity.js @@ -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')); + } +} diff --git a/src/entities/createEntity.js b/src/entities/createEntity.js index 6eb38b4..37e1df8 100644 --- a/src/entities/createEntity.js +++ b/src/entities/createEntity.js @@ -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, @@ -23,6 +25,8 @@ const entityTypeMap = { 'sensor': SensorEntity, 'text_sensor': TextSensorEntity, 'text': TextEntity, + 'lock': LockEntity, + 'climate': ClimateEntity, } diff --git a/src/entities/tests/ClimateEntity.test.js b/src/entities/tests/ClimateEntity.test.js new file mode 100644 index 0000000..9277e12 --- /dev/null +++ b/src/entities/tests/ClimateEntity.test.js @@ -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}); +}); diff --git a/src/entities/tests/LockEntity.test.js b/src/entities/tests/LockEntity.test.js new file mode 100644 index 0000000..6ca404e --- /dev/null +++ b/src/entities/tests/LockEntity.test.js @@ -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')); +});