|
1 | 1 | import classNames from 'classnames'
|
2 |
| -import { useState } from 'react' |
3 |
| -import type { ThermostatDevice } from 'seamapi' |
| 2 | +import { useEffect, useState } from 'react' |
| 3 | +import type { HvacModeSetting, ThermostatDevice } from 'seamapi' |
4 | 4 |
|
| 5 | +import { debounce } from 'lib/debounce.js' |
5 | 6 | import { BeeIcon } from 'lib/icons/Bee.js'
|
| 7 | +import { CheckBlackIcon } from 'lib/icons/CheckBlack.js' |
6 | 8 | import { ChevronWideIcon } from 'lib/icons/ChevronWide.js'
|
7 | 9 | import { NestedClimateSettingScheduleTable } from 'lib/seam/components/ClimateSettingScheduleTable/ClimateSettingScheduleTable.js'
|
8 | 10 | import type { CommonProps } from 'lib/seam/components/common-props.js'
|
9 | 11 | import { useConnectedAccount } from 'lib/seam/connected-accounts/use-connected-account.js'
|
10 | 12 | import { useClimateSettingSchedules } from 'lib/seam/thermostats/climate-setting-schedules/use-climate-setting-schedules.js'
|
| 13 | +import { useCoolThermostat } from 'lib/seam/thermostats/use-cool-thermostat.js' |
| 14 | +import { useHeatCoolThermostat } from 'lib/seam/thermostats/use-heat-cool-thermostat.js' |
| 15 | +import { useHeatThermostat } from 'lib/seam/thermostats/use-heat-thermostat.js' |
| 16 | +import { useSetThermostatOff } from 'lib/seam/thermostats/use-set-thermostat-off.js' |
11 | 17 | import { useUpdateFanMode } from 'lib/seam/thermostats/use-update-fan-mode.js'
|
12 | 18 | import { useUpdateThermostat } from 'lib/seam/thermostats/use-update-thermostat.js'
|
| 19 | +import { getSupportedThermostatModes } from 'lib/temperature-bounds.js' |
| 20 | +import { AccordionRow } from 'lib/ui/layout/AccordionRow.js' |
13 | 21 | import { ContentHeader } from 'lib/ui/layout/ContentHeader.js'
|
14 | 22 | import { DetailRow } from 'lib/ui/layout/DetailRow.js'
|
15 | 23 | import { DetailSection } from 'lib/ui/layout/DetailSection.js'
|
16 | 24 | import { DetailSectionGroup } from 'lib/ui/layout/DetailSectionGroup.js'
|
17 | 25 | import { Snackbar } from 'lib/ui/Snackbar/Snackbar.js'
|
18 | 26 | import { Switch } from 'lib/ui/Switch/Switch.js'
|
| 27 | +import { ClimateModeMenu } from 'lib/ui/thermostat/ClimateModeMenu.js' |
19 | 28 | import { ClimateSettingStatus } from 'lib/ui/thermostat/ClimateSettingStatus.js'
|
20 | 29 | import { FanModeMenu } from 'lib/ui/thermostat/FanModeMenu.js'
|
| 30 | +import { TemperatureControlGroup } from 'lib/ui/thermostat/TemperatureControlGroup.js' |
21 | 31 | import { ThermostatCard } from 'lib/ui/thermostat/ThermostatCard.js'
|
22 | 32 |
|
23 | 33 | interface ThermostatDeviceDetailsProps extends CommonProps {
|
@@ -105,12 +115,7 @@ export function ThermostatDeviceDetails({
|
105 | 115 | label={t.currentSettings}
|
106 | 116 | tooltipContent={t.currentSettingsTooltip}
|
107 | 117 | >
|
108 |
| - <DetailRow label={t.climate}> |
109 |
| - <ClimateSettingStatus |
110 |
| - climateSetting={device.properties.current_climate_setting} |
111 |
| - temperatureUnit='fahrenheit' |
112 |
| - /> |
113 |
| - </DetailRow> |
| 118 | + <ClimateSettingRow device={device} /> |
114 | 119 | <FanModeRow device={device} />
|
115 | 120 | </DetailSection>
|
116 | 121 |
|
@@ -239,6 +244,191 @@ function FanModeRow({ device }: { device: ThermostatDevice }): JSX.Element {
|
239 | 244 | )
|
240 | 245 | }
|
241 | 246 |
|
| 247 | +function ClimateSettingRow({ |
| 248 | + device, |
| 249 | +}: { |
| 250 | + device: ThermostatDevice |
| 251 | +}): JSX.Element { |
| 252 | + const deviceHeatValue = |
| 253 | + device.properties.current_climate_setting.heating_set_point_fahrenheit |
| 254 | + const deviceCoolValue = |
| 255 | + device.properties.current_climate_setting.cooling_set_point_fahrenheit |
| 256 | + |
| 257 | + const supportedModes = getSupportedThermostatModes(device) |
| 258 | + |
| 259 | + const [showSuccess, setShowSuccess] = useState(false) |
| 260 | + const [mode, setMode] = useState<HvacModeSetting>( |
| 261 | + (supportedModes.includes('heat_cool') ? 'heat_cool' : supportedModes[0]) ?? |
| 262 | + 'off' |
| 263 | + ) |
| 264 | + |
| 265 | + const [heatValue, setHeatValue] = useState( |
| 266 | + device.properties.current_climate_setting.heating_set_point_fahrenheit ?? 0 |
| 267 | + ) |
| 268 | + |
| 269 | + const [coolValue, setCoolValue] = useState( |
| 270 | + device.properties.current_climate_setting.cooling_set_point_fahrenheit ?? 0 |
| 271 | + ) |
| 272 | + |
| 273 | + const { |
| 274 | + mutate: heatCoolThermostat, |
| 275 | + isSuccess: isHeatCoolSuccess, |
| 276 | + isError: isHeatCoolError, |
| 277 | + } = useHeatCoolThermostat() |
| 278 | + |
| 279 | + const { |
| 280 | + mutate: heatThermostat, |
| 281 | + isSuccess: isHeatSuccess, |
| 282 | + isError: isHeatError, |
| 283 | + } = useHeatThermostat() |
| 284 | + |
| 285 | + const { |
| 286 | + mutate: coolThermostat, |
| 287 | + isSuccess: isCoolSuccess, |
| 288 | + isError: isCoolError, |
| 289 | + } = useCoolThermostat() |
| 290 | + |
| 291 | + const { |
| 292 | + mutate: setThermostatOff, |
| 293 | + isSuccess: isSetOffSuccess, |
| 294 | + isError: isSetOffError, |
| 295 | + } = useSetThermostatOff() |
| 296 | + |
| 297 | + useEffect(() => { |
| 298 | + const handler = debounce(() => { |
| 299 | + switch (mode) { |
| 300 | + case 'heat_cool': |
| 301 | + heatCoolThermostat({ |
| 302 | + device_id: device.device_id, |
| 303 | + heating_set_point_fahrenheit: heatValue, |
| 304 | + cooling_set_point_fahrenheit: coolValue, |
| 305 | + }) |
| 306 | + break |
| 307 | + case 'heat': |
| 308 | + heatThermostat({ |
| 309 | + device_id: device.device_id, |
| 310 | + heating_set_point_fahrenheit: heatValue, |
| 311 | + }) |
| 312 | + break |
| 313 | + case 'cool': |
| 314 | + coolThermostat({ |
| 315 | + device_id: device.device_id, |
| 316 | + cooling_set_point_fahrenheit: coolValue, |
| 317 | + }) |
| 318 | + break |
| 319 | + case 'off': |
| 320 | + setThermostatOff({ |
| 321 | + device_id: device.device_id, |
| 322 | + }) |
| 323 | + break |
| 324 | + } |
| 325 | + }, 2000) |
| 326 | + |
| 327 | + if ( |
| 328 | + heatValue !== deviceHeatValue || |
| 329 | + coolValue !== deviceCoolValue || |
| 330 | + mode === 'off' |
| 331 | + ) { |
| 332 | + handler() |
| 333 | + } |
| 334 | + |
| 335 | + return () => { |
| 336 | + handler.cancel() |
| 337 | + } |
| 338 | + }, [ |
| 339 | + heatValue, |
| 340 | + coolValue, |
| 341 | + mode, |
| 342 | + deviceHeatValue, |
| 343 | + deviceCoolValue, |
| 344 | + device, |
| 345 | + heatThermostat, |
| 346 | + coolThermostat, |
| 347 | + heatCoolThermostat, |
| 348 | + setThermostatOff, |
| 349 | + ]) |
| 350 | + |
| 351 | + useEffect(() => { |
| 352 | + if ( |
| 353 | + isHeatCoolSuccess || |
| 354 | + isHeatSuccess || |
| 355 | + isCoolSuccess || |
| 356 | + isSetOffSuccess |
| 357 | + ) { |
| 358 | + setShowSuccess(true) |
| 359 | + |
| 360 | + const timeout = globalThis.setTimeout(() => { |
| 361 | + setShowSuccess(false) |
| 362 | + }, 3000) |
| 363 | + |
| 364 | + return () => { |
| 365 | + globalThis.clearTimeout(timeout) |
| 366 | + } |
| 367 | + } |
| 368 | + |
| 369 | + return () => {} |
| 370 | + }, [isHeatCoolSuccess, isHeatSuccess, isCoolSuccess, isSetOffSuccess]) |
| 371 | + |
| 372 | + return ( |
| 373 | + <> |
| 374 | + <AccordionRow |
| 375 | + label={t.climate} |
| 376 | + leftContent={ |
| 377 | + <div |
| 378 | + className={classNames('seam-thermostat-mutation-status', { |
| 379 | + 'is-visible': showSuccess, |
| 380 | + })} |
| 381 | + > |
| 382 | + <div className='seam-thermostat-mutation-status-icon'> |
| 383 | + <CheckBlackIcon /> |
| 384 | + </div> |
| 385 | + <div className='seam-thermostat-mutation-status-label'> |
| 386 | + {t.saved} |
| 387 | + </div> |
| 388 | + </div> |
| 389 | + } |
| 390 | + rightCollapsedContent={ |
| 391 | + <ClimateSettingStatus |
| 392 | + climateSetting={device.properties.current_climate_setting} |
| 393 | + temperatureUnit='fahrenheit' |
| 394 | + /> |
| 395 | + } |
| 396 | + > |
| 397 | + <div className='seam-detail-row-end-alignment'> |
| 398 | + {mode !== 'off' && ( |
| 399 | + <TemperatureControlGroup |
| 400 | + mode={mode} |
| 401 | + heatValue={heatValue} |
| 402 | + coolValue={coolValue} |
| 403 | + onHeatValueChange={setHeatValue} |
| 404 | + onCoolValueChange={setCoolValue} |
| 405 | + delta={ |
| 406 | + Number( |
| 407 | + 'min_heating_cooling_delta_fahrenheit' in device.properties && |
| 408 | + device.properties.min_heating_cooling_delta_fahrenheit |
| 409 | + ) ?? 0 |
| 410 | + } |
| 411 | + /> |
| 412 | + )} |
| 413 | + |
| 414 | + <ClimateModeMenu |
| 415 | + mode={mode} |
| 416 | + onChange={setMode} |
| 417 | + supportedModes={supportedModes} |
| 418 | + /> |
| 419 | + </div> |
| 420 | + </AccordionRow> |
| 421 | + |
| 422 | + <Snackbar |
| 423 | + message={t.climateSettingError} |
| 424 | + variant='error' |
| 425 | + visible={isHeatCoolError || isHeatError || isCoolError || isSetOffError} |
| 426 | + automaticVisibility |
| 427 | + /> |
| 428 | + </> |
| 429 | + ) |
| 430 | +} |
| 431 | + |
242 | 432 | const t = {
|
243 | 433 | thermostat: 'Thermostat',
|
244 | 434 | climateSchedule: 'scheduled climate',
|
@@ -266,4 +456,6 @@ const t = {
|
266 | 456 | fanModeError: 'Error updating fan mode. Please try again.',
|
267 | 457 | manualOverrideSuccess: 'Successfully updated manual override!',
|
268 | 458 | manualOverrideError: 'Error updating manual override. Please try again.',
|
| 459 | + climateSettingError: 'Error updating climate setting. Please try again.', |
| 460 | + saved: 'Saved', |
269 | 461 | }
|
0 commit comments