-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add Thermostat Climate Preset Management #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f82323a
feat: Add Thermostat Climate Preset Management
kadiryazici 4f8d0d9
ci: Format code
seambot 9fc083d
Add endpoint support
kadiryazici 427e13d
ci: Format code
seambot c63fa44
Update order
kadiryazici 3329471
ci: Format code
seambot b0e87a2
Refactor climate preset form
kadiryazici 2fa9724
ci: Format code
seambot 2b87d66
fix button state
kadiryazici 967df3d
ci: Format code
seambot 9a02c90
Add are you sure popover?
kadiryazici 99ab23c
Update according to reviews
kadiryazici d71ab84
ci: Format code
seambot bec1d6d
Fix validation
kadiryazici 574a362
add field
kadiryazici File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/lib/seam/thermostats/use-create-thermostat-climate-preset.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import type { | ||
| SeamHttpApiError, | ||
| ThermostatsCreateClimatePresetBody, | ||
| } from '@seamapi/http/connect' | ||
| import { | ||
| useMutation, | ||
| type UseMutationResult, | ||
| useQueryClient, | ||
| } from '@tanstack/react-query' | ||
|
|
||
| import type { ThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js' | ||
| import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js' | ||
|
|
||
| export type UseCreateThermostatClimatePresetParams = never | ||
| export type UseCreateThermostatClimatePresetData = undefined | ||
|
|
||
| export type UseCreateThermostatClimatePresetVariables = | ||
| ThermostatsCreateClimatePresetBody | ||
|
|
||
| const fhToCelsius = (t?: number): number | undefined => | ||
| t == null ? undefined : (t - 32) * (5 / 9) | ||
|
|
||
| type ClimatePreset = | ||
| ThermostatDevice['properties']['available_climate_presets'][number] | ||
|
|
||
| export function useCreateThermostatClimatePreset(): UseMutationResult< | ||
| UseCreateThermostatClimatePresetData, | ||
| SeamHttpApiError, | ||
| UseCreateThermostatClimatePresetVariables | ||
| > { | ||
| const { client } = useSeamClient() | ||
| const queryClient = useQueryClient() | ||
|
|
||
| return useMutation< | ||
| UseCreateThermostatClimatePresetData, | ||
| SeamHttpApiError, | ||
| UseCreateThermostatClimatePresetVariables | ||
| >({ | ||
| mutationFn: async (variables) => { | ||
| if (client === null) throw new NullSeamClientError() | ||
| await client.thermostats.createClimatePreset(variables) | ||
| }, | ||
| onSuccess: (_data, variables) => { | ||
| const preset: ClimatePreset = { | ||
| ...variables, | ||
| cooling_set_point_celsius: fhToCelsius( | ||
| variables.cooling_set_point_fahrenheit | ||
| ), | ||
| heating_set_point_celsius: fhToCelsius( | ||
| variables.heating_set_point_fahrenheit | ||
| ), | ||
| display_name: variables.name ?? variables.climate_preset_key, | ||
| can_delete: true, | ||
| can_edit: true, | ||
| manual_override_allowed: true, | ||
kadiryazici marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| queryClient.setQueryData<ThermostatDevice | null>( | ||
| ['devices', 'get', { device_id: variables.device_id }], | ||
| (device) => { | ||
| if (device == null) { | ||
| return | ||
| } | ||
|
|
||
| return getUpdatedDevice(device, preset) | ||
| } | ||
| ) | ||
|
|
||
| queryClient.setQueryData<ThermostatDevice[]>( | ||
| ['devices', 'list', { device_id: variables.device_id }], | ||
| (devices): ThermostatDevice[] => { | ||
| if (devices == null) { | ||
| return [] | ||
| } | ||
|
|
||
| return devices.map((device) => { | ||
| if (device.device_id === variables.device_id) { | ||
| return getUpdatedDevice(device, preset) | ||
| } | ||
|
|
||
| return device | ||
| }) | ||
| } | ||
| ) | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function getUpdatedDevice( | ||
| device: ThermostatDevice, | ||
| preset: ClimatePreset | ||
| ): ThermostatDevice { | ||
kadiryazici marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (device == null) { | ||
| return device | ||
| } | ||
|
|
||
| return { | ||
| ...device, | ||
| properties: { | ||
| ...device.properties, | ||
| available_climate_presets: [ | ||
| preset, | ||
| ...(device.properties.available_climate_presets ?? []), | ||
| ], | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.