Command Line Sensor - json_attributes (#15679)
* Add tests to command_line for json_attrs * Add json_attrs to command_line * Remove whitespace on blank line * Stick to <80 row length * Use collections.Mapping, not dict * Rename *attrs to *attributes * Remove extraneous + for string concat * Test multiple keys * Add test Makes sure the sensor's attributes don't contain a value for a missing key, even if we want that key. * Test that unwanted keys are skipped * Remove additional log line * Update tests for log changes * Fix ordering
This commit is contained in:
1 parent
a2b793c61b
commit
1d68f4e279
2 files changed
+153
-16
No files matched your search
@@ -1,5 +1,6 @@
|
||||
"""The tests for the Command line sensor platform."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.components.sensor import command_line
|
||||
@@ -17,6 +18,10 @@ class TestCommandSensorSensor(unittest.TestCase):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def update_side_effect(self, data):
|
||||
"""Side effect function for mocking CommandSensorData.update()."""
|
||||
self.commandline.data = data
|
||||
|
||||
def test_setup(self):
|
||||
"""Test sensor setup."""
|
||||
config = {'name': 'Test',
|
||||
@@ -46,7 +51,7 @@ class TestCommandSensorSensor(unittest.TestCase):
|
||||
|
||||
entity = command_line.CommandSensor(
|
||||
self.hass, data, 'test', 'in',
|
||||
Template('{{ value | multiply(0.1) }}', self.hass))
|
||||
Template('{{ value | multiply(0.1) }}', self.hass), [])
|
||||
|
||||
entity.update()
|
||||
self.assertEqual(5, float(entity.state))
|
||||
@@ -68,3 +73,105 @@ class TestCommandSensorSensor(unittest.TestCase):
|
||||
data.update()
|
||||
|
||||
self.assertEqual(None, data.value)
|
||||
|
||||
def test_update_with_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key',
|
||||
'key_three'])
|
||||
self.sensor.update()
|
||||
self.assertEqual('some_json_value',
|
||||
self.sensor.device_state_attributes['key'])
|
||||
self.assertEqual('another_json_value',
|
||||
self.sensor.device_state_attributes['another_key'])
|
||||
self.assertEqual('value_three',
|
||||
self.sensor.device_state_attributes['key_three'])
|
||||
|
||||
@patch('homeassistant.components.sensor.command_line._LOGGER')
|
||||
def test_update_with_json_attrs_no_data(self, mock_logger):
|
||||
"""Test attributes when no JSON result fetched."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo ', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
self.assertEqual({}, self.sensor.device_state_attributes)
|
||||
self.assertTrue(mock_logger.warning.called)
|
||||
|
||||
@patch('homeassistant.components.sensor.command_line._LOGGER')
|
||||
def test_update_with_json_attrs_not_dict(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo [1, 2, 3]', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
self.assertEqual({}, self.sensor.device_state_attributes)
|
||||
self.assertTrue(mock_logger.warning.called)
|
||||
|
||||
@patch('homeassistant.components.sensor.command_line._LOGGER')
|
||||
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo This is text rather than JSON data.', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
self.assertEqual({}, self.sensor.device_state_attributes)
|
||||
self.assertTrue(mock_logger.warning.called)
|
||||
|
||||
def test_update_with_missing_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key',
|
||||
'key_three',
|
||||
'special_key'])
|
||||
self.sensor.update()
|
||||
self.assertEqual('some_json_value',
|
||||
self.sensor.device_state_attributes['key'])
|
||||
self.assertEqual('another_json_value',
|
||||
self.sensor.device_state_attributes['another_key'])
|
||||
self.assertEqual('value_three',
|
||||
self.sensor.device_state_attributes['key_three'])
|
||||
self.assertFalse('special_key' in self.sensor.device_state_attributes)
|
||||
|
||||
def test_update_with_unnecessary_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key'])
|
||||
self.sensor.update()
|
||||
self.assertEqual('some_json_value',
|
||||
self.sensor.device_state_attributes['key'])
|
||||
self.assertEqual('another_json_value',
|
||||
self.sensor.device_state_attributes['another_key'])
|
||||
self.assertFalse('key_three' in self.sensor.device_state_attributes)
|
||||
Reference in new issue
Block a user