Update homeAssistantWSRequest to accept passthrough JSON

This commit is contained in:
Justin Wong
2022-08-08 19:30:03 +08:00
parent 4c8b60905c
commit 4a80d14209
2 changed files with 11 additions and 3 deletions
+5 -2
View File
@@ -77,7 +77,8 @@ export const getRecordingsSummary = async (
type: "frigate/recordings/summary", type: "frigate/recordings/summary",
instance_id: client_id, instance_id: client_id,
camera: camera_name camera: camera_name
} },
true,
); );
}; };
@@ -106,7 +107,8 @@ export const getRecordingSegments = async (
camera: camera_name, camera: camera_name,
before: Math.floor(before.getTime() / 1000), before: Math.floor(before.getTime() / 1000),
after: Math.ceil(after.getTime() / 1000), after: Math.ceil(after.getTime() / 1000),
} },
true,
); );
}; };
@@ -133,6 +135,7 @@ export async function retainEvent(
hass, hass,
retainResultSchema, retainResultSchema,
retainRequest, retainRequest,
true,
); );
if (!response.success) { if (!response.success) {
throw new FrigateCardError(localize('error.failed_retain'), { throw new FrigateCardError(localize('error.failed_retain'), {
+6 -1
View File
@@ -25,6 +25,7 @@ export async function homeAssistantWSRequest<T>(
hass: HomeAssistant, hass: HomeAssistant,
schema: ZodSchema<T>, schema: ZodSchema<T>,
request: MessageBase, request: MessageBase,
passthrough = false,
): Promise<T> { ): Promise<T> {
let response; let response;
try { try {
@@ -44,7 +45,11 @@ export async function homeAssistantWSRequest<T>(
request: request, request: request,
}); });
} }
const parseResult = schema.safeParse(response); // Some endpoints on the integration pass through JSON directly from Frigate
// These end up wrapped in a string and must be unwrapped first
const parseResult = passthrough
? schema.safeParse(JSON.parse(response))
: schema.safeParse(response);
if (!parseResult.success) { if (!parseResult.success) {
throw new FrigateCardError(localize('error.invalid_response'), { throw new FrigateCardError(localize('error.invalid_response'), {
request: request, request: request,