|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from homeassistant.components import script |
| 10 | +from homeassistant.components import labs, script |
11 | 11 | from homeassistant.components.script import DOMAIN, EVENT_SCRIPT_STARTED, ScriptEntity |
12 | 12 | from homeassistant.config_entries import ConfigEntryState |
13 | 13 | from homeassistant.const import ( |
@@ -1868,3 +1868,64 @@ async def async_service_handler(*args, **kwargs) -> None: |
1868 | 1868 |
|
1869 | 1869 | await hass.services.async_call("script", "test_main", blocking=True) |
1870 | 1870 | assert calls == 4 |
| 1871 | + |
| 1872 | + |
| 1873 | +async def test_reload_when_labs_flag_changes( |
| 1874 | + hass: HomeAssistant, |
| 1875 | + hass_ws_client: WebSocketGenerator, |
| 1876 | +) -> None: |
| 1877 | + """Test scripts are reloaded when labs flag changes.""" |
| 1878 | + event = "test_event" |
| 1879 | + hass.states.async_set("test.script", "off") |
| 1880 | + |
| 1881 | + ws_client = await hass_ws_client(hass) |
| 1882 | + |
| 1883 | + assert await async_setup_component( |
| 1884 | + hass, |
| 1885 | + "script", |
| 1886 | + { |
| 1887 | + "script": { |
| 1888 | + "test": { |
| 1889 | + "sequence": [ |
| 1890 | + {"event": event}, |
| 1891 | + {"wait_template": "{{ is_state('test.script', 'on') }}"}, |
| 1892 | + ] |
| 1893 | + } |
| 1894 | + } |
| 1895 | + }, |
| 1896 | + ) |
| 1897 | + assert await async_setup_component(hass, labs.DOMAIN, {}) |
| 1898 | + |
| 1899 | + assert hass.states.get(ENTITY_ID) is not None |
| 1900 | + assert hass.services.has_service(script.DOMAIN, "test") |
| 1901 | + |
| 1902 | + for enabled, active_object_id, inactive_object_ids in ( |
| 1903 | + (False, "test2", ("test",)), |
| 1904 | + (True, "test3", ("test", "test2")), |
| 1905 | + ): |
| 1906 | + with patch( |
| 1907 | + "homeassistant.config.load_yaml_config_file", |
| 1908 | + return_value={ |
| 1909 | + "script": {active_object_id: {"sequence": [{"delay": {"seconds": 5}}]}} |
| 1910 | + }, |
| 1911 | + ): |
| 1912 | + await ws_client.send_json_auto_id( |
| 1913 | + { |
| 1914 | + "type": "labs/update", |
| 1915 | + "domain": "automation", |
| 1916 | + "preview_feature": "new_triggers_conditions", |
| 1917 | + "enabled": enabled, |
| 1918 | + } |
| 1919 | + ) |
| 1920 | + |
| 1921 | + msg = await ws_client.receive_json() |
| 1922 | + assert msg["success"] |
| 1923 | + await hass.async_block_till_done() |
| 1924 | + |
| 1925 | + for inactive_object_id in inactive_object_ids: |
| 1926 | + state = hass.states.get(f"script.{inactive_object_id}") |
| 1927 | + assert state.attributes["restored"] is True |
| 1928 | + assert not hass.services.has_service(script.DOMAIN, inactive_object_id) |
| 1929 | + |
| 1930 | + assert hass.states.get(f"script.{active_object_id}") is not None |
| 1931 | + assert hass.services.has_service(script.DOMAIN, active_object_id) |
0 commit comments