diff --git a/api/src/controllers/PhotoController.js b/api/src/controllers/PhotoController.js index 4545bcc..f731973 100644 --- a/api/src/controllers/PhotoController.js +++ b/api/src/controllers/PhotoController.js @@ -1,3 +1,5 @@ +import fetch from "node-fetch"; + export class PhotoController { constructor(db, photoService) { this.db = db; @@ -13,12 +15,27 @@ export class PhotoController { return res.status(400).json({ error: 'Invalid settings' }); } - let collections = settings.unsplash.collectionsId; + let collections = settings.unsplash?.HACollectionsId; + + if (collections && collections.length > 0) { + const resp = await fetch(`${settings.hass.endpoint}/api/states/${collections}`, { + headers: { + Authorization: `Bearer ${settings.hass.token}` + } + }); + const data = await resp?.json(); + collections = data.state; + if (collections.includes(',')) { + collections = collections.split(','); + } + } else { + collections = settings.unsplash.collectionsId; + } if (collections) { collections = Array.isArray(collections) ? collections : [collections]; } - const photo = await this.photoService.pickPictureFromFile(collections, false, settings.unsplash); + const photo = await this.photoService.pickPictureFromFile(collections, true, settings.unsplash); if (!photo) { return res.status(500).json({ error: 'Failed to fetch photo' }); diff --git a/src/app/pages/admin/admin.component.html b/src/app/pages/admin/admin.component.html index 7715fa0..36673b5 100644 --- a/src/app/pages/admin/admin.component.html +++ b/src/app/pages/admin/admin.component.html @@ -55,6 +55,14 @@ + +
+ + +
+
+ You can specify a Home Assistant entity that contains the Unsplash Collections ID. This way you can change the collections directly from Home Assistant.
For example, you can use an input_text entity with the collections ID as the value separated by commas.

If you use this option, the collections ID field will be ignored. +
diff --git a/src/app/pages/admin/admin.component.ts b/src/app/pages/admin/admin.component.ts index bd7335b..b65e2dc 100644 --- a/src/app/pages/admin/admin.component.ts +++ b/src/app/pages/admin/admin.component.ts @@ -19,7 +19,8 @@ export class AdminComponent implements OnInit { enabled: false, accessKey: '', secretKey: '', - collectionsId: '' + collectionsId: '', + HACollectionsId: '' }, hass: { endpoint: '', @@ -49,7 +50,8 @@ export class AdminComponent implements OnInit { enabled: respJson?.unsplash?.enabled || false, accessKey: respJson?.unsplash?.accessKey || '', secretKey: respJson?.unsplash?.secretKey || '', - collectionsId: respJson?.unsplash?.collectionsId ? respJson?.unsplash?.collectionsId.join(',') : '' + collectionsId: respJson?.unsplash?.collectionsId ? respJson?.unsplash?.collectionsId.join(',') : '', + HACollectionsId: respJson?.unsplash?.HACollectionsId || '' }, hass: { endpoint: respJson?.hass?.endpoint || '',