Skip to content

Commit

Permalink
Support updated routines in Rest Gen 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Feb 4, 2024
1 parent c4f2596 commit 512fdfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-cats-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"homebridge-hatch-baby-rest": minor
---

Add support for Rest Gen 2 with updated routines
23 changes: 14 additions & 9 deletions packages/homebridge-hatch-baby-rest/rest-iot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
IotDeviceInfo,
Product,
RestIotFavorite,
RestIotRoutine,
RestIotState,
} from '../shared/hatch-sleep-types'
import { distinctUntilChanged, map } from 'rxjs/operators'
Expand Down Expand Up @@ -45,25 +45,30 @@ export class RestIot extends IotDevice<RestIotState> implements BaseDevice {
}

async turnOnRoutine() {
const favorites = await this.fetchFavorites()
this.setCurrent('routine', 1, favorites[0].id)
const routines = await this.fetchRoutines()
this.setCurrent('routine', 1, routines[0].id)
}

turnOff() {
this.setCurrent('none', 0, 0)
}

async fetchFavorites() {
const favoritesPath = apiPath(
async fetchRoutines() {
const routinesPath = apiPath(
`service/app/routine/v2/fetch?macAddress=${encodeURIComponent(
this.info.macAddress,
)}&types=favorite`,
)}`,
),
favorites = await this.restClient.request<RestIotFavorite[]>({
url: favoritesPath,
allRoutines = await this.restClient.request<RestIotRoutine[]>({
url: routinesPath,
method: 'GET',
}),
sortedRoutines = allRoutines.sort((a, b) => a.displayOrder - b.displayOrder),
touchRingRoutines = sortedRoutines.filter((routine) => {
return routine.type === 'favorite' // Before upgrade, only favorites were on touch ring
|| routine.button0 // After upgrade, many routine types can be on touch ring but will have `button0: true`
})

return favorites.sort((a, b) => a.displayOrder - b.displayOrder)
return touchRingRoutines
}
}
11 changes: 6 additions & 5 deletions packages/shared/hatch-sleep-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,20 @@ export interface RestIotState {
streaming: { status: 'none' | string }
}

export interface RestIotFavorite {
export interface RestIotRoutine {
id: number
macAddress: string
name: string
type: 'favorite'
type: 'favorite' | 'sleep' | 'wake' | 'flex'
active: boolean
enabled: boolean
displayOrder: number
sleepScene: boolean
followBySleepScene: boolean
startTime: null
endTime: null
daysOfWeek: null
button0: boolean // true if this routine is available on touch ring
startTime: null | string
endTime: null | string
daysOfWeek: null | any
steps: any[]
}

Expand Down

0 comments on commit 512fdfe

Please sign in to comment.