예제 #1
0
    def test_toggle_service(self):
        """Test the toggling of a 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,
                    }]
                }
            }
        })

        toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)

        toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert not script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)
예제 #2
0
    def test_toggle_service(self):
        """Test the toggling of a 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.toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        script.toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))
예제 #3
0
    def test_toggle_service(self):
        """Test the toggling of a 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}]}
                }
            },
        )

        toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)

        toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert not script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)
예제 #4
0
    def test_toggle_service(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.toggle(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.toggle(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(calls))
예제 #5
0
    def test_toggle_service(self):
        """Test the toggling of a service."""
        event = 'test_event'
        events = []

        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.toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertTrue(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))

        script.toggle(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(events))
예제 #6
0
    def test_toggle_service(self):
        """Test the toggling of a 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.toggle(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.toggle(self.hass, ENTITY_ID)
        self.hass.pool.block_till_done()
        self.assertFalse(script.is_on(self.hass, ENTITY_ID))
        self.assertEqual(0, len(calls))
예제 #7
0
    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,)
예제 #8
0
async def test_turn_on_off_toggle(hass, toggle):
    """Verify turn_on, turn_off & toggle services."""
    event = "test_event"
    event_mock = Mock()

    hass.bus.async_listen(event, event_mock)

    was_on = False

    @callback
    def state_listener(entity_id, old_state, new_state):
        nonlocal was_on
        was_on = True

    async_track_state_change(hass, ENTITY_ID, state_listener, to_state="on")

    if toggle:
        turn_off_step = {"service": "script.toggle", "entity_id": ENTITY_ID}
    else:
        turn_off_step = {"service": "script.turn_off", "entity_id": ENTITY_ID}
    assert await async_setup_component(
        hass,
        "script",
        {
            "script": {
                "test": {
                    "sequence": [{
                        "event": event
                    }, turn_off_step, {
                        "event": event
                    }]
                }
            }
        },
    )

    assert not script.is_on(hass, ENTITY_ID)

    if toggle:
        await hass.services.async_call(DOMAIN, SERVICE_TOGGLE,
                                       {ATTR_ENTITY_ID: ENTITY_ID})
    else:
        await hass.services.async_call(DOMAIN, split_entity_id(ENTITY_ID)[1])
    await hass.async_block_till_done()

    assert not script.is_on(hass, ENTITY_ID)
    assert was_on
    assert event_mock.call_count == 1
예제 #9
0
    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
                        }]
                    }
                }
            },
        )

        turn_on(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)

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

        turn_off(self.hass, ENTITY_ID)
        self.hass.block_till_done()
        assert not script.is_on(self.hass, ENTITY_ID)
        assert 0 == len(events)

        state = self.hass.states.get("group.all_scripts")
        assert state is not None
        assert state.attributes.get("entity_id") == (ENTITY_ID, )
예제 #10
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)

        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))
예제 #11
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))
예제 #12
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))
예제 #13
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)

        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))
예제 #14
0
    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, )
예제 #15
0
    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,)
예제 #16
0
async def test_reload_service(hass, running):
    """Verify the reload service."""
    event = "test_event"
    event_flag = asyncio.Event()

    @callback
    def event_handler(event):
        event_flag.set()

    hass.bus.async_listen_once(event, event_handler)
    hass.states.async_set("test.script", "off")

    assert await async_setup_component(
        hass,
        "script",
        {
            "script": {
                "test": {
                    "sequence": [
                        {"event": event},
                        {"wait_template": "{{ is_state('test.script', 'on') }}"},
                    ]
                }
            }
        },
    )

    assert hass.states.get(ENTITY_ID) is not None
    assert hass.services.has_service(script.DOMAIN, "test")

    if running != "no":
        _, object_id = split_entity_id(ENTITY_ID)
        await hass.services.async_call(DOMAIN, object_id)
        await asyncio.wait_for(event_flag.wait(), 1)

        assert script.is_on(hass, ENTITY_ID)

    object_id = "test" if running == "same" else "test2"
    with patch(
        "homeassistant.config.load_yaml_config_file",
        return_value={"script": {object_id: {"sequence": [{"delay": {"seconds": 5}}]}}},
    ):
        await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
        await hass.async_block_till_done()

    if running != "same":
        state = hass.states.get(ENTITY_ID)
        assert state.attributes["restored"] is True
        assert not hass.services.has_service(script.DOMAIN, "test")

        assert hass.states.get("script.test2") is not None
        assert hass.services.has_service(script.DOMAIN, "test2")

    else:
        assert hass.states.get(ENTITY_ID) is not None
        assert hass.services.has_service(script.DOMAIN, "test")
