def _call_service(self, action, variables): """Call the service specified in the action.""" self.last_action = action.get(CONF_ALIAS, 'call service') self._log("Executing step %s" % self.last_action) service.call_from_config(self.hass, action, True, variables, validate_config=False)
def test_split_entity_string(self): """Test splitting of entity string.""" service.call_from_config(self.hass, { 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer' }) self.hass.pool.block_till_done() self.assertEqual(['hello.world', 'sensor.beer'], self.calls[-1].data.get('entity_id'))
def test_split_entity_string(self): """Test splitting of entity string.""" service.call_from_config( self.hass, { 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer' }) self.hass.pool.block_till_done() self.assertEqual(['hello.world', 'sensor.beer'], self.calls[-1].data.get('entity_id'))
def _call_service(self, service_name, service_data=None, allow_override=False): """Call either a specified or active child's service.""" if allow_override and service_name in self._cmds: call_from_config( self.hass, self._cmds[service_name], blocking=True) return if service_data is None: service_data = {} active_child = self._child_state service_data[ATTR_ENTITY_ID] = active_child.entity_id self.hass.services.call(DOMAIN, service_name, service_data, blocking=True)
def test_template_service_call(self): """Test service call with tempating.""" config = { 'service_template': '{{ \'test_domain.test_service\' }}', 'entity_id': 'hello.world', 'data_template': { 'hello': '{{ \'goodbye\' }}', }, } runs = [] decor = service.service('test_domain', 'test_service') decor(lambda x, y: runs.append(y)) service.call_from_config(self.hass, config) self.hass.pool.block_till_done() self.assertEqual('goodbye', runs[0].data['hello'])
def test_not_mutate_input(self): """Test for immutable input.""" orig = { 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer', 'data': { 'hello': 1, }, } service.call_from_config(self.hass, orig) self.hass.pool.block_till_done() self.assertEqual({ 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer', 'data': { 'hello': 1, }, }, orig)
def test_not_mutate_input(self): """Test for immutable input.""" orig = { 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer', 'data': { 'hello': 1, }, } service.call_from_config(self.hass, orig) self.hass.pool.block_till_done() self.assertEqual( { 'service': 'test_domain.test_service', 'entity_id': 'hello.world, sensor.beer', 'data': { 'hello': 1, }, }, orig)
def _call_service(self, service_name, service_data=None, allow_override=False): """Call either a specified or active child's service.""" if allow_override and service_name in self._cmds: call_from_config(self.hass, self._cmds[service_name], blocking=True) return if service_data is None: service_data = {} active_child = self._child_state service_data[ATTR_ENTITY_ID] = active_child.entity_id self.hass.services.call(DOMAIN, service_name, service_data, blocking=True)
def test_passing_variables_to_templates(self): """Test passing variables to templates.""" config = { 'service_template': '{{ var_service }}', 'entity_id': 'hello.world', 'data_template': { 'hello': '{{ var_data }}', }, } runs = [] decor = service.service('test_domain', 'test_service') decor(lambda x, y: runs.append(y)) service.call_from_config(self.hass, config, variables={ 'var_service': 'test_domain.test_service', 'var_data': 'goodbye', }) self.hass.pool.block_till_done() self.assertEqual('goodbye', runs[0].data['hello'])
def test_fail_silently_if_no_service(self, mock_log): """Test failling if service is missing.""" service.call_from_config(self.hass, None) self.assertEqual(1, mock_log.call_count) service.call_from_config(self.hass, {}) self.assertEqual(2, mock_log.call_count) service.call_from_config(self.hass, {'service': 'invalid'}) self.assertEqual(3, mock_log.call_count)
def test_fail_silently_if_no_service(self, mock_log): """Test failling if service is missing.""" service.call_from_config(self.hass, None) self.assertEqual(1, mock_log.call_count) service.call_from_config(self.hass, {}) self.assertEqual(2, mock_log.call_count) service.call_from_config(self.hass, { 'service': 'invalid' }) self.assertEqual(3, mock_log.call_count)