1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-05-22 11:25:46 +08:00

fix: 优化登录异常处理和设备发现逻辑,执行命令前先停止小爱避免播放不支持提示 (#871)

* feat: 增加搜索多结果选择功能

新增功能:
 - 搜索结果多条记录时通过TTS告知用户匹配数量
 - 支持用户重新呼叫'第X个'来选择并播放指定歌曲
 - 实现记忆机制:选择后保留待选列表,支持持续多次选择
 - 新增配置项 fuzzy_match_max_results 控制最大返回数量(默认100)

优化改进:
 - 搜索结果排序:从随机排序改为按文件名自然排序(custom_sort_key)
 - 日志输出优化:多结果时每个歌曲分行显示,带序号便于查看

修改文件:
 - command_handler.py: 添加待选择状态检查逻辑,优先匹配'第X个'指令
 - config.py: 新增 fuzzy_match_max_results 配置项
 - device_player.py: 添加 _pending_selection 属性、多结果处理逻辑、handle_selection 方法、优化日志格式
 - music_library.py: 将 random.shuffle 改为 sort(key=custom_sort_key) 自然排序
 - xiaomusic.py: 新增 select_index 命令处理方法

* fix: 优化登录异常处理和设备发现逻辑,执行命令前先停止小爱避免播放不支持提示

* style: ruff lint and format fix
This commit is contained in:
jokinas
2026-05-14 22:59:56 +08:00
committed by GitHub
parent ff8bb26c6e
commit 497d0d70de
6 changed files with 18 additions and 9 deletions

0
newpatch.sh Executable file → Normal file
View File

0
newversion.sh Executable file → Normal file
View File

0
update-holiday.sh Executable file → Normal file
View File

0
update-static-version.py Executable file → Normal file
View File

View File

@@ -124,6 +124,12 @@ class AuthManager:
self.login_acount = self.config.account
self.login_password = self.config.password
self.log.info(f"登录完成. {self.login_acount}")
except KeyError as e:
self.mina_service = None
self.miio_service = None
self.log.warning(
f"登录失败API响应格式错误: {e}。建议使用Cookie登录或访问小米官网验证"
)
except Exception as e:
self.mina_service = None
self.miio_service = None
@@ -148,15 +154,15 @@ class AuthManager:
name = h.get("alias", "")
if not name:
name = h.get("name", "未知名字")
if device_id and hardware and did and (did in mi_dids):
device = self.config.devices.get(did, Device())
device.did = did
# 将did存一下 方便其他地方调用
self._cur_did = did
device.device_id = device_id
device.hardware = hardware
device.name = name
devices[did] = device
if device_id and hardware and did:
if not mi_dids or not mi_dids[0] or (did in mi_dids):
device = self.config.devices.get(did, Device())
device.did = did
self._cur_did = did
device.device_id = device_id
device.hardware = hardware
device.name = name
devices[did] = device
self.config.devices = devices
self.log.info(f"选中的设备: {devices}")
return devices

View File

@@ -63,6 +63,9 @@ class CommandHandler:
await device.check_replay()
return
# 执行命令前先停止小爱,避免播放"不支持"提示
await device.group_force_stop_xiaoai()
# 执行命令
func = getattr(self.xiaomusic, opvalue)
await func(did=did, arg1=oparg)