Help with serving media files in my Home Assistant integration

Hi everyone,

I’m developing a custom integration that downloads media (e.g., pictures) generated by a smart pet feeder. The integration saves these files to a specified path in my case, /media/my_nas. My code is able to browse through the folders and display the images, but it only works on the local network.

Here’s the relevant code I use to generate the media link:

async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
    """Resolve media to a URL/path."""
    file_path = self.media_path / Path(item.identifier)
    if not file_path.exists():
        raise ValueError(f"File not found: {file_path}")

    url = async_process_play_media_url(
        self.hass, f"/media/local/{file_path.relative_to(self.media_path)}"
    )
    mime_type = self.get_mime_type(file_path.suffix)
    return PlayMedia(url, mime_type)

When accessing the image locally, the browser can load and display the file correctly. For example, the URL generated looks like this:

http://192.168.0.200:8123/media/local/my_nas/300004409/20250124/eat/snapshot/300004409_1737716796.jpg?authSig=xxxxxxxxxxxxxxxxxxxxxx

However, this only works when I’m on the local network. If I try to access the image remotely (from outside my network), the same URL fails, and I get an error.

Observations:

  1. On the local network, the path /media/local/my_nas/... works fine.
  2. The path /media/my_nas/... (without /local) doesn’t seem to work at all.
  3. The URL generated doesn’t seem to adapt for remote access, even though I’m using async_process_play_media_url.

Question:

How can I make these media files accessible both locally and remotely? Is there something wrong with the paths I’m using or the way I’m generating URLs?
Any guidance on resolving this issue would be greatly appreciated!

OK i answer myself if it could help someone :
The solution was to add theses parameters:

allow_relative_url=True,
for_supervisor_network=True,

at async_process_play_media_url