Skip to content

Commit b8d93fa

Browse files
committed
Only request known device types
1 parent 5278107 commit b8d93fa

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

.changeset/tender-shirts-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"homebridge-hatch-baby-rest": patch
3+
---
4+
5+
Only fetch known products to avoid accidentally sending a bad device request. Should fix #125

packages/homebridge-hatch-baby-rest/api.ts

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ export class HatchBabyApi {
5050
})
5151
}
5252

53-
async getIotDevices(memberProducts: string[]) {
53+
async getIotDevices(...products: Product[]) {
5454
// Combine the user's products with the known products to ensure we get all devices
55-
const allProducts = Array.from(
56-
new Set([...memberProducts, ...knownProducts]),
57-
),
58-
productFetchQueryString = allProducts
55+
const productFetchQueryString = products
5956
.map((product) => 'iotProducts=' + product)
6057
.join('&'),
6158
devices =
@@ -157,12 +154,9 @@ export class HatchBabyApi {
157154
}
158155

159156
async getDevices() {
160-
const member = await this.getMember(),
161-
memberProducts = member.products.filter(
162-
(product: Product) => !ignoredProducts.includes(product),
163-
),
164-
[devices, onIotClient] = await Promise.all([
165-
this.getIotDevices(memberProducts),
157+
const [devices, member, onIotClient] = await Promise.all([
158+
this.getIotDevices(...knownProducts),
159+
this.getMember(),
166160
this.getOnIotClient(),
167161
]),
168162
createDevices = <T extends IotDevice<any>>(
@@ -189,33 +183,44 @@ export class HatchBabyApi {
189183
logInfo('Unsupported Product Found: ' + product + debugMessage)
190184

191185
if (this.config.debug) {
192-
const debugDevices = createDevices(product, IotDevice)
193-
for (const device of debugDevices) {
194-
try {
195-
logInfo(`Debug info for ${product} ${device.info.name}:`)
196-
logInfo(
197-
JSON.stringify(
198-
{
199-
...device.info,
200-
id: '***',
201-
macAddress: '***',
202-
thingName: '***',
203-
memberId: '***',
204-
email: '***',
205-
},
206-
null,
207-
2,
208-
),
186+
this.getIotDevices(product)
187+
.then(async (unknownDevices) => {
188+
const debugDevices = unknownDevices.map(
189+
(device) => new IotDevice(device, onIotClient),
209190
)
210-
logInfo(`State for ${product} ${device.info.name}:`)
211-
logInfo(JSON.stringify(await device.getCurrentState(), null, 2))
212-
} catch (e) {
213-
logError(
214-
`Failed to get debug info for ${product} ${device.info.name}`,
215-
)
216-
logError(e)
217-
}
218-
}
191+
for (const device of debugDevices) {
192+
try {
193+
logInfo(`Debug info for ${product} ${device.info.name}:`)
194+
logInfo(
195+
JSON.stringify(
196+
{
197+
...device.info,
198+
id: '***',
199+
macAddress: '***',
200+
thingName: '***',
201+
memberId: '***',
202+
email: '***',
203+
},
204+
null,
205+
2,
206+
),
207+
)
208+
logInfo(`State for ${product} ${device.info.name}:`)
209+
logInfo(
210+
JSON.stringify(await device.getCurrentState(), null, 2),
211+
)
212+
} catch (e) {
213+
logError(
214+
`Failed to get debug info for ${product} ${device.info.name}`,
215+
)
216+
logError(e)
217+
}
218+
}
219+
})
220+
.catch((error) => {
221+
logError('Failed to get debug info for ' + product)
222+
logError(error)
223+
})
219224
}
220225
}
221226
}

0 commit comments

Comments
 (0)