-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds folder sensor #12208
Adds folder sensor #12208
Conversation
The state of the sensor is the time that the most recently modified file in a folder was modified.
return | ||
|
||
def get_sorted_files_list(self, folder_path, filter_term): | ||
"""Rerturn the sorted list of files in a directory, applying filter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo....return
. Also, shorten the docstring.
return sorted_files_list | ||
|
||
def get_last_updated(self, recent_modified_file): | ||
"""Rerturn the datetime a file was last modified.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same...typo return
Makes the recommended edits to docstrings
Don't forget to add your file to .coveragerc as described in the pull request template. |
Add sensor/folder.py
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): | ||
"""Set up the folder sensor.""" | ||
folder = Folder(config.get(CONF_FOLDER_PATHS), config.get(CONF_FILTER)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All paths have to be whitelisted by checking it with hass.config.is_allowed_path(path)
self._last_updated = '' | ||
self._name = folder_path.split("/")[-2] | ||
self._unit_of_measurement = '' | ||
self.update() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this. It's already called during entity addition.
|
||
self._last_updated = self.get_last_updated( | ||
self._recent_modified_file) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this unnecessary return statement.
@property | ||
def device_state_attributes(self): | ||
"""Return other details about the sensor state.""" | ||
attrs = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please construct the whole dict directly.
attr = {
'folder': self._folder_path,
...
}
self._recent_modified_file = '' | ||
self._last_updated = '' | ||
self._name = folder_path.split("/")[-2] | ||
self._unit_of_measurement = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not an allowed unit of measurement.
self._sorted_files_list = [] | ||
self._number_of_files = None | ||
self._recent_modified_file = '' | ||
self._last_updated = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Init as None
.
Address requested changes in home-assistant/core#12208
Address requests changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Can be merged when build passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I forgot about tests. This should be tested since it's not integrating a 3rd party device.
setup_component(self.hass, 'sensor', config)) | ||
assert len(self.hass.states.entity_ids()) == 1 | ||
state = self.hass.states.get('sensor.test_folder') | ||
#assert state.state == '0.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
@MartinHjelmare @arsaboo @balloob @fabaff I've made all the required changes now, don't know what that strange error from Travis is about. Cheers |
self._number_of_files = None | ||
self._recent_modified_file = None | ||
self._last_updated = None | ||
self._name = folder_path.split("/")[-2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must not use Windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in, this will not work on Windows? OK will try something else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use https://docs.python.org/3/library/os.path.html#os.path.split or Pathlib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
'filter': self._filter_term, | ||
'modified_file': self._recent_modified_file.split('/')[-1], | ||
'number_of_files': len(self._sorted_files_list), | ||
'files': [f.split('/')[-1] for f in self._sorted_files_list] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seperator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
'filter': self._filter_term, | ||
'modified_file': os.path.split(self._recent_modified_file)[1], | ||
'number_of_files': len(self._sorted_files_list), | ||
'files': [os.path.split(f)[1] for f in self._sorted_files_list] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not do this. This is way too much data for in the state machine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
attr = { | ||
'path': self._folder_path, | ||
'filter': self._filter_term, | ||
'modified_file': os.path.split(self._recent_modified_file)[1], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop this attribute. If people want this info, we should get a watchdog component to monitor folders/files and fire events. If people want to automate on this, it will not work if 2 files get added between 2 updates. Then they want to have that fixed etc. I think that we should have people do the right thing right away by not including this, so they will look for the watchdog component instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
|
||
def update(self): | ||
"""Update the sensor.""" | ||
self._sorted_files_list = get_sorted_files_list( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better if we don't keep this in memory but instead only set the instance variables that we want to derive from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
@balloob and @MartinHjelmare all changes have beeb made I believe |
Description:
Home-assistant custom component for monitoring the contents of a folder.
The state of the sensor is the size in MB of files within the folder that meet the filter criteria. The use case is detecting when a file is created in a folder . For example, I have a USB camera attached to my HA instance that saves a new timestamped photo when motion is detected. This sensor allows me to detect when another image is saved.
The number of files in the folder and the size in bytes are exposed in attributes.
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4588
Example entry for
configuration.yaml
(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.If the code does not interact with devices:
tox
run successfully. Your PR cannot be merged unless tests pass