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() assert {} == self.sensor.device_state_attributes assert mock_logger.warning.called
def test_template(self): """Test command sensor with template.""" data = command_line.CommandSensorData('echo 50') entity = command_line.CommandSensor(self.hass, data, 'test', 'in', '{{ value | multiply(0.1) }}') self.assertEqual(5, float(entity.state))
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() assert {} == self.sensor.device_state_attributes assert mock_logger.warning.called
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_template(self): """Test command sensor with template.""" data = command_line.CommandSensorData(self.hass, 'echo 50', 15) entity = command_line.CommandSensor( self.hass, data, 'test', 'in', Template('{{ value | multiply(0.1) }}', self.hass), []) entity.update() assert 5 == float(entity.state)
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() assert 'some_json_value' == \ self.sensor.device_state_attributes['key'] assert 'another_json_value' == \ self.sensor.device_state_attributes['another_key'] assert not ('key_three' in self.sensor.device_state_attributes)
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)