import { clamp } from "alcalzone-shared/math" ; import { entries } from "alcalzone-shared/objects" ; export type SetbackSpecialState = "Frost Protection" | "Energy Saving" | "Unused" ; export const setbackSpecialStateValues = { "Frost Protection" : 0x79 , "Energy Saving" : 0x7a , Unused : 0x7f } ; export type SetbackState = number | SetbackSpecialState ; /** Encodes a setback state to use in a ThermostatSetbackCC */ export function encodeSetbackState ( state ) { if ( typeof state === "string" ) return setbackSpecialStateValues [ state ] ; state = clamp ( state , - 12.8 , 12 ) ; return Math . round ( state * 10 ) ; } /** Decodes a setback state used in a ThermostatSetbackCC */ export function decodeSetbackState ( val ) { if ( val > 120 ) { const foundEntry = entries ( setbackSpecialStateValues ) . find ( ( [ , v ] ) => val === v ) ; if ( ! foundEntry ) return ; return foundEntry [ 0 ] as SetbackSpecialState ; } else { return val / 10 ; } }