mirror of
https://github.com/home-assistant/core.git
synced 2026-09-24 07:25:52 -05:00
Adds folder sensor (#12208)
* Adds folder sensor The state of the sensor is the time that the most recently modified file in a folder was modified. * Address lint errors * Edit docstrings Makes the recommended edits to docstrings * Update .coveragerc Add sensor/folder.py * Update folder.py * Address requests Address requests changes * Adds folder * Adds test, tidy up * Tidy * Update test_folder.py * Update folder.py * Fix setup Fix setup with else statement * Update folder.py * Update folder.py * Remove list of files from attributes * Update test_folder.py * Update folder.py * Update test_folder.py * Update folder.py * Update folder.py
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""The tests for the folder sensor."""
|
||||
import unittest
|
||||
import os
|
||||
|
||||
from homeassistant.components.sensor.folder import CONF_FOLDER_PATHS
|
||||
from homeassistant.setup import setup_component
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
CWD = os.path.join(os.path.dirname(__file__))
|
||||
TEST_FOLDER = 'test_folder'
|
||||
TEST_DIR = os.path.join(CWD, TEST_FOLDER)
|
||||
TEST_TXT = 'mock_test_folder.txt'
|
||||
TEST_FILE = os.path.join(TEST_DIR, TEST_TXT)
|
||||
|
||||
|
||||
def create_file(path):
|
||||
"""Create a test file."""
|
||||
with open(path, 'w') as test_file:
|
||||
test_file.write("test")
|
||||
|
||||
|
||||
class TestFolderSensor(unittest.TestCase):
|
||||
"""Test the filesize sensor."""
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
if not os.path.isdir(TEST_DIR):
|
||||
os.mkdir(TEST_DIR)
|
||||
self.hass.config.whitelist_external_dirs = set((TEST_DIR))
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
if os.path.isfile(TEST_FILE):
|
||||
os.remove(TEST_FILE)
|
||||
os.rmdir(TEST_DIR)
|
||||
self.hass.stop()
|
||||
|
||||
def test_invalid_path(self):
|
||||
"""Test that an invalid path is caught."""
|
||||
config = {
|
||||
'sensor': {
|
||||
'platform': 'folder',
|
||||
CONF_FOLDER_PATHS: 'invalid_path'}
|
||||
}
|
||||
self.assertTrue(
|
||||
setup_component(self.hass, 'sensor', config))
|
||||
assert len(self.hass.states.entity_ids()) == 0
|
||||
|
||||
def test_valid_path(self):
|
||||
"""Test for a valid path."""
|
||||
create_file(TEST_FILE)
|
||||
config = {
|
||||
'sensor': {
|
||||
'platform': 'folder',
|
||||
CONF_FOLDER_PATHS: TEST_DIR}
|
||||
}
|
||||
self.assertTrue(
|
||||
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'
|
||||
assert state.attributes.get('number_of_files') == 1
|
||||
Reference in New Issue
Block a user