Exemple #1
0
    def test_firing_event(self):
        event = 'test_event'
        calls = []

        def record_event(event):
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(script.setup(self.hass, {
            'script': {
                'test': {
                    'alias': 'Test Script',
                    'sequence': [{
                        'event': event,
                        'event_data': {
                            'hello': 'world'
                        }
                    }]
                }
            }
        }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
Exemple #2
0
    def test_firing_event(self):
        """Test the firing of events."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'alias': 'Test Script',
                    'sequence': [{
                        'event': event,
                        'event_data': {
                            'hello': 'world'
                        }
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
        self.assertIsNone(
            self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
    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)

        self.assertTrue(
            script.setup(
                self.hass, {
                    'script': {
                        'test': {
                            'sequence': [{
                                'service': 'test.script',
                                'data': {
                                    'hello': 'world'
                                }
                            }]
                        }
                    }
                }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
Exemple #4
0
    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)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'service': 'test.script',
                        'data': {
                            'hello': 'world'
                        }
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
    def test_passing_variables(self):
        """Test different ways of passing in variables."""
        calls = []

        @callback
        def record_call(service):
            """Add recorded event to set."""
            calls.append(service)

        self.hass.services.register("test", "script", record_call)

        assert setup_component(
            self.hass,
            "script",
            {
                "script": {
                    "test": {"sequence": {"service": "test.script", "data_template": {"hello": "{{ greeting }}"}}}
                }
            },
        )

        script.turn_on(self.hass, ENTITY_ID, {"greeting": "world"})

        self.hass.block_till_done()

        assert len(calls) == 1
        assert calls[-1].data["hello"] == "world"

        self.hass.services.call("script", "test", {"greeting": "universe"})

        self.hass.block_till_done()

        assert len(calls) == 2
        assert calls[-1].data["hello"] == "universe"
    def test_firing_event(self):
        event = 'test_event'
        calls = []

        def record_event(event):
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(script.setup(self.hass, {
            'script': {
                'test': {
                    'alias': 'Test Script',
                    'sequence': [{
                        'event': event,
                        'event_data': {
                            'hello': 'world'
                        }
                    }]
                }
            }
        }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
        self.assertEqual(
            True, self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
Exemple #7
0
    def test_calling_service_old(self):
        calls = []

        def record_call(service):
            calls.append(service)

        self.hass.services.register('test', 'script', record_call)

        self.assertTrue(script.setup(self.hass, {
            'script': {
                'test': {
                    'sequence': [{
                        'execute_service': 'test.script',
                        'service_data': {
                            'hello': 'world'
                        }
                    }]
                }
            }
        }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
Exemple #8
0
    def test_turn_on_service(self):
        """Verify that the turn_on service."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'delay': {
                            'seconds': 5
                        }
                    }, {
                        'event': event,
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(calls))

        # Calling turn_on a second time should not advance the script
        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertEqual(0, len(calls))
    def test_turn_on_service(self):
        """Verify that the turn_on service."""
        event = "test_event"
        events = []

        @callback
        def record_event(event):
            """Add recorded event to set."""
            events.append(event)

        self.hass.bus.listen(event, record_event)

        assert setup_component(
            self.hass, "script", {"script": {"test": {"sequence": [{"delay": {"seconds": 5}}, {"event": event}]}}}
        )

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        # Calling turn_on a second time should not advance the script
        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertEqual(0, len(events))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        state = self.hass.states.get("group.all_scripts")
        assert state is not None
        assert state.attributes.get("entity_id") == (ENTITY_ID,)
    def test_firing_event(self):
        """Test the firing of events."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(
            script.setup(
                self.hass, {
                    'script': {
                        'test': {
                            'alias':
                            'Test Script',
                            'sequence': [{
                                'event': event,
                                'event_data': {
                                    'hello': 'world'
                                }
                            }]
                        }
                    }
                }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
        self.assertIsNone(
            self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))
    def test_delay(self):
        """Test the delay."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(
            script.setup(
                self.hass, {
                    'script': {
                        'test': {
                            'sequence': [{
                                'event': event
                            }, {
                                'delay': {
                                    'seconds': 5
                                }
                            }, {
                                'event': event,
                            }]
                        }
                    }
                }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertTrue(
            self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))

        self.assertEqual(
            event,
            self.hass.states.get(ENTITY_ID).attributes.get('last_action'))
        self.assertEqual(1, len(calls))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(2, len(calls))
    def test_passing_variables(self):
        """Test different ways of passing in variables."""
        calls = []
        context = Context()

        @callback
        def record_call(service):
            """Add recorded event to set."""
            calls.append(service)

        self.hass.services.register('test', 'script', record_call)

        assert setup_component(
            self.hass, 'script', {
                'script': {
                    'test': {
                        'sequence': {
                            'service': 'test.script',
                            'data_template': {
                                'hello': '{{ greeting }}',
                            },
                        },
                    },
                },
            })

        script.turn_on(self.hass,
                       ENTITY_ID, {'greeting': 'world'},
                       context=context)

        self.hass.block_till_done()

        assert len(calls) == 1
        assert calls[0].context is context
        assert calls[0].data['hello'] == 'world'

        self.hass.services.call('script',
                                'test', {
                                    'greeting': 'universe',
                                },
                                context=context)

        self.hass.block_till_done()

        assert len(calls) == 2
        assert calls[1].context is context
        assert calls[1].data['hello'] == 'universe'
