-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.mts
59 lines (55 loc) · 1.71 KB
/
api.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
import {
MEASURE_TEMPERATURE,
OUTDOOR_TEMPERATURE,
type Capability,
type TemperatureListenerData,
type TemperatureSensor,
} from './types.mts'
import type { Homey } from 'homey/lib/Homey'
class AtaDeviceNotFoundError extends Error {
public constructor() {
super('ataDeviceNotFound')
}
}
const api = {
async autoAdjustCooling({
body,
homey,
}: {
body: TemperatureListenerData
homey: Homey
}): Promise<void> {
await homey.app.autoAdjustCooling(body)
},
getLanguage({ homey }: { homey: Homey }): string {
return homey.i18n.getLanguage()
},
getTemperatureSensors({ homey }: { homey: Homey }): TemperatureSensor[] {
const { app } = homey
if (!app.melcloudDevices.length) {
throw new AtaDeviceNotFoundError()
}
return app.temperatureSensors
.flatMap((device) => {
const capabilities = Object.values(
// @ts-expect-error: `homey-api` is partially typed
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(device.capabilitiesObj as Record<string, Capability> | null) ?? {},
).filter(({ id }) => id.startsWith(MEASURE_TEMPERATURE))
const outdoorCapability = capabilities.find(
({ id }) =>
app.melcloudDevices.includes(device) && id === OUTDOOR_TEMPERATURE,
)
return (outdoorCapability ? [outdoorCapability] : capabilities).map(
({ id, title }): TemperatureSensor => ({
capabilityName: `${device.name} - ${title}`,
capabilityPath: `${device.id}:${id}`,
}),
)
})
.sort((device1, device2) =>
device1.capabilityName.localeCompare(device2.capabilityName),
)
},
}
export default api