diff --git a/homeassistant/components/motioneye/media_source.py b/homeassistant/components/motioneye/media_source.py index 85fe3985b93..20fc4359ab2 100644 --- a/homeassistant/components/motioneye/media_source.py +++ b/homeassistant/components/motioneye/media_source.py @@ -284,7 +284,13 @@ class MotionEyeMediaSource(MediaSource): sub_dirs: set[str] = set() parts = parsed_path.parts - for media in resp.get(KEY_MEDIA_LIST, []): + media_list = resp.get(KEY_MEDIA_LIST, []) + + def get_media_sort_key(media: dict) -> str: + """Get media sort key.""" + return media.get(KEY_PATH, "") + + for media in sorted(media_list, key=get_media_sort_key): if ( KEY_PATH not in media or KEY_MIME_TYPE not in media diff --git a/tests/components/motioneye/test_media_source.py b/tests/components/motioneye/test_media_source.py index 541db872b51..b4803c52a6d 100644 --- a/tests/components/motioneye/test_media_source.py +++ b/tests/components/motioneye/test_media_source.py @@ -250,6 +250,20 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "children_media_class": "video", "thumbnail": None, "children": [ + { + "title": "00-02-27.mp4", + "media_class": "video", + "media_content_type": "video/mp4", + "media_content_id": ( + "media-source://motioneye" + f"/74565ad414754616000674c87bdc876c#{device.id}#movies#" + "/2021-04-25/00-02-27.mp4" + ), + "can_play": True, + "can_expand": False, + "thumbnail": "http://movie", + "children_media_class": None, + }, { "title": "00-26-22.mp4", "media_class": "video", @@ -278,20 +292,6 @@ async def test_async_browse_media_success(hass: HomeAssistant) -> None: "thumbnail": "http://movie", "children_media_class": None, }, - { - "title": "00-02-27.mp4", - "media_class": "video", - "media_content_type": "video/mp4", - "media_content_id": ( - "media-source://motioneye" - f"/74565ad414754616000674c87bdc876c#{device.id}#movies#" - "/2021-04-25/00-02-27.mp4" - ), - "can_play": True, - "can_expand": False, - "thumbnail": "http://movie", - "children_media_class": None, - }, ], "not_shown": 0, }