-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathswitchDevice.js
More file actions
71 lines (59 loc) · 2.57 KB
/
Copy pathswitchDevice.js
File metadata and controls
71 lines (59 loc) · 2.57 KB
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
// -----------------------------------------------------------------------------
// Device type: SWITCH
// Illustrates a simple binary ON/OFF actuator.
// -----------------------------------------------------------------------------
import {
createLogger,
DEVICE_FEATURE_CATEGORIES,
DEVICE_FEATURE_TYPES,
} from '@gladysassistant/integration-sdk';
const DEVICE_TYPE = 'switch';
const logger = createLogger({ name: DEVICE_TYPE });
// Unique id coming from the external platform (simulated here). External ids
// must be globally unique and stable across restarts: they are how Gladys
// matches states to devices. `gladys.externalIds()` builds them for you.
const PLATFORM_DEVICE_ID = 'sw-8a3f2c';
const FEATURE = { ON_OFF: 'on-off' };
// In-memory reflection of the device state. In a real integration this IS the
// state of your physical device: you read/write it, you do not simulate it.
let isOn = false;
export const switchDevice = {
key: DEVICE_TYPE,
deviceExternalId(gladys) {
return gladys.externalIds(DEVICE_TYPE, PLATFORM_DEVICE_ID).device;
},
buildDevice(gladys) {
const ids = gladys.externalIds(DEVICE_TYPE, PLATFORM_DEVICE_ID);
return {
name: 'Living room switch',
external_id: ids.device,
features: [
{
name: 'On/Off',
external_id: ids.feature(FEATURE.ON_OFF),
category: DEVICE_FEATURE_CATEGORIES.SWITCH,
type: DEVICE_FEATURE_TYPES.SWITCH.BINARY,
read_only: false, // actuator: the user can control it
has_feedback: true, // the device confirms its new state
keep_history: true,
},
],
};
},
async onSetValue(gladys, { feature, value }) {
const on = value === 1;
logger.info(`Command received: ${isOn ? 'ON' : 'OFF'} -> ${on ? 'ON' : 'OFF'}`);
// ------------------------------------------------------------------ //
// DO THE WORK: send the order to the real relay / plug.
// e.g. await mqttClient.publish('living-room/switch/set', on ? 'ON' : 'OFF');
// await cloudApi.setSwitch(PLATFORM_DEVICE_ID, on);
// If the device does not answer, `throw`: the SDK then sends a
// success:false acknowledgement to Gladys and the UI shows the failure.
// ------------------------------------------------------------------ //
isOn = on;
// has_feedback = true -> publish the state confirmed by the device.
// With a real feedback device you would publish the state reported in its
// confirmation message, not the value you just requested.
await gladys.publishState(feature.external_id, on ? 1 : 0);
},
};