UNPKG

dirigera

Version:

A TypeScript client for IKEA's DIRIGERA smart home hub

41 lines (37 loc) 1.02 kB
import type { Got } from 'got' import type { Device } from '../types/device/Device.ts' import type { Controller, GenericSwitch } from '../types/device/Controller.ts' export default (got: Got) => { return { async list() { const devices = await got.get(`devices`).json<Device[]>() return devices.filter((d): d is Controller => d.type === 'controller') }, async get({ id }: { id: string }) { const device = await got.get(`devices/${id}`).json<Device>() if (device.type !== 'controller') { throw new Error('The requested device is not a controller') } return device as Controller }, async setControlMode({ id, controlMode, }: { id: string controlMode: GenericSwitch['attributes']['controlMode'] }) { await got .patch(`devices/${id}`, { json: [ { attributes: { controlMode, }, }, ], }) .json() }, } }