-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
File sensor #7569
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
Merged
Merged
File sensor #7569
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """ | ||
| Support for sensor value(s) stored in local files. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.file/ | ||
| """ | ||
| import os | ||
| import asyncio | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import ( | ||
| CONF_VALUE_TEMPLATE, CONF_NAME, CONF_UNIT_OF_MEASUREMENT) | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_FILE_PATH = 'file_path' | ||
|
|
||
| DEFAULT_NAME = 'File' | ||
|
|
||
| ICON = 'mdi:file' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_FILE_PATH): cv.string, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_VALUE_TEMPLATE): cv.template, | ||
| vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, | ||
| }) | ||
|
|
||
|
|
||
| @asyncio.coroutine | ||
| def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | ||
| """Set up the file sensor.""" | ||
| file_path = config.get(CONF_FILE_PATH) | ||
| name = config.get(CONF_NAME) | ||
| unit = config.get(CONF_UNIT_OF_MEASUREMENT) | ||
| value_template = config.get(CONF_VALUE_TEMPLATE) | ||
|
|
||
| if value_template is not None: | ||
| value_template.hass = hass | ||
|
|
||
| async_add_devices( | ||
| [FileSensor(name, file_path, unit, value_template)], True) | ||
|
|
||
|
|
||
| class FileSensor(Entity): | ||
| """Implementation of a file sensor.""" | ||
|
|
||
| def __init__(self, name, file_path, unit_of_measurement, value_template): | ||
| """Initialize the file sensor.""" | ||
| self._name = name | ||
| self._file_path = file_path | ||
| self._unit_of_measurement = unit_of_measurement | ||
| self._val_tpl = value_template | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit the value is expressed in.""" | ||
| return self._unit_of_measurement | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Return the icon to use in the frontend, if any.""" | ||
| return ICON | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| def update(self): | ||
| """Get the latest entry from a file and updates the state.""" | ||
| try: | ||
| with open(self._file_path, 'r', encoding='utf-8') as file_data: | ||
| for line in file_data: | ||
| data = line | ||
| data = data.strip() | ||
| except (IndexError, FileNotFoundError, IsADirectoryError): | ||
| _LOGGER.warning("File or data not present at the moment: %s", | ||
| os.path.basename(self._file_path)) | ||
| return | ||
|
|
||
| if self._val_tpl is not None: | ||
| self._state = self._val_tpl.async_render_with_possible_json_value( | ||
| data, None) | ||
| else: | ||
| self._state = data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| """The tests for local file sensor platform.""" | ||
| import unittest | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| # Using third party package because of a bug reading binary data in Python 3.4 | ||
| # https://bugs.python.org/issue23004 | ||
| from mock_open import MockOpen | ||
|
|
||
| from homeassistant.setup import setup_component | ||
| from homeassistant.const import STATE_UNKNOWN | ||
|
|
||
| from tests.common import get_test_home_assistant | ||
|
|
||
|
|
||
| class TestFileSensor(unittest.TestCase): | ||
| """Test the File sensor.""" | ||
|
|
||
| def setup_method(self, method): | ||
| """Set up things to be run when tests are started.""" | ||
| self.hass = get_test_home_assistant() | ||
|
|
||
| def teardown_method(self, method): | ||
| """Stop everything that was started.""" | ||
| self.hass.stop() | ||
|
|
||
| @patch('os.path.isfile', Mock(return_value=True)) | ||
| @patch('os.access', Mock(return_value=True)) | ||
| def test_file_value(self): | ||
| """Test the File sensor.""" | ||
| config = { | ||
| 'sensor': { | ||
| 'platform': 'file', | ||
| 'name': 'file1', | ||
| 'file_path': 'mock.file1', | ||
| } | ||
| } | ||
|
|
||
| m_open = MockOpen(read_data='43\n45\n21') | ||
| with patch('homeassistant.components.sensor.file.open', m_open, | ||
| create=True): | ||
| assert setup_component(self.hass, 'sensor', config) | ||
| self.hass.block_till_done() | ||
|
|
||
| state = self.hass.states.get('sensor.file1') | ||
| self.assertEqual(state.state, '21') | ||
|
|
||
| @patch('os.path.isfile', Mock(return_value=True)) | ||
| @patch('os.access', Mock(return_value=True)) | ||
| def test_file_value_template(self): | ||
| """Test the File sensor with JSON entries.""" | ||
| config = { | ||
| 'sensor': { | ||
| 'platform': 'file', | ||
| 'name': 'file2', | ||
| 'file_path': 'mock.file2', | ||
| 'value_template': '{{ value_json.temperature }}', | ||
| } | ||
| } | ||
|
|
||
| data = '{"temperature": 29, "humidity": 31}\n' \ | ||
| '{"temperature": 26, "humidity": 36}' | ||
|
|
||
| m_open = MockOpen(read_data=data) | ||
| with patch('homeassistant.components.sensor.file.open', m_open, | ||
| create=True): | ||
| assert setup_component(self.hass, 'sensor', config) | ||
| self.hass.block_till_done() | ||
|
|
||
| state = self.hass.states.get('sensor.file2') | ||
| self.assertEqual(state.state, '26') | ||
|
|
||
| @patch('os.path.isfile', Mock(return_value=True)) | ||
| @patch('os.access', Mock(return_value=True)) | ||
| def test_file_empty(self): | ||
| """Test the File sensor with an empty file.""" | ||
| config = { | ||
| 'sensor': { | ||
| 'platform': 'file', | ||
| 'name': 'file3', | ||
| 'file_path': 'mock.file', | ||
| } | ||
| } | ||
|
|
||
| m_open = MockOpen(read_data='') | ||
| with patch('homeassistant.components.sensor.file.open', m_open, | ||
| create=True): | ||
| assert setup_component(self.hass, 'sensor', config) | ||
| self.hass.block_till_done() | ||
|
|
||
| state = self.hass.states.get('sensor.file3') | ||
| self.assertEqual(state.state, STATE_UNKNOWN) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
expected 2 blank lines, found 1