feat: home assistant entity generic toggle (#2015)

Added support for generic toggle on Home Assistant entities that support
it. For entities that does not support toggle the feature will silently
fail. This way matches how Home Assistant handles it.
This commit is contained in:
Dennis Vesterlund
2024-05-06 20:00:13 +02:00
committed by GitHub
parent 6a5836f096
commit 18cd1f961f
4 changed files with 85 additions and 10 deletions

View File

@@ -88,6 +88,36 @@ export const smartHomeEntityStateRouter = createTRPCRouter({
}
}
return false;
}),
triggerToggle: protectedProcedure
.input(z.object({
widgetId: z.string(),
configName: z.string()
})).mutation(async ({ input }) => {
const config = getConfig(input.configName);
const widget = config.widgets.find(widget => widget.id === input.widgetId) as ISmartHomeEntityStateWidget | null;
if (!widget) {
Consola.error(`Referenced widget ${input.widgetId} does not exist on backend.`);
throw new TRPCError({
code: 'CONFLICT',
message: 'Referenced widget does not exist on backend',
});
}
const instances = config.apps.filter((app) => app.integration?.type == 'homeAssistant');
for (const instance of instances) {
const url = new URL(instance.url);
const client = HomeAssistantSingleton.getOrSet(url, findAppProperty(instance, 'apiKey'));
const state = await client.triggerToggle(widget.properties.entityId);
if (state) {
return true;
}
}
return false;
}),
});