diff --git a/.changeset/witty-rules-toggle.md b/.changeset/witty-rules-toggle.md new file mode 100644 index 0000000..336fdda --- /dev/null +++ b/.changeset/witty-rules-toggle.md @@ -0,0 +1,5 @@ +--- +'@smartthings/core-sdk': minor +--- + +Add enable and disable methods to RulesEndpoint diff --git a/src/endpoint/rules.ts b/src/endpoint/rules.ts index c7128ef..af5d862 100644 --- a/src/endpoint/rules.ts +++ b/src/endpoint/rules.ts @@ -414,4 +414,24 @@ export class RulesEndpoint extends Endpoint { public async execute(id: string, locationId?: string): Promise { return this.client.post(`execute/${id}`, undefined, { locationId: this.locationId(locationId) }) } + + /** + * Enable a rule + * @param id UUID of the rule + * @param locationId UUID of the location, If the client is configured with a location ID this parameter + * can be omitted + */ + public async enable(id: string, locationId?: string): Promise { + await this.client.put(`${id}/status/enable`, undefined, { locationId: this.locationId(locationId) }) + } + + /** + * Disable a rule + * @param id UUID of the rule + * @param locationId UUID of the location, If the client is configured with a location ID this parameter + * can be omitted + */ + public async disable(id: string, locationId?: string): Promise { + await this.client.put(`${id}/status/disable`, undefined, { locationId: this.locationId(locationId) }) + } } diff --git a/test/unit/rules.test.ts b/test/unit/rules.test.ts index 02736d5..0e3bd39 100644 --- a/test/unit/rules.test.ts +++ b/test/unit/rules.test.ts @@ -86,4 +86,26 @@ describe('RulesEndpoint', () => { expect(postSpy).toHaveBeenCalledTimes(1) expect(postSpy).toHaveBeenCalledWith('execute/id-of-rule-to-execute', undefined, { locationId: 'final-location-id' }) }) + + test('enable', async () => { + putSpy.mockResolvedValueOnce(undefined) + + await rulesEndpoint.enable('id-of-rule-to-enable', 'input-location-id') + + expect(putSpy).toHaveBeenCalledTimes(1) + expect(putSpy).toHaveBeenCalledWith('id-of-rule-to-enable/status/enable', undefined, { locationId: 'final-location-id' }) + expect(locationIdMock).toHaveBeenCalledTimes(1) + expect(locationIdMock).toHaveBeenCalledWith('input-location-id') + }) + + test('disable', async () => { + putSpy.mockResolvedValueOnce(undefined) + + await rulesEndpoint.disable('id-of-rule-to-disable', 'input-location-id') + + expect(putSpy).toHaveBeenCalledTimes(1) + expect(putSpy).toHaveBeenCalledWith('id-of-rule-to-disable/status/disable', undefined, { locationId: 'final-location-id' }) + expect(locationIdMock).toHaveBeenCalledTimes(1) + expect(locationIdMock).toHaveBeenCalledWith('input-location-id') + }) })