summaryrefslogtreecommitdiff
path: root/src/device.ts
blob: bd9211e42c67a373ab4f19205c5db0bd79500210 (plain)
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
import type { Device as Miio } from 'miio';

const enum Power {
  Off,
  On,
}

const enum OperationMode {
  Auto = 1,
  Cool,
  Dry,
  Fan,
  Heat,
}

export const enum WindLevel {
  Auto,
  Level1,
  Level2,
  Level3,
  Level4,
  Level5,
  Level6,
  Level7,
}

export const enum Swing {
  Off,
  On,
}

type DeviceState = {
  power: Power;
  mode: OperationMode;
  settemp: number;
  temperature: number;
  wind_level: WindLevel;
  swing: Swing;
};

export class Device {
  private state: DeviceState = {
    power: Power.Off,
    mode: OperationMode.Cool,
    settemp: 26,
    temperature: 26,
    wind_level: WindLevel.Auto,
    swing: Swing.Off,
  };

  constructor(
    private readonly miio: Miio,
  ) {
    //
  }

  deviceId() {
    return this.miio.handle.api.id;
  }

  isOn() {
    return this.state.power === Power.On;
  }

  isOff() {
    return ! this.isOn();
  }

  async turnOn() {
    this.state.power = Power.On;
    await this.miio.call('set_power', [this.state.power]);
    await this.refresh();
  }

  async turnOff() {
    this.state.power = Power.Off;
    await this.miio.call('set_power', [this.state.power]);
    await this.refresh();
  }

  isCooling() {
    return this.state.mode === OperationMode.Cool;
  }

  isHeating() {
    return this.state.mode === OperationMode.Heat;
  }

  async auto() {
    this.state.mode = OperationMode.Auto;
    await this.miio.call('set_mode', [this.state.mode]);
    await this.refresh();
  }

  async cooling() {
    this.state.mode = OperationMode.Cool;
    await this.miio.call('set_mode', [this.state.mode]);
    await this.refresh();
  }

  async heating() {
    this.state.mode = OperationMode.Heat;
    await this.miio.call('set_mode', [this.state.mode]);
    await this.refresh();
  }

  async temperatureSetTo(temperature: number) {
    this.state.settemp = temperature;
    await this.miio.call('set_temp', [this.state.settemp]);
    await this.refresh();
  }

  temperature() {
    return this.state.temperature;
  }

  targetTemperature() {
    return this.state.settemp;
  }

  async windLevelSetTo(level: WindLevel) {
    this.state.wind_level = level;
    await this.miio.call('set_wind_level', [level]);
    await this.refresh();
  }

  windLevel() {
    return this.state.wind_level;
  }

  async startSwinging() {
    this.state.swing = Swing.On;
    await this.miio.call('set_swing', [this.state.swing]);
    await this.refresh();
  }

  async stopSwinging() {
    this.state.swing = Swing.Off;
    await this.miio.call('set_swing', [this.state.swing]);
    await this.refresh();
  }

  swinging() {
    return this.state.swing;
  }

  async refresh() {
    const attributes = Object.keys(this.state);
    const values = await this.miio.call('get_prop', attributes);

    this.state = attributes.reduce(
      (carry, attr, i) => ({ ...carry, [attr]: values[i] }),
      { ...this.state },
    );

    return this;
  }

  getRawState() {
    return this.state;
  }
}