Adds folder sensor#12208
Adds folder sensor#12208balloob merged 20 commits intohome-assistant:devfrom robmarkcole:folder-sensor
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.
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.""" |
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.
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.
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.
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.
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.
This is not an allowed unit of measurement.
| self._sorted_files_list = [] | ||
| self._number_of_files = None | ||
| self._recent_modified_file = '' | ||
| self._last_updated = '' |
Address requested changes in home-assistant/core#12208
Address requests changes
MartinHjelmare
left a comment
There was a problem hiding this comment.
Nice! Can be merged when build passes.
MartinHjelmare
left a comment
There was a problem hiding this comment.
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.
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.
As in, this will not work on Windows? OK will try something else
There was a problem hiding this comment.
Use https://docs.python.org/3/library/os.path.html#os.path.split or Pathlib
| '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] |
| '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.
We should not do this. This is way too much data for in the state machine.
| 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.
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.
|
|
||
| def update(self): | ||
| """Update the sensor.""" | ||
| self._sorted_files_list = get_sorted_files_list( |
There was a problem hiding this comment.
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.
|
@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:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass