Exemple #1
0
async def test_camera_invalid_image(hass: HomeAssistant) -> None:
    """Test retrieving a single invalid camera image."""
    client = create_mock_client()
    client.async_send_image_stream_start = AsyncMock(return_value=True)
    client.async_send_image_stream_stop = AsyncMock(return_value=True)

    await setup_test_config_entry(hass, hyperion_client=client)

    get_image_coro = async_get_image(hass, TEST_CAMERA_ENTITY_ID, timeout=0)
    image_stream_update_coro = async_call_registered_callback(
        client, "ledcolors-imagestream-update", None
    )
    with pytest.raises(HomeAssistantError):
        await asyncio.gather(get_image_coro, image_stream_update_coro)

    get_image_coro = async_get_image(hass, TEST_CAMERA_ENTITY_ID, timeout=0)
    image_stream_update_coro = async_call_registered_callback(
        client, "ledcolors-imagestream-update", {"garbage": 1}
    )
    with pytest.raises(HomeAssistantError):
        await asyncio.gather(get_image_coro, image_stream_update_coro)

    get_image_coro = async_get_image(hass, TEST_CAMERA_ENTITY_ID, timeout=0)
    image_stream_update_coro = async_call_registered_callback(
        client,
        "ledcolors-imagestream-update",
        {"result": {"image": "data:image/jpg;base64,FOO"}},
    )
    with pytest.raises(HomeAssistantError):
        await asyncio.gather(get_image_coro, image_stream_update_coro)
 def test_get_image_without_exists_camera(self):
     """Try to get image without exists camera."""
     with patch('homeassistant.helpers.entity_component.EntityComponent.'
                'get_entity', return_value=None), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(camera.async_get_image(
             self.hass, 'camera.demo_camera'), self.hass.loop).result()
 def test_get_image_fails(self):
     """Try to get image with timeout."""
     with patch('homeassistant.components.camera.Camera.async_camera_image',
                return_value=mock_coro(None)), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(camera.async_get_image(
             self.hass, 'camera.demo_camera'), self.hass.loop).result()
 def test_get_image_with_timeout(self):
     """Try to get image with timeout."""
     with patch('homeassistant.components.camera.Camera.async_camera_image',
                side_effect=asyncio.TimeoutError), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(camera.async_get_image(
             self.hass, 'camera.demo_camera'), self.hass.loop).result()
    def test_get_image_without_exists_camera(self):
        """Try to get image without exists camera."""
        self.hass.states.remove('camera.demo_camera')

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(camera.async_get_image(
                self.hass, 'camera.demo_camera'), self.hass.loop).result()
Exemple #6
0
    def test_get_image_without_exists_camera(self):
        """Try to get image without exists camera."""
        self.hass.states.remove('camera.demo_camera')

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(
                camera.async_get_image(self.hass, 'camera.demo_camera'),
                self.hass.loop).result()
Exemple #7
0
 def test_get_image_fails(self):
     """Try to get image with timeout."""
     with patch('homeassistant.components.camera.Camera.async_camera_image',
                return_value=mock_coro(None)), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(
             camera.async_get_image(self.hass, 'camera.demo_camera'),
             self.hass.loop).result()
Exemple #8
0
 def test_get_image_with_timeout(self):
     """Try to get image with timeout."""
     with patch('homeassistant.components.camera.Camera.async_camera_image',
                side_effect=asyncio.TimeoutError), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(
             camera.async_get_image(self.hass, 'camera.demo_camera'),
             self.hass.loop).result()
Exemple #9
0
 def test_get_image_without_exists_camera(self):
     """Try to get image without exists camera."""
     with patch('homeassistant.helpers.entity_component.EntityComponent.'
                'get_entity', return_value=None), \
             pytest.raises(HomeAssistantError):
         run_coroutine_threadsafe(
             camera.async_get_image(self.hass, 'camera.demo_camera'),
             self.hass.loop).result()
    def test_get_image_from_camera(self, mock_camera):
        """Grab an image from camera entity."""
        self.hass.start()

        image = run_coroutine_threadsafe(camera.async_get_image(
            self.hass, 'camera.demo_camera'), self.hass.loop).result()

        assert mock_camera.called
        assert image.content == b'Test'
    def test_get_image_with_bad_http_state(self, aioclient_mock):
        """Try to get image with bad http status."""
        aioclient_mock.get(self.url, status=400)

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(camera.async_get_image(
                self.hass, 'camera.demo_camera'), self.hass.loop).result()

        assert len(aioclient_mock.mock_calls) == 1
    def test_get_image_with_timeout(self, aioclient_mock):
        """Try to get image with timeout."""
        aioclient_mock.get(self.url, exc=asyncio.TimeoutError())

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(camera.async_get_image(
                self.hass, 'camera.demo_camera'), self.hass.loop).result()

        assert len(aioclient_mock.mock_calls) == 1
Exemple #13
0
    def test_get_image_from_camera(self, mock_camera):
        """Grab a image from camera entity."""
        self.hass.start()

        image = run_coroutine_threadsafe(
            camera.async_get_image(self.hass, 'camera.demo_camera'),
            self.hass.loop).result()

        assert mock_camera.called
        assert image == b'Test'
Exemple #14
0
    def test_get_image_from_camera(self, mock_camera):
        """Grab an image from camera entity."""
        self.hass.start()

        image = asyncio.run_coroutine_threadsafe(
            camera.async_get_image(self.hass, "camera.demo_camera"),
            self.hass.loop).result()

        assert mock_camera.called
        assert image.content == b"Test"
Exemple #15
0
    def test_get_image_with_timeout(self, aioclient_mock):
        """Try to get image with timeout."""
        aioclient_mock.get(self.url, exc=asyncio.TimeoutError())

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(
                camera.async_get_image(self.hass, 'camera.demo_camera'),
                self.hass.loop).result()

        assert len(aioclient_mock.mock_calls) == 1
Exemple #16
0
    def test_get_image_with_bad_http_state(self, aioclient_mock):
        """Try to get image with bad http status."""
        aioclient_mock.get(self.url, status=400)

        with pytest.raises(HomeAssistantError):
            run_coroutine_threadsafe(
                camera.async_get_image(self.hass, 'camera.demo_camera'),
                self.hass.loop).result()

        assert len(aioclient_mock.mock_calls) == 1
async def test_camera_image(hass: HomeAssistant) -> None:
    """Test retrieving a single camera image."""
    client = create_mock_client()
    client.async_send_image_stream_start = AsyncMock(return_value=True)
    client.async_send_image_stream_stop = AsyncMock(return_value=True)

    await setup_test_config_entry(hass, hyperion_client=client)

    get_image_coro = async_get_image(hass, TEST_CAMERA_ENTITY_ID)
    image_stream_update_coro = async_call_registered_callback(
        client, "ledcolors-imagestream-update", TEST_IMAGE_UPDATE)
    result = await asyncio.gather(get_image_coro, image_stream_update_coro)

    assert client.async_send_image_stream_start.called
    assert client.async_send_image_stream_stop.called
    assert result[0].content == TEST_IMAGE_DATA.encode()