To get rid of the block above, you'll need to change this:
properties.get('parentId', None)

To:
properties.get('parentId', ROOT_ENTITY)