@homelib/core 0.0.0-wip.01265bf1cc90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +11 -0
  3. package/bld/exports/mobx.d.ts +1 -0
  4. package/bld/exports/mobx.js +2 -0
  5. package/bld/exports/mobx.js.map +1 -0
  6. package/bld/library/@lifecycle.d.ts +4 -0
  7. package/bld/library/@lifecycle.js +23 -0
  8. package/bld/library/@lifecycle.js.map +1 -0
  9. package/bld/library/atomics/index.d.ts +1 -0
  10. package/bld/library/atomics/index.js +2 -0
  11. package/bld/library/atomics/index.js.map +1 -0
  12. package/bld/library/atomics/temperature.d.ts +13 -0
  13. package/bld/library/atomics/temperature.js +53 -0
  14. package/bld/library/atomics/temperature.js.map +1 -0
  15. package/bld/library/command.d.ts +12 -0
  16. package/bld/library/command.js +21 -0
  17. package/bld/library/command.js.map +1 -0
  18. package/bld/library/device/index.d.ts +1 -0
  19. package/bld/library/device/index.js +2 -0
  20. package/bld/library/device/index.js.map +1 -0
  21. package/bld/library/device/sensor.d.ts +8 -0
  22. package/bld/library/device/sensor.js +2 -0
  23. package/bld/library/device/sensor.js.map +1 -0
  24. package/bld/library/device-namespace.d.ts +13 -0
  25. package/bld/library/device-namespace.js +11 -0
  26. package/bld/library/device-namespace.js.map +1 -0
  27. package/bld/library/device.d.ts +23 -0
  28. package/bld/library/device.js +92 -0
  29. package/bld/library/device.js.map +1 -0
  30. package/bld/library/devices/air-conditioner.d.ts +72 -0
  31. package/bld/library/devices/air-conditioner.js +246 -0
  32. package/bld/library/devices/air-conditioner.js.map +1 -0
  33. package/bld/library/devices/dehumidifier.d.ts +62 -0
  34. package/bld/library/devices/dehumidifier.js +211 -0
  35. package/bld/library/devices/dehumidifier.js.map +1 -0
  36. package/bld/library/devices/fan.d.ts +70 -0
  37. package/bld/library/devices/fan.js +229 -0
  38. package/bld/library/devices/fan.js.map +1 -0
  39. package/bld/library/devices/index.d.ts +6 -0
  40. package/bld/library/devices/index.js +7 -0
  41. package/bld/library/devices/index.js.map +1 -0
  42. package/bld/library/devices/light.d.ts +59 -0
  43. package/bld/library/devices/light.js +197 -0
  44. package/bld/library/devices/light.js.map +1 -0
  45. package/bld/library/devices/pet-feeder.d.ts +36 -0
  46. package/bld/library/devices/pet-feeder.js +115 -0
  47. package/bld/library/devices/pet-feeder.js.map +1 -0
  48. package/bld/library/devices/temperature-humidity-sensor.d.ts +18 -0
  49. package/bld/library/devices/temperature-humidity-sensor.js +86 -0
  50. package/bld/library/devices/temperature-humidity-sensor.js.map +1 -0
  51. package/bld/library/endpoint.d.ts +100 -0
  52. package/bld/library/endpoint.js +431 -0
  53. package/bld/library/endpoint.js.map +1 -0
  54. package/bld/library/home.d.ts +4 -0
  55. package/bld/library/home.js +11 -0
  56. package/bld/library/home.js.map +1 -0
  57. package/bld/library/index.d.ts +16 -0
  58. package/bld/library/index.js +17 -0
  59. package/bld/library/index.js.map +1 -0
  60. package/bld/library/log.d.ts +43 -0
  61. package/bld/library/log.js +78 -0
  62. package/bld/library/log.js.map +1 -0
  63. package/bld/library/provider.d.ts +26 -0
  64. package/bld/library/provider.js +18 -0
  65. package/bld/library/provider.js.map +1 -0
  66. package/bld/library/registry.d.ts +19 -0
  67. package/bld/library/registry.js +65 -0
  68. package/bld/library/registry.js.map +1 -0
  69. package/bld/library/runtime/@binding-creation.d.ts +4 -0
  70. package/bld/library/runtime/@binding-creation.js +19 -0
  71. package/bld/library/runtime/@binding-creation.js.map +1 -0
  72. package/bld/library/runtime/binding.d.ts +50 -0
  73. package/bld/library/runtime/binding.js +108 -0
  74. package/bld/library/runtime/binding.js.map +1 -0
  75. package/bld/library/runtime/bootstrap-frontend.d.ts +48 -0
  76. package/bld/library/runtime/bootstrap-frontend.js +96 -0
  77. package/bld/library/runtime/bootstrap-frontend.js.map +1 -0
  78. package/bld/library/runtime/bootstrap.d.ts +9 -0
  79. package/bld/library/runtime/bootstrap.js +165 -0
  80. package/bld/library/runtime/bootstrap.js.map +1 -0
  81. package/bld/library/runtime/environment.d.ts +1 -0
  82. package/bld/library/runtime/environment.js +7 -0
  83. package/bld/library/runtime/environment.js.map +1 -0
  84. package/bld/library/runtime/index.d.ts +4 -0
  85. package/bld/library/runtime/index.js +5 -0
  86. package/bld/library/runtime/index.js.map +1 -0
  87. package/bld/library/scope.d.ts +41 -0
  88. package/bld/library/scope.js +132 -0
  89. package/bld/library/scope.js.map +1 -0
  90. package/bld/library/types.d.ts +17 -0
  91. package/bld/library/types.js +8 -0
  92. package/bld/library/types.js.map +1 -0
  93. package/bld/library/utils/backoff.d.ts +10 -0
  94. package/bld/library/utils/backoff.js +63 -0
  95. package/bld/library/utils/backoff.js.map +1 -0
  96. package/bld/library/utils/constructor.d.ts +21 -0
  97. package/bld/library/utils/constructor.js +30 -0
  98. package/bld/library/utils/constructor.js.map +1 -0
  99. package/bld/library/utils/index.d.ts +2 -0
  100. package/bld/library/utils/index.js +3 -0
  101. package/bld/library/utils/index.js.map +1 -0
  102. package/bld/library/x-data-medium.d.ts +12 -0
  103. package/bld/library/x-data-medium.js +3 -0
  104. package/bld/library/x-data-medium.js.map +1 -0
  105. package/package.json +27 -0
  106. package/src/exports/mobx.ts +1 -0
  107. package/src/library/@lifecycle.ts +30 -0
  108. package/src/library/atomics/index.ts +1 -0
  109. package/src/library/atomics/temperature.ts +60 -0
  110. package/src/library/command.ts +25 -0
  111. package/src/library/device/index.ts +1 -0
  112. package/src/library/device/sensor.ts +10 -0
  113. package/src/library/device-namespace.ts +32 -0
  114. package/src/library/device.ts +91 -0
  115. package/src/library/devices/air-conditioner.ts +234 -0
  116. package/src/library/devices/dehumidifier.ts +200 -0
  117. package/src/library/devices/fan.ts +204 -0
  118. package/src/library/devices/index.ts +6 -0
  119. package/src/library/devices/light.ts +177 -0
  120. package/src/library/devices/pet-feeder.ts +98 -0
  121. package/src/library/devices/temperature-humidity-sensor.ts +67 -0
  122. package/src/library/endpoint.ts +610 -0
  123. package/src/library/home.ts +11 -0
  124. package/src/library/index.ts +16 -0
  125. package/src/library/log.ts +163 -0
  126. package/src/library/provider.ts +66 -0
  127. package/src/library/registry.ts +129 -0
  128. package/src/library/runtime/@binding-creation.ts +48 -0
  129. package/src/library/runtime/binding.ts +165 -0
  130. package/src/library/runtime/bootstrap-frontend.ts +243 -0
  131. package/src/library/runtime/bootstrap.ts +286 -0
  132. package/src/library/runtime/environment.ts +6 -0
  133. package/src/library/runtime/index.ts +4 -0
  134. package/src/library/scope.ts +193 -0
  135. package/src/library/types/global.d.ts +8 -0
  136. package/src/library/types.ts +26 -0
  137. package/src/library/utils/backoff.ts +65 -0
  138. package/src/library/utils/constructor.ts +89 -0
  139. package/src/library/utils/index.ts +2 -0
  140. package/src/library/x-data-medium.ts +16 -0