예제 #17
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))
예제 #18
0
    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))
예제 #19
0
async def test_reload_service(hass, running):
    """Verify the reload service."""
    assert await async_setup_component(
        hass, "script",
        {"script": {
            "test": {
                "sequence": [{
                    "delay": {
                        "seconds": 5
                    }
                }]
            }
        }})

    assert hass.states.get(ENTITY_ID) is not None
    assert hass.services.has_service(script.DOMAIN, "test")

    if running != "no":
        _, object_id = split_entity_id(ENTITY_ID)
        await hass.services.async_call(DOMAIN, object_id)
        await hass.async_block_till_done()

        assert script.is_on(hass, ENTITY_ID)

    object_id = "test" if running == "same" else "test2"
    with patch(
            "homeassistant.config.load_yaml_config_file",
            return_value={
                "script": {
                    object_id: {
                        "sequence": [{
                            "delay": {
                                "seconds": 5
                            }
                        }]
                    }
                }
            },
    ):
        await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
        await hass.async_block_till_done()

    if running != "same":
        assert hass.states.get(ENTITY_ID) is None
        assert not hass.services.has_service(script.DOMAIN, "test")

        assert hass.states.get("script.test2") is not None
        assert hass.services.has_service(script.DOMAIN, "test2")

    else:
        assert hass.states.get(ENTITY_ID) is not None
        assert hass.services.has_service(script.DOMAIN, "test")
예제 #20
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))
예제 #21
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))
예제 #22
0
async def test_concurrent_script(hass, concurrently):
    """Test calling script concurrently or not."""
    if concurrently:
        call_script_2 = {
            "service": "script.turn_on",
            "data": {
                "entity_id": "script.script2"
            },
        }
    else:
        call_script_2 = {"service": "script.script2"}
    assert await async_setup_component(
        hass,
        "script",
        {
            "script": {
                "script1": {
                    "mode":
                    "parallel",
                    "sequence": [
                        call_script_2,
                        {
                            "wait_template":
                            "{{ is_state('input_boolean.test1', 'on') }}"
                        },
                        {
                            "service": "test.script",
                            "data": {
                                "value": "script1"
                            }
                        },
                    ],
                },
                "script2": {
                    "mode":
                    "parallel",
                    "sequence": [
                        {
                            "service": "test.script",
                            "data": {
                                "value": "script2a"
                            }
                        },
                        {
                            "wait_template":
                            "{{ is_state('input_boolean.test2', 'on') }}"
                        },
                        {
                            "service": "test.script",
                            "data": {
                                "value": "script2b"
                            }
                        },
                    ],
                },
            }
        },
    )

    service_called = asyncio.Event()
    service_values = []

    async def async_service_handler(service):
        nonlocal service_values
        service_values.append(service.data.get("value"))
        service_called.set()

    hass.services.async_register("test", "script", async_service_handler)
    hass.states.async_set("input_boolean.test1", "off")
    hass.states.async_set("input_boolean.test2", "off")

    await hass.services.async_call("script", "script1")
    await asyncio.wait_for(service_called.wait(), 1)
    service_called.clear()

    assert service_values[-1] == "script2a"
    assert script.is_on(hass, "script.script1")
    assert script.is_on(hass, "script.script2")

    if not concurrently:
        hass.states.async_set("input_boolean.test2", "on")
        await asyncio.wait_for(service_called.wait(), 1)
        service_called.clear()

        assert service_values[-1] == "script2b"

    hass.states.async_set("input_boolean.test1", "on")
    await asyncio.wait_for(service_called.wait(), 1)
    service_called.clear()

    assert service_values[-1] == "script1"
    assert concurrently == script.is_on(hass, "script.script2")

    if concurrently:
        hass.states.async_set("input_boolean.test2", "on")
        await asyncio.wait_for(service_called.wait(), 1)
        service_called.clear()

        assert service_values[-1] == "script2b"

    await hass.async_block_till_done()

    assert not script.is_on(hass, "script.script1")
    assert not script.is_on(hass, "script.script2")