import {z} from 'zod' export type CapabilityConfigurationValue = {key: string, range?: Array | undefined, enabledValues?: Array | undefined, step?: number | undefined}; export type CapabilityConfiguration = {values: Array}; export type Restriction = {tier: number, historyRetentionTTLDays?: number | undefined, visibleWhenRestricted?: boolean | undefined}; export type CapabilityReferenceRequest = {id: string, version?: number | undefined, optional?: boolean | undefined, config?: CapabilityConfiguration | undefined, restrictions?: Restriction | undefined}; export type DeviceCategory = {name: string, categoryType: 'manufacturer' | 'user'}; export type DeviceComponentRequest = {label?: string | undefined, id: unknown, capabilities: Array, categories: Array, restrictions?: Restriction | undefined, optional?: boolean | undefined}; export type DeviceProfileMetadata = unknown; export type PreferenceType = 'integer' | 'number' | 'boolean' | 'string' | 'enumeration'; export type PreferenceRequest = {preferenceId?: string | undefined, name: string, title: string, description?: string | undefined, explicit?: boolean | undefined, required?: boolean | undefined, preferenceType: PreferenceType, definition: {minimum?: number | undefined, maximum?: number | undefined, minLength?: number | undefined, maxLength?: number | undefined, default: unknown, stringType?: ('text' | 'password' | 'paragraph') | undefined, options?: {[key: string]: string} | undefined}}; export type UpdateDeviceProfileRequest = {components?: Array | undefined, metadata?: DeviceProfileMetadata | undefined, preferences?: Array | undefined, presentationId?: string | undefined}; export type UpdateApiDeviceprofilesDeviceProfileIdArgs = {body: UpdateDeviceProfileRequest}; export type UpdateApiDeviceprofilesDeviceProfileIdOptions = {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 }; /** * Update a Device Profile * * PUT /deviceprofiles/{deviceProfileId} */ export const updateApiDeviceprofilesDeviceProfileId = async (args: UpdateApiDeviceprofilesDeviceProfileIdArgs, options?: UpdateApiDeviceprofilesDeviceProfileIdOptions) => { const {baseUrl = "https://api.smartthings.com/v1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "PUT", headers, body: JSON.stringify(args.body)}) 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()) };