-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.mts
207 lines (187 loc) · 5.57 KB
/
app.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import 'core-js/actual/object/group-by.js'
import 'source-map-support/register.js'
import {
DeviceModel,
FacadeManager,
HeatzyAPI,
type DeviceFacade,
type LoginPostData,
} from '@olivierzal/heatzy-api'
import { Homey } from './homey.mjs'
import { changelog } from './jsonFiles.mjs'
import type HeatzyDevice from './drivers/heatzy/device.mjs'
import type {
DeviceSettings,
DriverSetting,
LoginSetting,
Manifest,
ManifestDriver,
Settings,
} from './types.mjs'
const NOTIFICATION_DELAY = 10000
const getDriverSettings = (
{ id: driverId, settings }: ManifestDriver,
language: string,
): DriverSetting[] =>
(settings ?? []).flatMap(({ children, id: groupId, label: groupLabel }) =>
(children ?? []).map(({ id, label, max, min, type, units, values }) => ({
driverId,
groupId,
groupLabel: groupLabel[language] ?? groupLabel.en,
id,
max,
min,
title: label[language] ?? label.en,
type,
units,
values: values?.map(({ id: valueId, label: valueLabel }) => ({
id: valueId,
label: valueLabel[language] ?? valueLabel.en,
})),
})),
)
const getDriverLoginSetting = (
{ id: driverId, pair }: ManifestDriver,
language: string,
): DriverSetting[] =>
Object.values(
Object.entries(
pair?.find(
(pairSetting): pairSetting is LoginSetting =>
pairSetting.id === 'login',
)?.options ?? [],
).reduce<Record<string, DriverSetting>>((acc, [option, label]) => {
const isPassword = option.startsWith('password')
const key = isPassword ? 'password' : 'username'
acc[key] ??= {
driverId,
groupId: 'login',
id: key,
title: '',
type: isPassword ? 'password' : 'text',
}
acc[key][option.endsWith('Placeholder') ? 'placeholder' : 'title'] =
label[language] ?? label.en
return acc
}, {}),
)
export default class HeatzyApp extends Homey.App {
readonly #language = this.homey.i18n.getLanguage()
#api!: HeatzyAPI
#facadeManager!: FacadeManager
public get api(): HeatzyAPI {
return this.#api
}
public override async onInit(): Promise<void> {
this.#api = await HeatzyAPI.create({
language: this.#language,
logger: {
error: (...args) => {
this.error(...args)
},
log: (...args) => {
this.log(...args)
},
},
onSync: async () => this.#syncFromDevices(),
settingManager: this.homey.settings,
timezone: this.homey.clock.getTimezone(),
})
this.#facadeManager = new FacadeManager(this.#api)
this.#createNotification()
}
public override async onUninit(): Promise<void> {
this.#api.clearSync()
return Promise.resolve()
}
public getDeviceSettings(): DeviceSettings {
return this.#getDevices().reduce<DeviceSettings>((acc, device) => {
const {
driver: { id: driverId },
} = device
acc[driverId] ??= {}
for (const [id, value] of Object.entries(
device.getSettings() as Settings,
)) {
if (!(id in acc[driverId])) {
acc[driverId][id] = value
} else if (acc[driverId][id] !== value) {
acc[driverId][id] = null
break
}
}
return acc
}, {})
}
public getDriverSettings(): Partial<Record<string, DriverSetting[]>> {
return Object.groupBy(
(this.homey.manifest as Manifest).drivers.flatMap((driver) => [
...getDriverSettings(driver, this.#language),
...getDriverLoginSetting(driver, this.#language),
]),
({ driverId, groupId }) => groupId ?? driverId,
)
}
public getFacade(id: string): DeviceFacade {
const instance = DeviceModel.getById(id)
if (!instance) {
throw new Error(this.homey.__('errors.deviceNotFound'))
}
return this.#facadeManager.get(instance)
}
public async login(data: LoginPostData): Promise<boolean> {
return this.api.authenticate(data)
}
public async setDeviceSettings(settings: Settings): Promise<void> {
await Promise.all(
this.#getDevices().map(async (device) => {
const changedKeys = Object.keys(settings).filter(
(changedKey) =>
settings[changedKey] !== device.getSetting(changedKey),
)
if (changedKeys.length) {
await device.setSettings(
Object.fromEntries(changedKeys.map((key) => [key, settings[key]])),
)
await device.onSettings({
changedKeys,
newSettings: device.getSettings() as Settings,
})
}
}),
)
}
#createNotification(): void {
const { version } = this.homey.manifest as Manifest
if (
this.homey.settings.get('notifiedVersion') !== version &&
version in changelog
) {
const { [version as keyof typeof changelog]: versionChangelog } =
changelog
this.homey.setTimeout(async () => {
try {
await this.homey.notifications.createNotification({
excerpt:
versionChangelog[
this.#language in versionChangelog ?
(this.#language as keyof typeof versionChangelog)
: 'en'
],
})
this.homey.settings.set('notifiedVersion', version)
} catch {}
}, NOTIFICATION_DELAY)
}
}
#getDevices(): HeatzyDevice[] {
return Object.values(this.homey.drivers.getDrivers()).flatMap(
(driver) => driver.getDevices() as HeatzyDevice[],
)
}
async #syncFromDevices(): Promise<void> {
await Promise.all(
this.#getDevices().map(async (device) => device.syncFromDevice()),
)
}
}