0

Create Schedule

by
Published Nov 5, 2024
Script enode Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Enode = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Enode,
8
	userId: string,
9
	body:
10
		| ({
11
				isEnabled?: false | true
12
				defaultShouldCharge?: false | true
13
				rules?: {
14
					hourMinute?: { from: string; to: string }
15
					fromTimestamp?: string
16
					toTimestamp?: string
17
					weekdays?: 0 | 1 | 2 | 3 | 4 | 5 | 6[]
18
				} & { shouldCharge: false | true }[]
19
				targetId?: string
20
				targetType?: 'vehicle' | 'charger'
21
				locationId?: string
22
		  } & {})
23
		| ({
24
				isEnabled?: false | true
25
				targetId?: string
26
				targetType?: 'hvac'
27
				defaultTargetState?:
28
					| ({ coolSetpoint: number; mode: 'COOL'; holdType: 'PERMANENT' } & {})
29
					| ({ heatSetpoint: number; mode: 'HEAT'; holdType: 'PERMANENT' } & {})
30
					| ({
31
							coolSetpoint: number
32
							heatSetpoint: number
33
							mode: 'AUTO'
34
							holdType: 'PERMANENT'
35
					  } & {})
36
					| ({ mode: 'OFF'; holdType: 'PERMANENT' } & {})
37
					| ({ holdType: 'SCHEDULED' } & {})
38
				rules?: {
39
					hourMinute?: { from: string; to: string }
40
					fromTimestamp?: string
41
					toTimestamp?: string
42
					weekdays?: 0 | 1 | 2 | 3 | 4 | 5 | 6[]
43
				} & {
44
					targetState:
45
						| ({
46
								coolSetpoint: number
47
								mode: 'COOL'
48
								holdType: 'PERMANENT'
49
						  } & {})
50
						| ({
51
								heatSetpoint: number
52
								mode: 'HEAT'
53
								holdType: 'PERMANENT'
54
						  } & {})
55
						| ({
56
								coolSetpoint: number
57
								heatSetpoint: number
58
								mode: 'AUTO'
59
								holdType: 'PERMANENT'
60
						  } & {})
61
						| ({ mode: 'OFF'; holdType: 'PERMANENT' } & {})
62
						| ({ holdType: 'SCHEDULED' } & {})
63
				}[]
64
		  } & {})
65
) {
66
	const url = new URL(`https://enode-api.production.enode.io/users/${userId}/schedules`)
67

68
	const response = await fetch(url, {
69
		method: 'POST',
70
		headers: {
71
			'Content-Type': 'application/json',
72
			Authorization: 'Bearer ' + auth.token
73
		},
74
		body: JSON.stringify(body)
75
	})
76

77
	if (!response.ok) {
78
		const text = await response.text()
79
		throw new Error(`${response.status} ${text}`)
80
	}
81

82
	return await response.json()
83
}
84