@@ -0,0 +1,30 @@
1
+ let phase: RuntimePhase = 'declaring';
2
+
3
+ export function assertDeclaring(): void {
4
+ if (phase !== 'declaring') {
5
+ throw new Error('Logical declarations are closed.');
6
+ }
7
+ }
8
+
9
+ export function beginBootstrap(): void {
10
+ assertDeclaring();
11
+ phase = 'starting';
12
+ }
13
+
14
+ export function completeBootstrap(): void {
15
+ if (phase !== 'starting') {
16
+ throw new Error(`Cannot complete homelib bootstrap from phase: ${phase}.`);
17
+ }
18
+
19
+ phase = 'running';
20
+ }
21
+
22
+ export function failBootstrap(): void {
23
+ if (phase !== 'starting') {
24
+ throw new Error(`Cannot fail homelib bootstrap from phase: ${phase}.`);
25
+ }
26
+
27
+ phase = 'failed';
28
+ }
29
+
30
+ type RuntimePhase = 'declaring' | 'starting' | 'running' | 'failed';
@@ -0,0 +1 @@
1
+ export * from './temperature.js';
@@ -0,0 +1,60 @@
1
+ const CELSIUS_ZERO_IN_KELVIN = 273.15;
2
+ const CELSIUS_ZERO_IN_FAHRENHEIT = 32;
3
+ const FAHRENHEIT_PER_CELSIUS = 9 / 5;
4
+
5
+ export class Temperature {
6
+ static fromKelvin(value: number): Temperature {
7
+ const celsius = value - CELSIUS_ZERO_IN_KELVIN;
8
+
9
+ return new Temperature(
10
+ value,
11
+ celsius,
12
+ celsius * FAHRENHEIT_PER_CELSIUS + CELSIUS_ZERO_IN_FAHRENHEIT,
13
+ );
14
+ }
15
+
16
+ static fromCelsius(value: number): Temperature {
17
+ return new Temperature(
18
+ value + CELSIUS_ZERO_IN_KELVIN,
19
+ value,
20
+ value * FAHRENHEIT_PER_CELSIUS + CELSIUS_ZERO_IN_FAHRENHEIT,
21
+ );
22
+ }
23
+
24
+ static fromFahrenheit(value: number): Temperature {
25
+ const celsius =
26
+ (value - CELSIUS_ZERO_IN_FAHRENHEIT) / FAHRENHEIT_PER_CELSIUS;
27
+
28
+ return new Temperature(celsius + CELSIUS_ZERO_IN_KELVIN, celsius, value);
29
+ }
30
+
31
+ get kelvin(): number {
32
+ return this.valueInKelvin;
33
+ }
34
+
35
+ get celsius(): number {
36
+ return this.valueInCelsius;
37
+ }
38
+
39
+ get fahrenheit(): number {
40
+ return this.valueInFahrenheit;
41
+ }
42
+
43
+ private constructor(
44
+ private readonly valueInKelvin: number,
45
+ private readonly valueInCelsius: number,
46
+ private readonly valueInFahrenheit: number,
47
+ ) {
48
+ if (!Number.isFinite(valueInKelvin) || valueInKelvin < 0) {
49
+ throw new RangeError(
50
+ 'Temperature must be finite and at or above absolute zero.',
51
+ );
52
+ }
53
+
54
+ Object.freeze(this);
55
+ }
56
+
57
+ toString(): string {
58
+ return `${this.celsius} °C`;
59
+ }
60
+ }
@@ -0,0 +1,25 @@
1
+ export abstract class Command {
2
+ declare private readonly commandBrand: void;
3
+
4
+ toLogString(): string {
5
+ return this.constructor.name;
6
+ }
7
+
8
+ supersedes(_command: Command): boolean {
9
+ return false;
10
+ }
11
+ }
12
+
13
+ /** A desired-state command whose effect can be compared with state and commands. */
14
+ export abstract class StatefulCommand extends Command {
15
+ override supersedes(command: Command): boolean {
16
+ return command.constructor === this.constructor;
17
+ }
18
+ }
19
+
20
+ export class CommandError extends Error {
21
+ constructor(message: string) {
22
+ super(message);
23
+ this.name = new.target.name;
24
+ }
25
+ }
@@ -0,0 +1 @@
1
+ export * from './sensor.js';
@@ -0,0 +1,10 @@
1
+ import type {Temperature} from '../atomics/index.js';
2
+
3
+ export type TemperatureSensor = {
4
+ readonly temperature: Temperature | undefined;
5
+ };
6
+
7
+ export type HumiditySensor = {
8
+ /** Relative humidity as a normalized ratio from 0 to 1. */
9
+ readonly relativeHumidity: number | undefined;
10
+ };
@@ -0,0 +1,32 @@
1
+ import {
2
+ AirConditioner,
3
+ Dehumidifier,
4
+ Fan,
5
+ Light,
6
+ PetFeeder,
7
+ TemperatureHumiditySensor,
8
+ } from './devices/index.js';
9
+ import {register} from './registry.js';
10
+
11
+ register({
12
+ airConditioner: AirConditioner,
13
+ dehumidifier: Dehumidifier,
14
+ fan: Fan,
15
+ light: Light,
16
+ petFeeder: PetFeeder,
17
+ temperatureHumiditySensor: TemperatureHumiditySensor,
18
+ });
19
+
20
+ declare global {
21
+ namespace Home {
22
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
23
+ interface DeviceConstructors {
24
+ airConditioner: AirConditioner;
25
+ dehumidifier: Dehumidifier;
26
+ fan: Fan;
27
+ light: Light;
28
+ petFeeder: PetFeeder;
29
+ temperatureHumiditySensor: TemperatureHumiditySensor;
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,91 @@
1
+ import {computed} from 'mobx';
2
+
3
+ import {assertDeclaring} from './@lifecycle.js';
4
+ import type {EndpointReference} from './endpoint.js';
5
+
6
+ export abstract class Device {
7
+ constructor(private readonly entry: DeviceEntry) {}
8
+
9
+ get name(): string {
10
+ return this.entry.name;
11
+ }
12
+
13
+ @computed
14
+ get ready(): boolean {
15
+ return this.entry.endpoints.every(endpoint => endpoint.ready);
16
+ }
17
+
18
+ protected getOrCreateEndpoint<TEndpoint extends EndpointReference>(
19
+ Constructor: EndpointConstructor<TEndpoint>,
20
+ name = '',
21
+ ): TEndpoint {
22
+ return this.entry.getOrCreateEndpoint(Constructor, name);
23
+ }
24
+ }
25
+
26
+ export class DeviceEntry {
27
+ private readonly endpointMap = new Map<string, EndpointReference>();
28
+
29
+ private readonly instanceMap = new Map<DeviceConstructor<Device>, Device>();
30
+
31
+ constructor(readonly name: string) {}
32
+
33
+ get endpoints(): MapIterator<EndpointReference> {
34
+ return this.endpointMap.values();
35
+ }
36
+
37
+ get instances(): IterableIterator<Device> {
38
+ return this.instanceMap.values();
39
+ }
40
+
41
+ get constructors(): IterableIterator<DeviceConstructor<Device>> {
42
+ return this.instanceMap.keys();
43
+ }
44
+
45
+ getEndpoint(name = ''): EndpointReference | undefined {
46
+ return this.endpointMap.get(name);
47
+ }
48
+
49
+ getOrCreateEndpoint<TEndpoint extends EndpointReference>(
50
+ Constructor: EndpointConstructor<TEndpoint>,
51
+ name = '',
52
+ ): TEndpoint {
53
+ let endpoint = this.endpointMap.get(name);
54
+
55
+ if (endpoint === undefined) {
56
+ assertDeclaring();
57
+ endpoint = new Constructor(name);
58
+ this.endpointMap.set(name, endpoint);
59
+ } else if (!(endpoint instanceof Constructor)) {
60
+ throw new TypeError(`Incompatible endpoint: ${name}.`);
61
+ }
62
+
63
+ return endpoint as TEndpoint;
64
+ }
65
+
66
+ createInstance<TDevice extends Device>(
67
+ Constructor: DeviceConstructor<TDevice>,
68
+ ): TDevice;
69
+ createInstance(Constructor: DeviceConstructor<Device>): Device {
70
+ assertDeclaring();
71
+
72
+ const existingInstance = this.instanceMap.get(Constructor);
73
+
74
+ if (existingInstance !== undefined) {
75
+ throw new TypeError(`Duplicate device instance: ${this.name}.`);
76
+ }
77
+
78
+ const instance = new Constructor(this);
79
+ this.instanceMap.set(Constructor, instance);
80
+
81
+ return instance;
82
+ }
83
+ }
84
+
85
+ export type DeviceConstructor<TDevice extends Device> = new (
86
+ entry: DeviceEntry,
87
+ ) => TDevice;
88
+
89
+ type EndpointConstructor<TEndpoint extends EndpointReference> = new (
90
+ name?: string,
91
+ ) => TEndpoint;
@@ -0,0 +1,234 @@
1
+ import {computed} from 'mobx';
2
+
3
+ import type {Temperature} from '../atomics/index.js';
4
+ import {StatefulCommand} from '../command.js';
5
+ import type {HumiditySensor, TemperatureSensor} from '../device/index.js';
6
+ import {Device, type DeviceEntry} from '../device.js';
7
+ import {
8
+ Endpoint,
9
+ type EndpointConnection,
10
+ type EndpointLogState,
11
+ } from '../endpoint.js';
12
+
13
+ export type AirConditionerMode = 'auto' | 'cool' | 'dry' | 'fan' | 'heat';
14
+
15
+ export class AirConditioner
16
+ extends Device
17
+ implements TemperatureSensor, HumiditySensor
18
+ {
19
+ protected readonly endpoint: AirConditionerEndpoint;
20
+
21
+ @computed
22
+ get on(): boolean {
23
+ return this.endpoint.on;
24
+ }
25
+
26
+ @computed
27
+ get mode(): AirConditionerMode | undefined {
28
+ return this.endpoint.mode;
29
+ }
30
+
31
+ @computed
32
+ get targetTemperature(): Temperature | undefined {
33
+ return this.endpoint.targetTemperature;
34
+ }
35
+
36
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
37
+ @computed
38
+ get targetRelativeHumidity(): number | undefined {
39
+ return this.endpoint.targetRelativeHumidity;
40
+ }
41
+
42
+ @computed
43
+ get temperature(): Temperature | undefined {
44
+ return this.endpoint.temperature;
45
+ }
46
+
47
+ /** Relative humidity as a normalized ratio from 0 to 1. */
48
+ @computed
49
+ get relativeHumidity(): number | undefined {
50
+ return this.endpoint.relativeHumidity;
51
+ }
52
+
53
+ constructor(entry: DeviceEntry) {
54
+ super(entry);
55
+ this.endpoint = this.getOrCreateEndpoint(AirConditionerEndpoint);
56
+ }
57
+
58
+ turnOn(): this {
59
+ this.endpoint.turnOn();
60
+ return this;
61
+ }
62
+
63
+ turnOff(): this {
64
+ this.endpoint.turnOff();
65
+ return this;
66
+ }
67
+
68
+ setMode(value: AirConditionerMode): this {
69
+ this.endpoint.setMode(value);
70
+ return this;
71
+ }
72
+
73
+ setTargetTemperature(value: Temperature): this {
74
+ this.endpoint.setTargetTemperature(value);
75
+ return this;
76
+ }
77
+
78
+ /** Sets target relative humidity using a normalized ratio from 0 to 1. */
79
+ setTargetHumidity(relativeHumidity: number): this {
80
+ this.endpoint.setTargetHumidity(relativeHumidity);
81
+ return this;
82
+ }
83
+ }
84
+
85
+ export class AirConditionerEndpoint<
86
+ TConnection extends AirConditionerEndpointConnection =
87
+ AirConditionerEndpointConnection,
88
+ >
89
+ extends Endpoint<AirConditionerEndpointCommand, TConnection>
90
+ implements TemperatureSensor, HumiditySensor
91
+ {
92
+ @computed
93
+ get on(): boolean {
94
+ return this.connection?.on ?? false;
95
+ }
96
+
97
+ @computed
98
+ get mode(): AirConditionerMode | undefined {
99
+ return this.connection?.mode;
100
+ }
101
+
102
+ @computed
103
+ get targetTemperature(): Temperature | undefined {
104
+ return this.connection?.targetTemperature;
105
+ }
106
+
107
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
108
+ @computed
109
+ get targetRelativeHumidity(): number | undefined {
110
+ return this.connection?.targetRelativeHumidity;
111
+ }
112
+
113
+ @computed
114
+ get temperature(): Temperature | undefined {
115
+ return this.connection?.temperature;
116
+ }
117
+
118
+ /** Relative humidity as a normalized ratio from 0 to 1. */
119
+ @computed
120
+ get relativeHumidity(): number | undefined {
121
+ return this.connection?.relativeHumidity;
122
+ }
123
+
124
+ protected override get logState(): EndpointLogState {
125
+ if (!this.ready) {
126
+ return {ready: false};
127
+ }
128
+
129
+ return {
130
+ ready: true,
131
+ on: this.on,
132
+ mode: this.mode,
133
+ targetTemperatureCelsius: this.targetTemperature?.celsius,
134
+ targetRelativeHumidity: this.targetRelativeHumidity,
135
+ temperatureCelsius: this.temperature?.celsius,
136
+ relativeHumidity: this.relativeHumidity,
137
+ };
138
+ }
139
+
140
+ turnOn(): this {
141
+ return this.enqueueCommand(new SetAirConditionerOnCommand(true));
142
+ }
143
+
144
+ turnOff(): this {
145
+ return this.enqueueCommand(new SetAirConditionerOnCommand(false));
146
+ }
147
+
148
+ setMode(value: AirConditionerMode): this {
149
+ return this.enqueueCommand(new SetAirConditionerModeCommand(value));
150
+ }
151
+
152
+ setTargetTemperature(value: Temperature): this {
153
+ return this.enqueueCommand(
154
+ new SetAirConditionerTargetTemperatureCommand(value),
155
+ );
156
+ }
157
+
158
+ /** Sets target relative humidity using a normalized ratio from 0 to 1. */
159
+ setTargetHumidity(relativeHumidity: number): this {
160
+ return this.enqueueCommand(
161
+ new SetAirConditionerTargetHumidityCommand(relativeHumidity),
162
+ );
163
+ }
164
+ }
165
+
166
+ export type AirConditionerEndpointConnection =
167
+ EndpointConnection<AirConditionerEndpointCommand> &
168
+ TemperatureSensor &
169
+ HumiditySensor & {
170
+ readonly on: boolean;
171
+ readonly mode: AirConditionerMode | undefined;
172
+ readonly targetTemperature: Temperature | undefined;
173
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
174
+ readonly targetRelativeHumidity: number | undefined;
175
+ };
176
+
177
+ export abstract class AirConditionerCommand extends StatefulCommand {}
178
+
179
+ export class SetAirConditionerOnCommand extends AirConditionerCommand {
180
+ constructor(readonly value: boolean) {
181
+ super();
182
+ }
183
+
184
+ override toLogString(): string {
185
+ return `set on=${this.value}`;
186
+ }
187
+ }
188
+
189
+ export class SetAirConditionerModeCommand extends AirConditionerCommand {
190
+ constructor(readonly value: AirConditionerMode) {
191
+ super();
192
+ }
193
+
194
+ override toLogString(): string {
195
+ return `set mode=${this.value}`;
196
+ }
197
+ }
198
+
199
+ export class SetAirConditionerTargetTemperatureCommand extends AirConditionerCommand {
200
+ constructor(readonly value: Temperature) {
201
+ super();
202
+ }
203
+
204
+ override toLogString(): string {
205
+ return `set targetTemperatureCelsius=${this.value.celsius}`;
206
+ }
207
+ }
208
+
209
+ export class SetAirConditionerTargetHumidityCommand extends AirConditionerCommand {
210
+ /** A normalized relative-humidity ratio from 0 to 1. */
211
+ constructor(readonly relativeHumidity: number) {
212
+ super();
213
+
214
+ if (
215
+ !Number.isFinite(relativeHumidity) ||
216
+ relativeHumidity < 0 ||
217
+ relativeHumidity > 1
218
+ ) {
219
+ throw new RangeError(
220
+ 'Target relative humidity must be a finite number from 0 to 1.',
221
+ );
222
+ }
223
+ }
224
+
225
+ override toLogString(): string {
226
+ return `set targetRelativeHumidity=${this.relativeHumidity}`;
227
+ }
228
+ }
229
+
230
+ export type AirConditionerEndpointCommand =
231
+ | SetAirConditionerOnCommand
232
+ | SetAirConditionerModeCommand
233
+ | SetAirConditionerTargetTemperatureCommand
234
+ | SetAirConditionerTargetHumidityCommand;
@@ -0,0 +1,200 @@
1
+ import {computed} from 'mobx';
2
+
3
+ import type {Temperature} from '../atomics/index.js';
4
+ import {StatefulCommand} from '../command.js';
5
+ import type {HumiditySensor, TemperatureSensor} from '../device/index.js';
6
+ import {Device, type DeviceEntry} from '../device.js';
7
+ import {
8
+ Endpoint,
9
+ type EndpointConnection,
10
+ type EndpointLogState,
11
+ } from '../endpoint.js';
12
+
13
+ export type DehumidifierMode = 'auto' | 'sleep' | 'laundry';
14
+
15
+ export class Dehumidifier
16
+ extends Device
17
+ implements TemperatureSensor, HumiditySensor
18
+ {
19
+ protected readonly endpoint: DehumidifierEndpoint;
20
+
21
+ @computed
22
+ get on(): boolean {
23
+ return this.endpoint.on;
24
+ }
25
+
26
+ @computed
27
+ get mode(): DehumidifierMode | undefined {
28
+ return this.endpoint.mode;
29
+ }
30
+
31
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
32
+ @computed
33
+ get targetRelativeHumidity(): number | undefined {
34
+ return this.endpoint.targetRelativeHumidity;
35
+ }
36
+
37
+ @computed
38
+ get temperature(): Temperature | undefined {
39
+ return this.endpoint.temperature;
40
+ }
41
+
42
+ /** Relative humidity as a normalized ratio from 0 to 1. */
43
+ @computed
44
+ get relativeHumidity(): number | undefined {
45
+ return this.endpoint.relativeHumidity;
46
+ }
47
+
48
+ constructor(entry: DeviceEntry) {
49
+ super(entry);
50
+ this.endpoint = this.getOrCreateEndpoint(DehumidifierEndpoint);
51
+ }
52
+
53
+ turnOn(): this {
54
+ this.endpoint.turnOn();
55
+ return this;
56
+ }
57
+
58
+ turnOff(): this {
59
+ this.endpoint.turnOff();
60
+ return this;
61
+ }
62
+
63
+ setMode(value: DehumidifierMode): this {
64
+ this.endpoint.setMode(value);
65
+ return this;
66
+ }
67
+
68
+ /** Sets target relative humidity using a normalized ratio from 0 to 1. */
69
+ setTargetHumidity(relativeHumidity: number): this {
70
+ this.endpoint.setTargetHumidity(relativeHumidity);
71
+ return this;
72
+ }
73
+ }
74
+
75
+ export class DehumidifierEndpoint<
76
+ TConnection extends DehumidifierEndpointConnection =
77
+ DehumidifierEndpointConnection,
78
+ >
79
+ extends Endpoint<DehumidifierEndpointCommand, TConnection>
80
+ implements TemperatureSensor, HumiditySensor
81
+ {
82
+ @computed
83
+ get on(): boolean {
84
+ return this.connection?.on ?? false;
85
+ }
86
+
87
+ @computed
88
+ get mode(): DehumidifierMode | undefined {
89
+ return this.connection?.mode;
90
+ }
91
+
92
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
93
+ @computed
94
+ get targetRelativeHumidity(): number | undefined {
95
+ return this.connection?.targetRelativeHumidity;
96
+ }
97
+
98
+ @computed
99
+ get temperature(): Temperature | undefined {
100
+ return this.connection?.temperature;
101
+ }
102
+
103
+ /** Relative humidity as a normalized ratio from 0 to 1. */
104
+ @computed
105
+ get relativeHumidity(): number | undefined {
106
+ return this.connection?.relativeHumidity;
107
+ }
108
+
109
+ protected override get logState(): EndpointLogState {
110
+ if (!this.ready) {
111
+ return {ready: false};
112
+ }
113
+
114
+ return {
115
+ ready: true,
116
+ on: this.on,
117
+ mode: this.mode,
118
+ targetRelativeHumidity: this.targetRelativeHumidity,
119
+ temperatureCelsius: this.temperature?.celsius,
120
+ relativeHumidity: this.relativeHumidity,
121
+ };
122
+ }
123
+
124
+ turnOn(): this {
125
+ return this.enqueueCommand(new SetDehumidifierOnCommand(true));
126
+ }
127
+
128
+ turnOff(): this {
129
+ return this.enqueueCommand(new SetDehumidifierOnCommand(false));
130
+ }
131
+
132
+ setMode(value: DehumidifierMode): this {
133
+ return this.enqueueCommand(new SetDehumidifierModeCommand(value));
134
+ }
135
+
136
+ /** Sets target relative humidity using a normalized ratio from 0 to 1. */
137
+ setTargetHumidity(relativeHumidity: number): this {
138
+ return this.enqueueCommand(
139
+ new SetDehumidifierTargetHumidityCommand(relativeHumidity),
140
+ );
141
+ }
142
+ }
143
+
144
+ export type DehumidifierEndpointConnection =
145
+ EndpointConnection<DehumidifierEndpointCommand> &
146
+ TemperatureSensor &
147
+ HumiditySensor & {
148
+ readonly on: boolean;
149
+ readonly mode: DehumidifierMode | undefined;
150
+ /** Target relative humidity as a normalized ratio from 0 to 1. */
151
+ readonly targetRelativeHumidity: number | undefined;
152
+ };
153
+
154
+ export abstract class DehumidifierCommand extends StatefulCommand {}
155
+
156
+ export class SetDehumidifierOnCommand extends DehumidifierCommand {
157
+ constructor(readonly value: boolean) {
158
+ super();
159
+ }
160
+
161
+ override toLogString(): string {
162
+ return `set on=${this.value}`;
163
+ }
164
+ }
165
+
166
+ export class SetDehumidifierModeCommand extends DehumidifierCommand {
167
+ constructor(readonly value: DehumidifierMode) {
168
+ super();
169
+ }
170
+
171
+ override toLogString(): string {
172
+ return `set mode=${this.value}`;
173
+ }
174
+ }
175
+
176
+ export class SetDehumidifierTargetHumidityCommand extends DehumidifierCommand {
177
+ /** A normalized relative-humidity ratio from 0 to 1. */
178
+ constructor(readonly relativeHumidity: number) {
179
+ super();
180
+
181
+ if (
182
+ !Number.isFinite(relativeHumidity) ||
183
+ relativeHumidity < 0 ||
184
+ relativeHumidity > 1
185
+ ) {
186
+ throw new RangeError(
187
+ 'Target relative humidity must be a finite number from 0 to 1.',
188
+ );
189
+ }
190
+ }
191
+
192
+ override toLogString(): string {
193
+ return `set targetRelativeHumidity=${this.relativeHumidity}`;
194
+ }
195
+ }
196
+
197
+ export type DehumidifierEndpointCommand =
198
+ | SetDehumidifierOnCommand
199
+ | SetDehumidifierModeCommand
200
+ | SetDehumidifierTargetHumidityCommand;