|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
11 | | -from homeassistant.components import automation, input_boolean, script |
| 11 | +from homeassistant.components import automation, input_boolean, labs, script |
12 | 12 | from homeassistant.components.automation import ( |
13 | 13 | ATTR_SOURCE, |
14 | 14 | DOMAIN, |
@@ -3493,3 +3493,92 @@ async def test_valid_configuration( |
3493 | 3493 | }, |
3494 | 3494 | ) |
3495 | 3495 | await hass.async_block_till_done() |
| 3496 | + |
| 3497 | + |
| 3498 | +async def test_reload_when_labs_flag_changes( |
| 3499 | + hass: HomeAssistant, |
| 3500 | + hass_ws_client: WebSocketGenerator, |
| 3501 | + calls: list[ServiceCall], |
| 3502 | + hass_admin_user: MockUser, |
| 3503 | + hass_read_only_user: MockUser, |
| 3504 | +) -> None: |
| 3505 | + """Test automations are reloaded when labs flag changes.""" |
| 3506 | + ws_client = await hass_ws_client(hass) |
| 3507 | + |
| 3508 | + assert await async_setup_component( |
| 3509 | + hass, |
| 3510 | + automation.DOMAIN, |
| 3511 | + { |
| 3512 | + automation.DOMAIN: { |
| 3513 | + "alias": "hello", |
| 3514 | + "trigger": {"platform": "event", "event_type": "test_event"}, |
| 3515 | + "action": { |
| 3516 | + "action": "test.automation", |
| 3517 | + "data_template": {"event": "{{ trigger.event.event_type }}"}, |
| 3518 | + }, |
| 3519 | + } |
| 3520 | + }, |
| 3521 | + ) |
| 3522 | + assert await async_setup_component(hass, labs.DOMAIN, {}) |
| 3523 | + assert hass.states.get("automation.hello") is not None |
| 3524 | + assert hass.states.get("automation.bye") is None |
| 3525 | + listeners = hass.bus.async_listeners() |
| 3526 | + assert listeners.get("test_event") == 1 |
| 3527 | + assert listeners.get("test_event2") is None |
| 3528 | + |
| 3529 | + hass.bus.async_fire("test_event") |
| 3530 | + await hass.async_block_till_done() |
| 3531 | + |
| 3532 | + assert len(calls) == 1 |
| 3533 | + assert calls[0].data.get("event") == "test_event" |
| 3534 | + |
| 3535 | + test_reload_event = async_capture_events(hass, EVENT_AUTOMATION_RELOADED) |
| 3536 | + |
| 3537 | + # Check we reload whenever the labs flag is set, even if it's already enabled |
| 3538 | + for enabled in (True, True, False, False): |
| 3539 | + test_reload_event.clear() |
| 3540 | + calls.clear() |
| 3541 | + |
| 3542 | + with patch( |
| 3543 | + "homeassistant.config.load_yaml_config_file", |
| 3544 | + autospec=True, |
| 3545 | + return_value={ |
| 3546 | + automation.DOMAIN: { |
| 3547 | + "alias": "bye", |
| 3548 | + "trigger": {"platform": "event", "event_type": "test_event2"}, |
| 3549 | + "action": { |
| 3550 | + "action": "test.automation", |
| 3551 | + "data_template": {"event": "{{ trigger.event.event_type }}"}, |
| 3552 | + }, |
| 3553 | + } |
| 3554 | + }, |
| 3555 | + ): |
| 3556 | + await ws_client.send_json_auto_id( |
| 3557 | + { |
| 3558 | + "type": "labs/update", |
| 3559 | + "domain": "automation", |
| 3560 | + "preview_feature": "new_triggers_conditions", |
| 3561 | + "enabled": enabled, |
| 3562 | + } |
| 3563 | + ) |
| 3564 | + |
| 3565 | + msg = await ws_client.receive_json() |
| 3566 | + assert msg["success"] |
| 3567 | + await hass.async_block_till_done() |
| 3568 | + |
| 3569 | + assert len(test_reload_event) == 1 |
| 3570 | + |
| 3571 | + assert hass.states.get("automation.hello") is None |
| 3572 | + assert hass.states.get("automation.bye") is not None |
| 3573 | + listeners = hass.bus.async_listeners() |
| 3574 | + assert listeners.get("test_event") is None |
| 3575 | + assert listeners.get("test_event2") == 1 |
| 3576 | + |
| 3577 | + hass.bus.async_fire("test_event") |
| 3578 | + await hass.async_block_till_done() |
| 3579 | + assert len(calls) == 0 |
| 3580 | + |
| 3581 | + hass.bus.async_fire("test_event2") |
| 3582 | + await hass.async_block_till_done() |
| 3583 | + assert len(calls) == 1 |
| 3584 | + assert calls[-1].data.get("event") == "test_event2" |
0 commit comments