bexem / Fetch-Latest-File

A custom component for home assistant to fetch the latest file in a given directory.
GNU General Public License v3.0
2 stars 1 forks source link

Never returns results #1

Open ScottESanDiego opened 2 weeks ago

ScottESanDiego commented 2 weeks ago

Using HA 2024.8.3 , I can't get this to return any attributes.

In "Developer Tools", calling this:

action: fetch_latest_file.fetch
data:
  FileName: conf
  Directory: /config

yields No matching files found even though clearly there is a configuration.yaml in that directory.

Before I fork this or dig into it, is there something obvious I'm missing?

bexem commented 2 weeks ago

I'm sorry you're having troubles with it, I'm not really using the component any longer since I now fetch the latest modified file with folder watcher. (I use it to get the latest person detection video file from my reolink camera/nvr)

Anyway, I've just installed it and tested it, and with this action/service call:

action: fetch_latest_file.fetch
data:
  FileName: configuration
  Directory: /config
  Extension: 
    - ".yaml"

It returns:

fetch_latest_file.file 
Status: Done
Attribute: config: /config/configuration.yaml

Feel free to fork it or I can even give you ownership. I honestly thought I archived the repository.

ScottESanDiego commented 1 week ago

I think the root cause has to do with how it's filtering the files it appends to the dict. This change makes it work for me:

--- __init__.py.orig    2024-09-07 16:22:22.638961306 -0700
+++ __init__.py 2024-09-07 16:28:47.072630293 -0700
@@ -33,7 +33,7 @@
         if extensions is not None:
             extensions = [ext.lower().strip('.') for ext in extensions]

-        files = {ext: [] for ext in extensions}
+        files = {}
         try:
             for dirpath, dirnames, filenames in os.walk(directory):
                     for filename in filenames:
@@ -41,8 +41,11 @@
                             file_path = os.path.join(dirpath, filename)
                             file_size = os.path.getsize(file_path)
                             file_ext = os.path.splitext(filename)[1].lower().strip('.')
-                            if file_size >= min_size and (extensions is None or file_ext in extensions or file_ext == ''):
-                                files[file_ext].append(os.path.join(dirpath, filename))
+                            if file_size >= min_size and (file_ext in extensions or extensions == []):
+                                if file_ext not in files:
+                                    files[file_ext]=[os.path.join(dirpath, filename)]
+                                else:
+                                    files[file_ext].append(os.path.join(dirpath, filename))

         except FileNotFoundError:
             hass.states.set(f"{DOMAIN}.file", "Directory not found")

I'll make a fork or something if this works out for me. :-)

Did you find a better solution for decent notifications with Reolink cameras that just dump things into an FTP dir? The Folder Watcher way feels odd. What's your setup for that look like?