diff --git a/src/facades/device.ts b/src/facades/device.ts index 81a6c44d..a91a9d11 100644 --- a/src/facades/device.ts +++ b/src/facades/device.ts @@ -11,6 +11,7 @@ import { type UpdateDeviceData, type Values, flags, + keys, } from '../types' import { YEAR_1970, nowISO } from './utils' import type API from '../services' @@ -19,8 +20,8 @@ import type { IDeviceFacade } from './interfaces' // @ts-expect-error: most runtimes do not support it natively Symbol.metadata ??= Symbol('Symbol.metadata') +const keySymbol = Symbol('value') const setDataSymbol = Symbol('setData') -const valueSymbol = Symbol('value') export const mapTo = < @@ -35,13 +36,12 @@ export const mapTo = context: ClassAccessorDecoratorContext, ): ClassAccessorDecoratorResult => ({ get(this: This): unknown { - const value = String(context.name) + const key = String(context.name) + context.metadata[keySymbol] ??= {} + ;(context.metadata[keySymbol] as Record)[key] = setData context.metadata[setDataSymbol] ??= {} ;(context.metadata[setDataSymbol] as Record)[setData] = - value - context.metadata[valueSymbol] ??= {} - ;(context.metadata[valueSymbol] as Record)[value] = - setData + key return setData in this.data ? this.data[setData as keyof typeof this.data] : null @@ -65,16 +65,8 @@ export default abstract class readonly #flags: Record, number> - readonly #setDataMapping = this.constructor[Symbol.metadata]?.[ - setDataSymbol - ] as Record, keyof Values[T]> - readonly #type: T - readonly #valueMapping = this.constructor[Symbol.metadata]?.[ - valueSymbol - ] as Record> - public constructor(api: API, model: DeviceModel) { super(api, model as DeviceModelAny) this.#type = model.type @@ -82,6 +74,9 @@ export default abstract class NonFlagsKeyOf, number > + keys[this.#type] + .filter((key) => key in this) + .forEach((key) => this[key as keyof typeof this]) } @mapTo('Power') @@ -93,14 +88,14 @@ export default abstract class public get values(): Values[T] { return Object.fromEntries( - Object.entries( - this.constructor[Symbol.metadata]?.[valueSymbol] as Record< + Object.keys( + this.constructor[Symbol.metadata]?.[keySymbol] as Record< string, string >, ) - .filter(([value]) => value in this) - .map(([key, value]) => [value, this[key as keyof typeof this]]), + .filter((key) => key in this) + .map((key) => [key, this[key as keyof typeof this]]), ) } @@ -110,6 +105,26 @@ export default abstract class ) as Omit } + get #setDataMapping(): Record< + NonFlagsKeyOf, + keyof Values[T] + > { + return this.constructor[Symbol.metadata]?.[setDataSymbol] as Record< + NonFlagsKeyOf, + keyof Values[T] + > + } + + get #valueMapping(): Record< + keyof Values[T], + NonFlagsKeyOf + > { + return this.constructor[Symbol.metadata]?.[keySymbol] as Record< + keyof Values[T], + NonFlagsKeyOf + > + } + public async fetch(): Promise { await this.api.fetch() return this.data diff --git a/src/facades/device_ata.ts b/src/facades/device_ata.ts index 714d1f33..4479314b 100644 --- a/src/facades/device_ata.ts +++ b/src/facades/device_ata.ts @@ -11,7 +11,7 @@ export default class extends BaseDeviceFacade<'Ata'> { @mapTo('FanSpeed') public accessor fan: unknown = FanSpeed.auto - @mapTo('VaneHorizontal') + @mapTo('VaneHorizontalDirection') public accessor horizontal: unknown = Horizontal.auto @mapTo('OperationMode') @@ -20,6 +20,6 @@ export default class extends BaseDeviceFacade<'Ata'> { @mapTo('SetTemperature') public accessor temperature: unknown = NUMBER_0 - @mapTo('VaneVertical') + @mapTo('VaneVerticalDirection') public accessor vertical: unknown = Vertical.auto } diff --git a/src/index.ts b/src/index.ts index 9b478697..f392a532 100644 --- a/src/index.ts +++ b/src/index.ts @@ -134,4 +134,8 @@ export { flagsAta, flagsAtw, flagsErv, + keys, + keysAta, + keysAtw, + keysErv, } from './types' diff --git a/src/types/ata.ts b/src/types/ata.ts index a59c609a..e1fbc0cd 100644 --- a/src/types/ata.ts +++ b/src/types/ata.ts @@ -118,3 +118,12 @@ export interface ValuesAta extends BaseValues { readonly temperature?: number readonly vertical?: Vertical } + +export const keysAta = [ + 'fan', + 'horizontal', + 'mode', + 'power', + 'temperature', + 'vertical', +] as const satisfies (keyof ValuesAta)[] diff --git a/src/types/atw.ts b/src/types/atw.ts index f9267b82..4916fece 100644 --- a/src/types/atw.ts +++ b/src/types/atw.ts @@ -131,3 +131,17 @@ export interface ValuesAtw extends BaseValues { readonly temperature?: number readonly temperatureZone2?: number } + +export const keysAtw = [ + 'coolFlowTemperature', + 'coolFlowTemperatureZone2', + 'forcedHotWater', + 'heatFlowTemperature', + 'heatFlowTemperatureZone2', + 'hotWaterTemperature', + 'mode', + 'modeZone2', + 'power', + 'temperature', + 'temperatureZone2', +] as const satisfies (keyof ValuesAtw)[] diff --git a/src/types/common.ts b/src/types/common.ts index 0c12fd83..78118e57 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -11,6 +11,7 @@ import { type ValuesAta, type Vertical, flagsAta, + keysAta, } from './ata' import { type EnergyDataAtw, @@ -21,6 +22,7 @@ import { type UpdateDeviceDataAtw, type ValuesAtw, flagsAtw, + keysAtw, } from './atw' import { type GetDeviceDataErv, @@ -30,6 +32,7 @@ import { type UpdateDeviceDataErv, type ValuesErv, flagsErv, + keysErv, } from './erv' export enum Language { @@ -334,3 +337,9 @@ export interface Values { readonly Atw: ValuesAtw readonly Erv: ValuesErv } + +export const keys = { + Ata: keysAta, + Atw: keysAtw, + Erv: keysErv, +} as const diff --git a/src/types/erv.ts b/src/types/erv.ts index b56c0635..f3681b42 100644 --- a/src/types/erv.ts +++ b/src/types/erv.ts @@ -56,3 +56,9 @@ export interface ValuesErv extends BaseValues { readonly fan?: Exclude readonly mode?: VentilationMode } + +export const keysErv = [ + 'fan', + 'mode', + 'power', +] as const satisfies (keyof ValuesErv)[] diff --git a/src/types/index.ts b/src/types/index.ts index 1ece7525..fd8d1d4e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -27,6 +27,7 @@ export { OperationMode, Vertical, flagsAta, + keysAta, } from './ata' export { type AreaData, @@ -72,6 +73,7 @@ export { type WifiPostData, Language, flags, + keys, } from './common' export { type EnergyDataAtw, @@ -84,6 +86,7 @@ export { OperationModeState, OperationModeZone, flagsAtw, + keysAtw, } from './atw' export { type GetDeviceDataErv, @@ -94,4 +97,5 @@ export { type ValuesErv, VentilationMode, flagsErv, + keysErv, } from './erv'