File size custom component

Based on that question, I was talking about python_script.

Sorry if @mattlward wasn’t, I’ve probably caused mass confusion.

Works great! Thanks for sharing.

hmm, got this in my sensors file:

- platform: filesize
  file_paths:
    - /config/home-assistant_v2.db
    - /config/home-assistant.log

and your cc in the /config/custom_components/sensor directory.

2018-02-06 20:03:18 ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.filesize

No sensor showing up. What am I doing wrong? Hassio 0.62.1 here.

Cheers,
Marius

That’s odd. Try rebooting the pi

nevermind, please ignore. feel embarrassed…
named it file_size.py…

showing up nicely now (besides the ‘no state history found’-issue of hassio 62.1)

46

Nice!

1 Like

So… it is not working for me. I have my config in multiple directories and I have a feeling that I need to do and include in my configuration.yaml file…?

Error:

ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.filesize

My sensor entry in my included folder:

- platform: filesize
  file_paths:
    - /config/home-assistant_v2.db

And the code is in /config/custom_components/sensor filesize.py. Do I need to tell Hassio to include the custom component directory some how? I also set the directories and file to 777 incase it was a permissions problem.

So, I now have my directory structure like it wants, but the load pukes! I am running 62.1 on a Hassio Pi.

2018-02-06 22:07:02 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/tasks.py", line 182, in _step
    result = coro.throw(exc)
  File "/usr/lib/python3.6/site-packages/homeassistant/setup.py", line 60, in async_setup_component
    return (yield from task)
  File "/usr/lib/python3.6/asyncio/futures.py", line 332, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
    future.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/asyncio/tasks.py", line 180, in _step
    result = coro.send(None)
  File "/usr/lib/python3.6/site-packages/homeassistant/setup.py", line 159, in _async_setup_component
    conf_util.async_process_component_config(hass, config, domain)
  File "/usr/lib/python3.6/site-packages/homeassistant/config.py", line 624, in async_process_component_config
    platform = get_platform(domain, p_name)
  File "/usr/lib/python3.6/site-packages/homeassistant/loader.py", line 104, in get_platform
    return get_component(PLATFORM_FORMAT.format(domain, platform))
  File "/usr/lib/python3.6/site-packages/homeassistant/loader.py", line 142, in get_component
    module = importlib.import_module(path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/sensor/filesize.py", line 93
    return self._unit_of_measurement
                                   ^
SyntaxError: 'return' outside function

I got it! I could not get firefox to DL the .py file and had to cut and paste it and it spaced the last line wrongly.

Great work, Thanks!

1 Like

Since this is a generic component of great purpose, I suggest it is included in the core components. Do you mind submitting a PR?

@rtvb Already submitted PR but could use some assistance with the unit test

1 Like

Running hass.io 0.63.2 I get

2018-02-16 11:22:51 ERROR (SyncWorker_11) [custom_components.sensor.filesize] Filepath /config/home-assistant_v2.db is not valid or allowed

The filename looks good to me. Is this a permissions issue? What else could be wrong?

Add to whitelist

1 Like

Tried that. This doesn’t seem to work. Same error message. Other components are able to read the /config path with my Hass.io 0.63.2 on a Raspberry Pi 2B. So something else is probably wrong.

This is my current seeminly valid (packaged) setup:

system.yaml

homeassistant:
  whitelist_external_dirs:
    - /config

sensor:
  - platform: filesize
    file_paths:
      - /config/home-assistant_v2.db

For the purpose of this issue I have also tested some other files. The also growing in use log file, a static configuration file and a non-existent file, and an existing empty dummy file in a different, not explicitly whitelisted path:

homeassistant:
  whitelist_external_dirs:
    - /config

sensor:
  - platform: filesize
    file_paths:
      - /config/home-assistant_v2.db
      - /config/home-assistant.log
      - /config/configuration.yaml
      - /config/notfound.txt
      - /share/empty.txt

This raises an error for the file notfound.txt.

2018-02-16 15:00:33 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.filesize]: not a file @ data['file_paths'][3]. Got '/config/notfound.txt'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.filesize/

I expected a warning for the non-existing file, but not that the component would not load. So I removed that file and tried again:

homeassistant:
  whitelist_external_dirs:
    - /config

sensor:
  - platform: filesize
    file_paths:
      - /config/home-assistant_v2.db
      - /config/home-assistant.log
      - /config/configuration.yaml
      # - /config/notfound.txt
      - /share/empty.txt

The component loads, and again I see the error for the .db file. But ONLY for that file. The other 2 files do not show errors in the logs. And NO sensors are created.

You asked for some unit testing, here it is. :wink:

Still no succes :angry:

I found it. :smile:

Whitelisting is required. I have a packaged setup. It appears that the whitelist variable can only be used in configuration.yaml and not inside a package file.

For the record, my current setup is:

configuration.yaml

homeassistant:
  packages: !include_dir_named packages
  whitelist_external_dirs:
    - /config
  # etc.

packages/system.yaml

sensor:
  - platform: filesize
    file_paths:
      - /config/home-assistant_v2.db
2 Likes

White_list definitely requires improved documentation

1 Like

If anyone can help advise on the unit test I would be very grateful:

I was wondering why files in my config folder need to be whitelisted. Some other components (e.g. http) can access files. Is this because this still is a custom component?

Those components probably predate the requirement for whitelisting. This is submitted for inclusion as a full component

Have you succeeded in the meantime? I used this script and it worked:

- platform: filesize
  name: Filesize Database
  file_paths:
    - /home/homeassistant/.homeassistant/home-assistant_v2.db

who are you asking?