-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { ClimateSetting } from "lib/zod/climate_setting.ts" | ||
Check failure on line 1 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
|
||
export const normalizeClimateSettingMode = (cs: Partial<ClimateSetting>) => { | ||
Check failure on line 3 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 3 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
return { | ||
automatic_heating_enabled: | ||
cs.automatic_heating_enabled ?? | ||
deriveAutomaticHeatingEnabledFromHvacModeSetting(cs.hvac_mode_setting), | ||
automatic_cooling_enabled: | ||
cs.automatic_cooling_enabled ?? | ||
deriveAutomaticCoolingEnabledFromHvacModeSetting(cs.hvac_mode_setting), | ||
hvac_mode_setting: | ||
cs.hvac_mode_setting ?? | ||
deriveHvacModeSettingFromFlags({ | ||
automatic_cooling_enabled: cs.automatic_cooling_enabled!, | ||
Check failure on line 14 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 14 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
automatic_heating_enabled: cs.automatic_heating_enabled!, | ||
Check failure on line 15 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 15 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
}), | ||
} | ||
} | ||
|
||
/** | ||
* @param {ClimateSetting} cs | ||
* @returns {ClimateSetting} | ||
* @description | ||
* This function takes a Partial<ClimateSetting> and returns a new ClimateSetting with all fields filled in | ||
* | ||
*/ | ||
export const normalizeClimateSetting = ( | ||
cs: Partial<ClimateSetting> | ||
): ClimateSetting => { | ||
if (cs.manual_override_allowed === undefined) | ||
throw new Error("manual_override_allowed is required") | ||
|
||
let normalized_climate_setting: ClimateSetting = { | ||
...normalizeClimateSettingMode(cs), | ||
manual_override_allowed: cs.manual_override_allowed, | ||
} | ||
|
||
// if the climate setting calls for cooling set points, we set them | ||
if (normalized_climate_setting.automatic_cooling_enabled) { | ||
normalized_climate_setting = { | ||
...normalized_climate_setting, | ||
...normalizeCoolingSetPoints(cs), | ||
} | ||
} | ||
|
||
// likewise for heating | ||
if (normalized_climate_setting.automatic_heating_enabled) { | ||
normalized_climate_setting = { | ||
...normalized_climate_setting, | ||
...normalizeHeatingSetPoints(cs), | ||
} | ||
} | ||
|
||
return normalized_climate_setting | ||
} | ||
|
||
export const deriveAutomaticHeatingEnabledFromHvacModeSetting = ( | ||
Check failure on line 57 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 57 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
hvac_mode_setting?: ClimateSetting["hvac_mode_setting"] | ||
) => hvac_mode_setting === "heat" || hvac_mode_setting === "heatcool" | ||
|
||
export const deriveAutomaticCoolingEnabledFromHvacModeSetting = ( | ||
Check failure on line 61 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 61 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
hvac_mode_setting?: ClimateSetting["hvac_mode_setting"] | ||
) => hvac_mode_setting === "cool" || hvac_mode_setting === "heatcool" | ||
|
||
export const deriveHvacModeSettingFromFlags = ({ | ||
automatic_cooling_enabled, | ||
automatic_heating_enabled, | ||
}: { | ||
automatic_cooling_enabled: boolean | ||
automatic_heating_enabled: boolean | ||
}): ClimateSetting["hvac_mode_setting"] => { | ||
if (automatic_cooling_enabled && automatic_heating_enabled) return "heatcool" | ||
if (automatic_cooling_enabled) return "cool" | ||
if (automatic_heating_enabled) return "heat" | ||
return "off" | ||
} | ||
|
||
/** | ||
* | ||
* @param cs | ||
* @returns {Partial<ClimateSetting>} | ||
* This will look at what cooling set point is on the climate setting, and set the other temperature scale if neccessary. | ||
* For example, if a request comes in with a cooling_set_point_ceslius, we will set the cooling_set_point_fahrenheit as well. | ||
* | ||
*/ | ||
const normalizeCoolingSetPoints = (cs: Partial<ClimateSetting>) => { | ||
Check failure on line 86 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 86 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
const setPoints: Partial<ClimateSetting> = {} | ||
|
||
if (cs.cooling_set_point_celsius !== undefined) { | ||
setPoints.cooling_set_point_celsius = cs.cooling_set_point_celsius | ||
} else if (cs.cooling_set_point_fahrenheit !== undefined) { | ||
setPoints.cooling_set_point_celsius = convertToCelsius( | ||
cs.cooling_set_point_fahrenheit | ||
) | ||
} | ||
|
||
if (cs.cooling_set_point_fahrenheit !== undefined) { | ||
setPoints.cooling_set_point_fahrenheit = cs.cooling_set_point_fahrenheit | ||
} else if (cs.cooling_set_point_celsius !== undefined) { | ||
setPoints.cooling_set_point_fahrenheit = convertToFahrenheit( | ||
cs.cooling_set_point_celsius | ||
) | ||
} | ||
return setPoints | ||
} | ||
|
||
/** | ||
* | ||
* @param cs | ||
* @returns {Partial<ClimateSetting>} | ||
* This will look at what heating set point is on the climate setting, and set the other temperature scale. | ||
* For example, if a request comes in with a heating_set_point_ceslius, we will set the heating_set_point_fahrenheit as well. | ||
* | ||
*/ | ||
const normalizeHeatingSetPoints = (cs: Partial<ClimateSetting>) => { | ||
Check failure on line 115 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 115 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
const setPoints: Partial<ClimateSetting> = {} | ||
|
||
if (cs.heating_set_point_celsius !== undefined) { | ||
setPoints.heating_set_point_celsius = cs.heating_set_point_celsius | ||
} else if (cs.heating_set_point_fahrenheit !== undefined) { | ||
setPoints.heating_set_point_celsius = convertToCelsius( | ||
cs.heating_set_point_fahrenheit | ||
) | ||
} | ||
|
||
if (cs.heating_set_point_fahrenheit !== undefined) { | ||
setPoints.heating_set_point_fahrenheit = cs.heating_set_point_fahrenheit | ||
} else if (cs.heating_set_point_celsius !== undefined) { | ||
setPoints.heating_set_point_fahrenheit = convertToFahrenheit( | ||
cs.heating_set_point_celsius | ||
) | ||
} | ||
|
||
return setPoints | ||
} | ||
|
||
export const convertToFahrenheit = (celsius: number) => (celsius * 9) / 5 + 32 | ||
Check failure on line 137 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 137 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
|
||
export const convertToCelsius = (fahrenheit: number) => | ||
Check failure on line 139 in src/lib/util/thermostats.ts GitHub Actions / Format code
Check failure on line 139 in src/lib/util/thermostats.ts GitHub Actions / Lint (Node.js v16)
|
||
(fahrenheit - 32) * (5 / 9) |