import {z} from 'zod' export type GetApiDeviceprofilesDeviceProfileIdArgs = {'Accept-Language'?: string | undefined}; export type GetApiDeviceprofilesDeviceProfileIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const deviceProfileMetadata = z.unknown(); export const deviceRestriction = z.object({tier: z.number().int(), historyRetentionTTLDays: z.number().int().optional(), visibleWhenRestricted: z.boolean().optional()}); export const deviceProfileStatus = z.enum(["DEVELOPMENT", "PUBLISHED"]); export const preferenceType = z.enum(["integer", "number", "boolean", "string", "enumeration"]); export const preferenceResponse = z.object({preferenceId: z.string().optional(), name: z.string().optional(), title: z.string(), description: z.string().optional(), required: z.boolean().optional(), explicit: z.boolean().optional(), preferenceType: preferenceType}); export const capabilityConfigurationValue = z.object({key: z.string(), range: z.array(z.number()).optional(), enabledValues: z.array(z.string()).optional(), step: z.number().optional()}); export const capabilityConfiguration = z.object({values: z.array(capabilityConfigurationValue)}); export const restriction = z.object({tier: z.number().int(), historyRetentionTTLDays: z.number().int().optional(), visibleWhenRestricted: z.boolean().optional()}); export const attributeState = z.object({value: z.object({}).optional(), unit: z.string().optional(), data: z.record(z.string(), z.object({})).optional(), timestamp: z.string().optional()}); export const capabilityStatus = z.record(z.string(), attributeState); export const deviceCapabilityReference = z.object({id: z.string(), version: z.number().int().optional(), optional: z.boolean().optional(), config: capabilityConfiguration.optional(), restrictions: restriction.optional(), ephemeral: z.boolean().optional(), status: capabilityStatus.optional()}); export const deviceCategory = z.object({name: z.string(), categoryType: z.enum(["manufacturer", "user"])}); export const deviceComponent = z.object({id: z.string(), label: z.string().optional(), capabilities: z.array(deviceCapabilityReference), categories: z.array(deviceCategory), restrictions: restriction.optional(), optional: z.boolean().optional()}); export const deviceProfileResponse = z.object({id: z.string(), name: z.string(), metadata: deviceProfileMetadata.optional(), restrictions: deviceRestriction.optional(), presentationId: z.string().optional(), migrationStatus: z.enum(["MIGRATED", "NOT_MIGRATED"]), status: deviceProfileStatus, preferences: z.array(preferenceResponse).optional(), components: z.array(deviceComponent)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get a Device Profile * * GET /deviceprofiles/{deviceProfileId} */ export const getApiDeviceprofilesDeviceProfileId = async (args: GetApiDeviceprofilesDeviceProfileIdArgs, options?: GetApiDeviceprofilesDeviceProfileIdOptions) => { const {baseUrl = "https://api.smartthings.com/v1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/deviceprofiles/${encodeURIComponent(String(args.deviceProfileId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deviceProfileResponse, await res.json()) };