def test_calling_service_template(self): """Test the calling of a service.""" calls = [] @callback def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) script.call_from_config(self.hass, { 'service_template': """ {% if True %} test.script {% else %} test.not_script {% endif %}""", 'data_template': { 'hello': """ {% if True %} world {% else %} Not world {% endif %} """ } }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data.get('hello') == 'world'
def test_calling_service_template(self): """Test the calling of a service.""" calls = [] @callback def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) script.call_from_config( self.hass, { 'service_template': """ {% if True %} test.script {% else %} test.not_script {% endif %}""", 'data_template': { 'hello': """ {% if is_world == 'yes' %} world {% else %} not world {% endif %} """ } }, {'is_world': 'yes'}) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data.get('hello') == 'world'
def test_calling_notify_from_script_loaded_from_yaml_with_title(self): """Test if we can call a notify from a script.""" self._setup_notify() conf = { "service": "notify.notify", "data": { "data": { "push": { "sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav" } } }, "data_template": { "message": "Test 123 {{ 2 + 2 }}\n", "title": "Test" }, } script.call_from_config(self.hass, conf) self.hass.block_till_done() assert len(self.events) == 1 assert { "message": "Test 123 4", "title": "Test", "data": { "push": { "sound": "US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav" } }, } == self.events[0].data
def test_calling_notify_from_script_loaded_from_yaml_with_title(self): """Test if we can call a notify from a script.""" self._setup_notify() conf = { 'service': 'notify.notify', 'data': { 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav' } } }, 'data_template': { 'message': 'Test 123 {{ 2 + 2 }}\n', 'title': 'Test' } } script.call_from_config(self.hass, conf) self.hass.block_till_done() assert len(self.events) == 1 assert { 'message': 'Test 123 4', 'title': 'Test', 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav' } } } == self.events[0].data
def test_calling_notify_from_script_loaded_from_yaml(self): """Test if we can call a notify from a script.""" yaml_conf = """ service: notify.notify data: data: push: sound: US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav data_template: message: > Test 123 {{ 2 + 2 }} """ with tempfile.NamedTemporaryFile() as fp: fp.write(yaml_conf.encode('utf-8')) fp.flush() conf = yaml.load_yaml(fp.name) script.call_from_config(self.hass, conf) self.hass.pool.block_till_done() self.assertTrue(len(self.events) == 1) assert { 'message': 'Test 123 4', 'target': None, 'title': 'Home Assistant', 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav' } } } == self.events[0].data
def test_calling_notify_from_script_loaded_from_yaml_with_title(self): """Test if we can call a notify from a script.""" self._setup_notify() conf = { 'service': 'notify.notify', 'data': { 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav' } } }, 'data_template': { 'message': 'Test 123 {{ 2 + 2 }}\n', 'title': 'Test' } } script.call_from_config(self.hass, conf) self.hass.block_till_done() self.assertTrue(len(self.events) == 1) assert { 'message': 'Test 123 4', 'title': 'Test', 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'}} } == self.events[0].data
def test_calling_notify_from_script_loaded_from_yaml_with_title(self): """Test if we can call a notify from a script.""" yaml_conf = """ service: notify.notify data: data: push: sound: US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav data_template: title: Test message: > Test 123 {{ 2 + 2 }} """ with tempfile.NamedTemporaryFile() as fp: fp.write(yaml_conf.encode('utf-8')) fp.flush() conf = yaml.load_yaml(fp.name) script.call_from_config(self.hass, conf) self.hass.pool.block_till_done() self.assertTrue(len(self.events) == 1) assert { 'message': 'Test 123 4', 'title': 'Test', 'data': { 'push': { 'sound': 'US-EN-Morgan-Freeman-Roommate-Is-Arriving.wav'}} } == self.events[0].data
def test_calling_service(self): """Test the calling of a service.""" calls = [] context = Context() @callback def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) script.call_from_config(self.hass, { 'service': 'test.script', 'data': { 'hello': 'world' } }, context=context) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].context is context assert calls[0].data.get('hello') == 'world'
def test_calling_service(self): """Test the calling of a service.""" calls = [] def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) script.call_from_config(self.hass, { 'service': 'test.script', 'data': { 'hello': 'world' } }) self.hass.block_till_done() assert len(calls) == 1 assert calls[0].data.get('hello') == 'world'