File tree Expand file tree Collapse file tree 1 file changed +18
-4
lines changed
esphome/components/satellite1/media_player Expand file tree Collapse file tree 1 file changed +18
-4
lines changed Original file line number Diff line number Diff line change 33import hashlib
44import logging
55from pathlib import Path
6- from magic import Magic
76
87import esphome .codegen as cg
98import esphome .config_validation as cv
@@ -215,13 +214,28 @@ def _read_audio_file_and_type(file_config):
215214 with open (path , "rb" ) as f :
216215 data = f .read ()
217216
218- magic = Magic (mime = True )
219- file_type = magic .from_buffer (data )
217+ try :
218+ import puremagic
219+
220+ file_type : str = puremagic .from_string (data )
221+ except ImportError :
222+ try :
223+ from magic import Magic
224+
225+ magic = Magic (mime = True )
226+ file_type : str = magic .from_buffer (data )
227+ except ImportError as exc :
228+ raise cv .Invalid ("Please install puremagic" ) from exc
229+
230+ if file_type .startswith ("." ):
231+ file_type = file_type [1 :]
232+ elif file_type .startswith ("audio/" ):
233+ file_type = file_type [6 :]
220234
221235 media_file_type = MEDIA_FILE_TYPE_ENUM ["NONE" ]
222236 if "wav" in file_type :
223237 media_file_type = MEDIA_FILE_TYPE_ENUM ["WAV" ]
224- elif " mpeg" in file_type :
238+ elif file_type in ( "mp3" , " mpeg", "mpga" ) :
225239 media_file_type = MEDIA_FILE_TYPE_ENUM ["MP3" ]
226240 elif "flac" in file_type :
227241 media_file_type = MEDIA_FILE_TYPE_ENUM ["FLAC" ]
You can’t perform that action at this time.
0 commit comments