コード例 #1
0
async def test_media_player_play_error(
    hass: HomeAssistant,
    ufp: MockUFPFixture,
    doorbell: Camera,
    unadopted_camera: Camera,
):
    """Test media_player entity test play_media, not music."""

    await init_entry(hass, ufp, [doorbell, unadopted_camera])
    assert_entity_counts(hass, Platform.MEDIA_PLAYER, 1, 1)

    doorbell.__fields__["play_audio"] = Mock()
    doorbell.__fields__["wait_until_audio_completes"] = Mock()
    doorbell.play_audio = AsyncMock(side_effect=StreamError)
    doorbell.wait_until_audio_completes = AsyncMock()

    with pytest.raises(HomeAssistantError):
        await hass.services.async_call(
            "media_player",
            "play_media",
            {
                ATTR_ENTITY_ID: "media_player.test_camera_speaker",
                "media_content_id": "/test.mp3",
                "media_content_type": "music",
            },
            blocking=True,
        )

    assert doorbell.play_audio.called
    assert not doorbell.wait_until_audio_completes.called
コード例 #2
0
async def test_media_player_play(
    hass: HomeAssistant,
    ufp: MockUFPFixture,
    doorbell: Camera,
    unadopted_camera: Camera,
):
    """Test media_player entity test play_media."""

    await init_entry(hass, ufp, [doorbell, unadopted_camera])
    assert_entity_counts(hass, Platform.MEDIA_PLAYER, 1, 1)

    doorbell.__fields__["stop_audio"] = Mock()
    doorbell.__fields__["play_audio"] = Mock()
    doorbell.__fields__["wait_until_audio_completes"] = Mock()
    doorbell.stop_audio = AsyncMock()
    doorbell.play_audio = AsyncMock()
    doorbell.wait_until_audio_completes = AsyncMock()

    await hass.services.async_call(
        "media_player",
        "play_media",
        {
            ATTR_ENTITY_ID: "media_player.test_camera_speaker",
            "media_content_id": "http://example.com/test.mp3",
            "media_content_type": "music",
        },
        blocking=True,
    )

    doorbell.play_audio.assert_called_once_with("http://example.com/test.mp3",
                                                blocking=False)
    doorbell.wait_until_audio_completes.assert_called_once()