TarheelGrad1998 / gallery-card

A custom card for Home Assistant that will display images and/or videos from a folder in the style of a gallery.
99 stars 33 forks source link

galler-card no longer working file platform files #41

Closed 0n3man closed 1 year ago

0n3man commented 2 years ago

I've use the files custom-component for a couple of years. I was a few version behind on HA so just updated to 2022.6.5. I display images using the files custom-component displayed via the gallery card. I'm pretty sure I got the files custom component from your previous implementation of gallery-panel. The files custom component used to include the attributes path, filter, number of file, bytes, fileList, sort, unit_of_measurement, icon and friendly_name. After updating to 2022.6.5 the associated sensor entities only include unit_of_measurement, icon and friendly_name. So the gallery card no longer shows any images. Any chance there is a fix somewhere for the files custom component that fixes this issue? I use the card to display images captured with alarm system events so the images need to be displayed based on file date. While the files are named including date and time, they start with the name of the object detected (person, dog, etc.) so there doesn't seem to be a way to use the folder platform to create a sensor with the images sorted by date. Thanks.

TarheelGrad1998 commented 2 years ago

Thanks for the heads up. I am still on 2022.5 but plan to upgrade in the next week or so, and I do use both components, so if they are fixable I certainly will.

0n3man commented 2 years ago

As the gallery card allows for pulling the date and time from the file name, and then sorting based on these dates and time, any chance for modification to allow some type of wild card character to allow processing filenames with additional data. Here are a couple examples of the file names I using: person-06-07-22-21:57:31.jpg, cat-06-04-22-21:40:26.jpg , dog-06-04-22-00:44:06.jpg. I'm using zoneminder to do image processing of my video streams and then passing snapshots with new objects are identified to HA. I tag the file that are then passed to HA with the object name, along with the date and time. If the gallery card handled pulling date and time from this type of file name then I could use the folder platform instead of the files platform.

0n3man commented 2 years ago

For some reason I couldn't clone your repo to my online github account so I couldn't generate a pull request. The following is patch that works for my filename formatting. If you like it please consider including it in your gallery-card.js.

diff --git a/gallery-card.js b/gallery-card.js
index 08b0435..9d8097a 100644
--- a/gallery-card.js
+++ b/gallery-card.js
@@ -571,7 +571,7 @@ class GalleryCard extends LitElement {
       if (fileNameFormat === undefined || captionFormat === undefined)
           fileCaption = fileName;
       else {
-        var tokens = ["%YYY", "%m", "%d", "%H", "%M", "%S", "%p"]
+        var tokens = ["%YYY", "%y", "%m", "%d", "%H", "%M", "%S", "%p"]
         fileCaption = captionFormat;

         var hr = 0;
@@ -581,12 +581,20 @@ class GalleryCard extends LitElement {
         var hour = 0;
         var min = 0;
         var sec = 0;
+        var skipVal = 0;
+        skipVal = fileName.search(/\d/) - 1;
+        if (skipVal < 0) {
+          skipVal = 0;
+        }
+
         for (let token of tokens) {
           var searchIndex = fileNameFormat.indexOf(token);

           if (searchIndex >= 0) {
+            searchIndex = searchIndex + skipVal;
             var val = fileName.substring(searchIndex, searchIndex + token.length);
             if (token == "%YYY" ) year = parseInt(val);
+            if (token == "%y" ) year = parseInt(val);
             if (token == "%m" ) month = parseInt(val);
             if (token == "%d" ) day = parseInt(val);
             if (token == "%H" ) hour = parseInt(val);
@@ -1131,6 +1139,7 @@ class GalleryCardEditor extends LitElement {
         Use the following placeholders to reformat the file name into the caption:
         <ul>
           <li>%YYY - A 4 digit year, e.g. 2019</li>
+          <li>%y - A 2 digit year, e.g. 22</li>
           <li>%m - The 2 digit month</li>
           <li>%d - The 2 digit day</li>
           <li>%H - The 2 digit hour</li>
@@ -1390,3 +1399,4 @@ window.customCards.push({
   preview: false, // Optional - defaults to false
   description: "The Gallery Card allows for viewing multiple images/videos.  Requires the Files sensor availble at https://github.com/TarheelGrad1998" // Optional
 });
+
0n3man commented 2 years ago

In the patch above I subtract one on this line

skipVal = fileName.search(/\d/) - 1;

It didn't make any sense why I need to do the -1. This was on a test system I have setup to make things work. I just moved the code to my production system and to make it work I had to remove the -1. It's strange. One is running on RPI4 the operation is running on odroid n2+, both as supervised installs.

TarheelGrad1998 commented 2 years ago

I finally got around to upgrading (to 2022.6.7) and my files component still contains all the same attributes. Maybe the core team fixed something in v .6 or .7 that was breaking yours? Or if not maybe it was something specific to your configuration? I'm not sure if you're interested in the original fix since you have created a different solution.

As to the pull request, I do have an in progress change where I have completely redone the date parsing, but I agree your change could have use for others with "mixed" file names. I'll take a look when I get time to continue working on that update..

0n3man commented 2 years ago

Thanks for the feedback. I'll have a look at your update. My preference is to use your code if possible. I hate diverging code if it can be prevented.

TarheelGrad1998 commented 1 year ago

This is still working as of version 3.4, though you might take a look at the Local Media in media_source to see if that's a better option.