def temp_connect(self, hardware: hc.API):
        """ Connect temporarily to the specified hardware controller.

        This should be used as a context manager:

        .. code-block :: python

            with ctx.temp_connect(hw):
                # do some tasks
                ctx.home()
            # after the with block, the context is connected to the same
            # hardware control API it was connected to before, even if
            # an error occured in the code inside the with block

        """
        old_hw = self._hw_manager.hardware
        try:
            self._hw_manager.set_hw(hardware)
            yield self
        finally:
            self._hw_manager.set_hw(old_hw)