Exemple #13
0
    def test_delay(self):
        """Test the delay."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'event': event
                    }, {
                        'delay': {
                            'seconds': 5
                        }
                    }, {
                        'event': event,
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertTrue(
            self.hass.states.get(ENTITY_ID).attributes.get('can_cancel'))

        self.assertEqual(
            event,
            self.hass.states.get(ENTITY_ID).attributes.get('last_action'))
        self.assertEqual(1, len(calls))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(2, len(calls))
    def test_turn_on_service(self):
        """Verify that the turn_on service."""
        event = 'test_event'
        events = []

        @callback
        def record_event(event):
            """Add recorded event to set."""
            events.append(event)

        self.hass.bus.listen(event, record_event)

        assert setup_component(
            self.hass, 'script', {
                'script': {
                    'test': {
                        'sequence': [{
                            'delay': {
                                'seconds': 5
                            }
                        }, {
                            'event': event,
                        }]
                    }
                }
            })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        # Calling turn_on a second time should not advance the script
        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertEqual(0, len(events))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        state = self.hass.states.get('group.all_scripts')
        assert state is not None
        assert state.attributes.get('entity_id') == (ENTITY_ID, )
    def test_cancel_while_delay(self):
        """Test the cancelling while the delay is present."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(
            script.setup(
                self.hass, {
                    'script': {
                        'test': {
                            'sequence': [{
                                'delay': {
                                    'seconds': 5
                                }
                            }, {
                                'event': event,
                            }]
                        }
                    }
                }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))
    def test_turn_on_service(self):
        """Verify that the turn_on service."""
        event = 'test_event'
        events = []

        @callback
        def record_event(event):
            """Add recorded event to set."""
            events.append(event)

        self.hass.bus.listen(event, record_event)

        assert setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'delay': {
                            'seconds': 5
                        }
                    }, {
                        'event': event,
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        # Calling turn_on a second time should not advance the script
        script.turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertEqual(0, len(events))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        state = self.hass.states.get('group.all_scripts')
        assert state is not None
        assert state.attributes.get('entity_id') == (ENTITY_ID,)
    def test_passing_variables(self):
        """Test different ways of passing in variables."""
        calls = []
        context = Context()

        @callback
        def record_call(service):
            """Add recorded event to set."""
            calls.append(service)

        self.hass.services.register('test', 'script', record_call)

        assert setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': {
                        'service': 'test.script',
                        'data_template': {
                            'hello': '{{ greeting }}',
                        },
                    },
                },
            },
        })

        script.turn_on(self.hass, ENTITY_ID, {
            'greeting': 'world'
        }, context=context)

        self.hass.block_till_done()

        assert len(calls) == 1
        assert calls[0].context is context
        assert calls[0].data['hello'] == 'world'

        self.hass.services.call('script', 'test', {
            'greeting': 'universe',
        }, context=context)

        self.hass.block_till_done()

        assert len(calls) == 2
        assert calls[1].context is context
        assert calls[1].data['hello'] == 'universe'
Exemple #18
0
    def test_cancel_while_delay(self):
        """Test the cancelling while the delay is present."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'delay': {
                            'seconds': 5
                        }
                    }, {
                        'event': event,
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))
    def test_cancel_while_delay(self):
        event = 'test_event'
        calls = []

        def record_event(event):
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(script.setup(self.hass, {
            'script': {
                'test': {
                    'sequence': [{
                        'delay': {
                            'seconds': 5
                        }
                    }, {
                        'event': event,
                    }]
                }
            }
        }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))

        script.turn_off(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))

        self.assertEqual(0, len(calls))
Exemple #20
0
    def test_calling_service_template(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)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'service_template': """
                            {% if True %}
                                test.script
                            {% else %}
                                test.not_script
                            {% endif %}""",
                        'data_template': {
                            'hello': """
                                {% if True %}
                                    world
                                {% else %}
                                    Not world
                                {% endif %}
                            """
                        }
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertEqual(1, len(calls))
        self.assertEqual('world', calls[0].data.get('hello'))
Exemple #21
0
    def test_alt_delay(self):
        """Test alternative delay config format."""
        event = 'test_event'
        calls = []

        def record_event(event):
            """Add recorded event to set."""
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        assert _setup_component(self.hass, 'script', {
            'script': {
                'test': {
                    'sequence': [{
                        'event': event,
                    }, {
                        'delay': None,
                        'seconds': 5
                    }, {
                        'event': event,
                    }]
                }
            }
        })

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()

        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(1, len(calls))

        future = dt_util.utcnow() + timedelta(seconds=5)
        fire_time_changed(self.hass, future)
        self.hass.pool.block_till_done()

        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(2, len(calls))
Exemple #22
0
    def test_turn_on_service(self):
        """
        Verifies that the turn_on service for a script only turns on scripts
        that are not currently running.
        """
        event = 'test_event'
        calls = []

        def record_event(event):
            calls.append(event)

        self.hass.bus.listen(event, record_event)

        self.assertTrue(
            script.setup(
                self.hass, {
                    'script': {
                        'test': {
                            'sequence': [{
                                'delay': {
                                    'seconds': 5
                                }
                            }, {
                                'event': event,
                            }]
                        }
                    }
                }))

        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(calls))

        # calling turn_on a second time should not advance the script
        script.turn_on(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertEqual(0, len(calls))