コード例 #1
0
async def test_if_fires_on_opp_shutdown(opp):
    """Test the firing when Open Peer Power shuts down."""
    calls = async_mock_service(opp, "test", "automation")
    opp.state = CoreState.not_running

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {
                    "platform": "openpeerpower",
                    "event": "shutdown"
                },
                "action": {
                    "service": "test.automation"
                },
            }
        },
    )
    assert automation.is_on(opp, "automation.hello")
    assert len(calls) == 0

    await opp.async_start()
    assert automation.is_on(opp, "automation.hello")
    assert len(calls) == 0

    with patch.object(opp.loop, "stop"):
        await opp.async_stop()
    assert len(calls) == 1
コード例 #2
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_automation_not_trigger_on_bootstrap(opp):
    """Test if automation is not trigger on bootstrap."""
    opp.state = CoreState.not_running
    calls = async_mock_service(opp, "test", "automation")

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {"service": "test.automation", "entity_id": "hello.world"},
            }
        },
    )
    assert automation.is_on(opp, "automation.hello")

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 0

    opp.bus.async_fire(EVENT_OPENPEERPOWER_STARTED)
    await opp.async_block_till_done()
    assert automation.is_on(opp, "automation.hello")

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()

    assert len(calls) == 1
    assert ["hello.world"] == calls[0].data.get(ATTR_ENTITY_ID)
コード例 #3
0
async def test_if_fires_on_opp_start(opp):
    """Test the firing when Open Peer Power starts."""
    calls = async_mock_service(opp, "test", "automation")
    opp.state = CoreState.not_running
    config = {
        automation.DOMAIN: {
            "alias": "hello",
            "trigger": {
                "platform": "openpeerpower",
                "event": "start"
            },
            "action": {
                "service": "test.automation"
            },
        }
    }

    assert await async_setup_component(opp, automation.DOMAIN, config)
    assert automation.is_on(opp, "automation.hello")
    assert len(calls) == 0

    await opp.async_start()
    assert automation.is_on(opp, "automation.hello")
    assert len(calls) == 1

    with patch(
            "openpeerpower.config.async_opp_config_yaml",
            Mock(return_value=mock_coro(config)),
    ):
        await opp.services.async_call(automation.DOMAIN,
                                      automation.SERVICE_RELOAD,
                                      blocking=True)

    assert automation.is_on(opp, "automation.hello")
    assert len(calls) == 1
コード例 #4
0
async def test_services(opp, calls):
    """Test the automation services for turning entities on/off."""
    entity_id = "automation.hello"

    assert opp.states.get(entity_id) is None
    assert not automation.is_on(opp, entity_id)

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {"service": "test.automation"},
            }
        },
    )

    assert opp.states.get(entity_id) is not None
    assert automation.is_on(opp, entity_id)

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1

    await common.async_turn_off(opp, entity_id)
    await opp.async_block_till_done()

    assert not automation.is_on(opp, entity_id)
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1

    await common.async_toggle(opp, entity_id)
    await opp.async_block_till_done()

    assert automation.is_on(opp, entity_id)
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 2

    await common.async_trigger(opp, entity_id)
    await opp.async_block_till_done()
    assert len(calls) == 3

    await common.async_turn_off(opp, entity_id)
    await opp.async_block_till_done()
    await common.async_trigger(opp, entity_id)
    await opp.async_block_till_done()
    assert len(calls) == 4

    await common.async_turn_on(opp, entity_id)
    await opp.async_block_till_done()
    assert automation.is_on(opp, entity_id)
コード例 #5
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_automation_restore_state(opp):
    """Ensure states are restored on startup."""
    time = dt_util.utcnow()

    mock_restore_cache(
        opp,
        (
            State("automation.hello", STATE_ON),
            State("automation.bye", STATE_OFF, {"last_triggered": time}),
        ),
    )

    config = {
        automation.DOMAIN: [
            {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event_hello"},
                "action": {"service": "test.automation"},
            },
            {
                "alias": "bye",
                "trigger": {"platform": "event", "event_type": "test_event_bye"},
                "action": {"service": "test.automation"},
            },
        ]
    }

    assert await async_setup_component(opp, automation.DOMAIN, config)

    state = opp.states.get("automation.hello")
    assert state
    assert state.state == STATE_ON
    assert state.attributes["last_triggered"] is None

    state = opp.states.get("automation.bye")
    assert state
    assert state.state == STATE_OFF
    assert state.attributes["last_triggered"] == time

    calls = async_mock_service(opp, "test", "automation")

    assert automation.is_on(opp, "automation.bye") is False

    opp.bus.async_fire("test_event_bye")
    await opp.async_block_till_done()
    assert len(calls) == 0

    assert automation.is_on(opp, "automation.hello")

    opp.bus.async_fire("test_event_hello")
    await opp.async_block_till_done()

    assert len(calls) == 1
コード例 #6
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_initial_value_on(opp):
    """Test initial value on."""
    opp.state = CoreState.not_running
    calls = async_mock_service(opp, "test", "automation")

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "initial_state": "on",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {
                    "service": "test.automation",
                    "entity_id": ["hello.world", "hello.world2"],
                },
            }
        },
    )
    assert automation.is_on(opp, "automation.hello")

    await opp.async_start()
    await opp.async_block_till_done()
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1
コード例 #7
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_automation_is_on_if_no_initial_state_or_restore(opp):
    """Test initial value is on when no initial state or restored state."""
    calls = async_mock_service(opp, "test", "automation")

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {"service": "test.automation", "entity_id": "hello.world"},
            }
        },
    )
    assert automation.is_on(opp, "automation.hello")

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1
コード例 #8
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_no_initial_value_and_restore_off(opp):
    """Test initial value off and restored state is turned on."""
    calls = async_mock_service(opp, "test", "automation")
    mock_restore_cache(opp, (State("automation.hello", STATE_OFF),))

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {"service": "test.automation", "entity_id": "hello.world"},
            }
        },
    )
    assert not automation.is_on(opp, "automation.hello")

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 0
コード例 #9
0
ファイル: test_init.py プロジェクト: OpenPeerPower/core
async def test_services(opp, calls):
    """Test the automation services for turning entities on/off."""
    entity_id = "automation.hello"

    assert opp.states.get(entity_id) is None
    assert not automation.is_on(opp, entity_id)

    assert await async_setup_component(
        opp,
        automation.DOMAIN,
        {
            automation.DOMAIN: {
                "alias": "hello",
                "trigger": {"platform": "event", "event_type": "test_event"},
                "action": {"service": "test.automation"},
            }
        },
    )

    assert opp.states.get(entity_id) is not None
    assert automation.is_on(opp, entity_id)

    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1

    await opp.services.async_call(
        automation.DOMAIN,
        SERVICE_TURN_OFF,
        {
            ATTR_ENTITY_ID: entity_id,
        },
        blocking=True,
    )

    assert not automation.is_on(opp, entity_id)
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 1

    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )

    assert automation.is_on(opp, entity_id)
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 2

    await opp.services.async_call(
        automation.DOMAIN,
        SERVICE_TOGGLE,
        {ATTR_ENTITY_ID: entity_id},
        blocking=True,
    )
    assert not automation.is_on(opp, entity_id)
    opp.bus.async_fire("test_event")
    await opp.async_block_till_done()
    assert len(calls) == 2

    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )
    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TRIGGER, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )
    assert len(calls) == 3

    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )
    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TRIGGER, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )
    assert len(calls) == 4

    await opp.services.async_call(
        automation.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True
    )
    assert automation.is_on(opp, entity_id)